@onigeya/dsh-kanabo-memory 0.0.0-stage → 0.1.1
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/backfill-child.js +310 -0
- package/backfill-vectors.js +125 -0
- package/contract.js +114 -0
- package/cordis.patch.yml +7 -0
- package/cursor.js +116 -0
- package/dedupe.js +101 -0
- package/egress.js +790 -0
- package/embed-child.js +152 -0
- package/embed.js +475 -0
- package/envelope.js +368 -0
- package/extract.js +420 -0
- package/idle-interrupt.js +922 -0
- package/index.js +154 -0
- package/migrate.js +1723 -0
- package/package.json +68 -3
- package/persona.js +949 -0
- package/providers.js +786 -0
- package/query-window.js +562 -0
- package/recall-stages.js +150 -0
- package/recall-trivium.js +916 -0
- package/recall.js +129 -0
- package/remove-legacy.mjs +720 -0
- package/retention-sweep.js +124 -0
- package/serve.js +138 -0
- package/store-trivium-child.js +447 -0
- package/store-trivium.js +510 -0
- package/store.js +347 -0
- package/sweep.js +296 -0
- package/tools.js +507 -0
- package/trivium-runtime.js +1900 -0
- package/README.md +0 -3
package/dedupe.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 写入期的 decision 去重:Hindsight consolidation 的 `restates` 分流,落到本项目的 `remember` 上。
|
|
3
|
+
*
|
|
4
|
+
* Hindsight 在 retain → consolidation 链路上先用判定模型把「新事实重复了某条既有 observation」与
|
|
5
|
+
* 「新事实改变 / 是新的」分开,重复的只追加证据、不进 LLM(`consolidation/triage.py` 注释算过
|
|
6
|
+
* 约 40% 的调用节省)。本项目没有 observation 层,但有等价的决策点:**新条目是否与既有条目
|
|
7
|
+
* 说的是同一件事**。判为重复就不发新节点、直接返回既有 id,于是库里不会出现两条同义记忆。
|
|
8
|
+
*
|
|
9
|
+
* 三条边界:
|
|
10
|
+
* - 先向量近邻(默认 minScore 0.8)把候选压到 ≤3 条,再逐对让判定模型回答;判定模型不做检索。
|
|
11
|
+
* - 只问「B 是否重复 A」(`DEDUP_INSTRUCTIONS` 原文),不问「是不是闲聊」——本项目「每个回合都
|
|
12
|
+
* 值得记住」是既有契约,去重不能变相变成丢记忆。
|
|
13
|
+
* - 任何失败(未配置 / 超时 / 形状不对)都返回 `null`,调用方照常写入:最佳努力,绝不阻断写入。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { DEDUP_INSTRUCTIONS } from './providers.js'
|
|
17
|
+
|
|
18
|
+
const MAX_CANDIDATES = 3
|
|
19
|
+
const MIN_VECTOR_SCORE = 0.8
|
|
20
|
+
const REPEAT_THRESHOLD = 0.5
|
|
21
|
+
|
|
22
|
+
function roundMs(value) {
|
|
23
|
+
return Math.round(value * 10) / 10
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {object} options
|
|
28
|
+
* @param {object|null} options.judge `providers.createDecisionClient()` 的返回对象
|
|
29
|
+
* @param {(vector:number[], options:object) => Promise<object>} options.search 向量近邻搜索(注入,便于探针)
|
|
30
|
+
* @param {number[]|null} options.vector 新条目的向量
|
|
31
|
+
* @param {string} options.text 新条目的正文
|
|
32
|
+
* @param {number} [options.minScore] 近邻的最低余弦,默认 0.8
|
|
33
|
+
* @param {number} [options.threshold] 判「重复」的最低概率,默认 0.5
|
|
34
|
+
* @returns {Promise<{id:string, text:string, score:number, probability:number, ms:number}|null>}
|
|
35
|
+
*/
|
|
36
|
+
function textOfPayload(payload) {
|
|
37
|
+
if (payload === null || typeof payload !== 'object') return ''
|
|
38
|
+
const fromEnvelope = payload.envelope !== null && typeof payload.envelope === 'object' ? payload.envelope.text : null
|
|
39
|
+
for (const value of [fromEnvelope, payload.text, payload.scene_description, payload.topic]) {
|
|
40
|
+
if (typeof value === 'string' && value.trim() !== '') return value.trim()
|
|
41
|
+
}
|
|
42
|
+
return ''
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function findRestatingMemory(options = {}) {
|
|
46
|
+
const judge = options.judge ?? null
|
|
47
|
+
const vector = Array.isArray(options.vector) ? options.vector : null
|
|
48
|
+
const text = typeof options.text === 'string' ? options.text.trim() : ''
|
|
49
|
+
if (judge === null || judge.enabled !== true) return null
|
|
50
|
+
if (vector === null || vector.length === 0 || text === '') return null
|
|
51
|
+
if (typeof options.search !== 'function') return null
|
|
52
|
+
|
|
53
|
+
const started = performance.now()
|
|
54
|
+
let found
|
|
55
|
+
try {
|
|
56
|
+
found = await options.search(vector, {
|
|
57
|
+
topK: MAX_CANDIDATES,
|
|
58
|
+
expandDepth: 0,
|
|
59
|
+
minScore: Number.isFinite(options.minScore) ? options.minScore : MIN_VECTOR_SCORE,
|
|
60
|
+
})
|
|
61
|
+
} catch {
|
|
62
|
+
return null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const candidates = (Array.isArray(found?.hits) ? found.hits : [])
|
|
66
|
+
.map((hit) => ({
|
|
67
|
+
id: String(hit?.payload?.legacy_id ?? hit?.payload?.id ?? hit?.id ?? ''),
|
|
68
|
+
text: textOfPayload(hit?.payload),
|
|
69
|
+
score: Number(hit?.score) || 0,
|
|
70
|
+
}))
|
|
71
|
+
.filter((row) => row.id !== '' && row.text !== '' && row.text !== text)
|
|
72
|
+
.slice(0, MAX_CANDIDATES)
|
|
73
|
+
if (candidates.length === 0) return null
|
|
74
|
+
|
|
75
|
+
const questions = candidates.map((row, index) => ({
|
|
76
|
+
key: `d${index}`,
|
|
77
|
+
instructions: { task: DEDUP_INSTRUCTIONS, memory_a: row.text, memory_b: text },
|
|
78
|
+
}))
|
|
79
|
+
|
|
80
|
+
let verdict
|
|
81
|
+
try {
|
|
82
|
+
verdict = await judge.judge('Question: Which existing memory does the new memory repeat?', questions)
|
|
83
|
+
} catch {
|
|
84
|
+
return null
|
|
85
|
+
}
|
|
86
|
+
if (verdict?.ok !== true) return null
|
|
87
|
+
|
|
88
|
+
const threshold = Number.isFinite(options.threshold) ? options.threshold : REPEAT_THRESHOLD
|
|
89
|
+
let best = null
|
|
90
|
+
for (let index = 0; index < questions.length; index += 1) {
|
|
91
|
+
const probability = Number(verdict.probabilities?.[questions[index].key])
|
|
92
|
+
if (!Number.isFinite(probability) || probability < threshold) continue
|
|
93
|
+
const candidate = candidates[index]
|
|
94
|
+
if (candidate === undefined) continue
|
|
95
|
+
if (best === null || probability > best.probability) best = { ...candidate, probability }
|
|
96
|
+
}
|
|
97
|
+
if (best === null) return null
|
|
98
|
+
return { id: best.id, text: best.text, score: best.score, probability: best.probability, ms: roundMs(performance.now() - started) }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export default findRestatingMemory
|