@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/recall.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import { MAX_INJECT_BYTES, clip, scopeOf, textOf } from './store.js'
|
|
3
|
+
import * as defaultRuntime from './trivium-runtime.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 回合召回:**纯 provider**(平台计划 todo 16 第③项)。本文件不注册任何钩子——
|
|
7
|
+
* `agent/pre-step` 的唯一注册者是任务 37 的 `serve.js`,它调用本文件的 `getRecallContext()`。
|
|
8
|
+
*
|
|
9
|
+
* 为什么去掉注册:旧实现同时挂了 `agent/pre-step`(主路径)与 `systemPrompt.context`(兜底),
|
|
10
|
+
* 两条路各自读存储并在健康路径旁再注入一块的风险一直存在;`serve.js` 成为唯一所有者后,
|
|
11
|
+
* 召回时序(assemble → pre-step → refreshContext → agent/request)只由它负责。
|
|
12
|
+
*
|
|
13
|
+
* 注入消息必须带非空字符串 `id`(`.omo/evidence/dsh-kanabo-memory-measured-facts.md` §R1):
|
|
14
|
+
* `assertMessageEventShape` 在重放时校验它,缺 id 的会话从该 seq 起永久不可读。`injectionMessage`
|
|
15
|
+
* 保留旧形状,供 serve 直接使用。
|
|
16
|
+
*
|
|
17
|
+
* 措辞必须是 workspace 而不是 session(§V2):存储按 workspace 分域、跨会话共享。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** 召回块的头行。`migrate.js#detectOwnInjection` 逐字依赖它;改这句话必须同步迁移侧。 */
|
|
21
|
+
export const RECALL_HEADER = "Recalled from this workspace's memory (shared across sessions):"
|
|
22
|
+
|
|
23
|
+
const RECALL_LIMIT = 8
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 逐条塞进字节预算,因此末尾的条目数与真实进入块内的条目**一致**;放不下的条目换成一行省略号,
|
|
27
|
+
* 而不是悄悄少给却仍报全部条数。
|
|
28
|
+
*/
|
|
29
|
+
export function blockFor(entries) {
|
|
30
|
+
const footerRoom = Buffer.byteLength(`\n(${String(entries.length)} entries)`, 'utf8')
|
|
31
|
+
const header = `${RECALL_HEADER}\n`
|
|
32
|
+
let used = Buffer.byteLength(header, 'utf8')
|
|
33
|
+
const limit = MAX_INJECT_BYTES - footerRoom
|
|
34
|
+
const lines = []
|
|
35
|
+
for (const entry of entries) {
|
|
36
|
+
const room = limit - used - 1
|
|
37
|
+
if (room <= 1) break
|
|
38
|
+
const line = clip(`- ${textOf(entry)}`, room)
|
|
39
|
+
used += Buffer.byteLength(line, 'utf8') + 1
|
|
40
|
+
lines.push(line)
|
|
41
|
+
}
|
|
42
|
+
if (lines.length < entries.length) {
|
|
43
|
+
const mark = '- …'
|
|
44
|
+
if (used + Buffer.byteLength(mark, 'utf8') + 1 <= limit) lines.push(mark)
|
|
45
|
+
}
|
|
46
|
+
const footer = `(${lines.filter(line => line !== '- …').length} entries)`
|
|
47
|
+
return `${header}${lines.join('\n')}\n${footer}`
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function injectionMessage(text) {
|
|
51
|
+
return {
|
|
52
|
+
id: `kanabo-recall-${randomUUID()}`,
|
|
53
|
+
role: 'user',
|
|
54
|
+
content: [{ type: 'text', text }],
|
|
55
|
+
source: { kind: 'kanabo-memory', form: 'snapshot', sections: [{ name: 'kanabo-memory', text }] },
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function describe(error) {
|
|
60
|
+
if (error instanceof Error) {
|
|
61
|
+
const code = typeof error.code === 'string' && error.code !== '' ? `${error.code}: ` : ''
|
|
62
|
+
return `${code}${error.message}`
|
|
63
|
+
}
|
|
64
|
+
return String(error)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 给 `serve.js` 的唯一入口:按当前会话的 workspace 与用户提示词召回,返回可注入的文本块。
|
|
69
|
+
*
|
|
70
|
+
* 契约:
|
|
71
|
+
* - **绝不抛错**(召回失败不得让回合失败):任何失败都返回 `text: ''` 与 `degraded` 原因;
|
|
72
|
+
* - 空提示词/空白提示词不召回(零信息查询不得注入记忆);
|
|
73
|
+
* - 存储经 `trivium-runtime.js` 的进程级注册表——与工具/活写入/扫描/时间窗是**同一个**门面。
|
|
74
|
+
*
|
|
75
|
+
* @param {object} options
|
|
76
|
+
* @param {object} options.session 会话(`session.header.cwd` 决定 workspace 分域键)
|
|
77
|
+
* @param {string} [options.prompt] 用户提示词
|
|
78
|
+
* @param {number} [options.limit] 最多召回条数(默认 8,夹到 1..MAX_INJECT_BYTES 量级)
|
|
79
|
+
* @param {object} [options.runtime] 自足探针注入的运行时
|
|
80
|
+
* @returns {Promise<{text: string, entries: number, source: 'trivium', degraded: object|null, stages?: object}>}
|
|
81
|
+
*/
|
|
82
|
+
export async function getRecallContext(options = {}) {
|
|
83
|
+
const runtime = options.runtime ?? defaultRuntime
|
|
84
|
+
const limit = Number.isInteger(options.limit) && options.limit > 0 ? Math.min(options.limit, 50) : RECALL_LIMIT
|
|
85
|
+
const prompt = typeof options.prompt === 'string' ? options.prompt.trim() : ''
|
|
86
|
+
const empty = (degraded = null) => ({ text: '', entries: 0, source: 'trivium', degraded })
|
|
87
|
+
if (prompt === '') return empty()
|
|
88
|
+
let scope
|
|
89
|
+
try {
|
|
90
|
+
scope = scopeOf(options.session)
|
|
91
|
+
} catch (error) {
|
|
92
|
+
return empty({ code: 'KANABO_RECALL_SCOPE', message: describe(error) })
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const { createTriviumRecall } = await import('./recall-trivium.js')
|
|
96
|
+
const { createModelClients, activeEmbeddingPrefixes } = await import('./providers.js')
|
|
97
|
+
const clients = createModelClients()
|
|
98
|
+
const embedder = clients.embeddings === null ? null : runtime.createServiceEmbedder()
|
|
99
|
+
const hits = await runtime.withFacade(scope, async (facade) => {
|
|
100
|
+
const recall = createTriviumRecall({
|
|
101
|
+
store: facade,
|
|
102
|
+
arms: { text: true, vector: embedder !== null, graph: true },
|
|
103
|
+
embedder,
|
|
104
|
+
queryPrefix: activeEmbeddingPrefixes().query,
|
|
105
|
+
reranker: clients.rerank,
|
|
106
|
+
judge: clients.decision,
|
|
107
|
+
judgeOptions: {
|
|
108
|
+
maxCandidates: clients.config.decision?.maxCandidates,
|
|
109
|
+
minProbability: clients.config.decision?.minProbability,
|
|
110
|
+
keepBest: clients.config.decision?.keepBest,
|
|
111
|
+
required: clients.config.decision?.required,
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
try {
|
|
115
|
+
const result = await recall.recall(prompt, { limit, budgetChars: Number.MAX_SAFE_INTEGER })
|
|
116
|
+
return { hits: result.hits.map((hit) => ({ text: hit.text, id: hit.id, ts: hit.ts })), stages: result.stages }
|
|
117
|
+
} finally {
|
|
118
|
+
await recall.close()
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
const usable = hits.hits.filter((hit) => typeof hit.text === 'string' && hit.text.trim() !== '')
|
|
122
|
+
if (usable.length === 0) return { ...empty(), stages: hits.stages }
|
|
123
|
+
return { text: blockFor(usable), entries: usable.length, source: 'trivium', degraded: null, stages: hits.stages }
|
|
124
|
+
} catch (error) {
|
|
125
|
+
return empty({ code: typeof error?.code === 'string' ? error.code : 'KANABO_RECALL_PROVIDER', message: describe(error) })
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export default getRecallContext
|