dsh-macro-memory 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +506 -2
- package/cordis.patch.yml +6 -0
- package/lib/client.js +3623 -0
- package/lib/index.js +502 -0
- package/lib/macro/brand.js +82 -0
- package/lib/macro/bridge.js +158 -0
- package/lib/macro/chain.js +206 -0
- package/lib/macro/contract.js +471 -0
- package/lib/macro/cost.js +164 -0
- package/lib/macro/defs/admin.js +442 -0
- package/lib/macro/defs/dream.js +24 -0
- package/lib/macro/defs/link.js +257 -0
- package/lib/macro/defs/project.js +25 -0
- package/lib/macro/defs/read.js +160 -0
- package/lib/macro/defs/remember.js +411 -0
- package/lib/macro/defs/search.js +871 -0
- package/lib/macro/defs/update.js +161 -0
- package/lib/macro/dreamledger.js +1398 -0
- package/lib/macro/facade.js +135 -0
- package/lib/macro/fusion.js +182 -0
- package/lib/macro/linkflow.js +391 -0
- package/lib/macro/links.js +448 -0
- package/lib/macro/neighbor.js +209 -0
- package/lib/macro/panel.js +96 -0
- package/lib/macro/prompt.js +139 -0
- package/lib/macro/provenance.js +148 -0
- package/lib/macro/render.js +35 -0
- package/lib/macro/revisions.js +253 -0
- package/lib/macro/shared.js +113 -0
- package/lib/macro/store.js +103 -0
- package/lib/macro/supersede.js +197 -0
- package/lib/macro/tokencost.js +90 -0
- package/lib/macro/tools.js +67 -0
- package/package.json +54 -4
- package/vendor/VENDOR.json +23 -0
- package/vendor/dsh-memory-nebula/CONTRIBUTING.md +48 -0
- package/vendor/dsh-memory-nebula/LICENSE +21 -0
- package/vendor/dsh-memory-nebula/README.en.md +203 -0
- package/vendor/dsh-memory-nebula/README.md +253 -0
- package/vendor/dsh-memory-nebula/SECURITY.md +43 -0
- package/vendor/dsh-memory-nebula/SUPPORT.md +33 -0
- package/vendor/dsh-memory-nebula/cordis.patch.yml +4 -0
- package/vendor/dsh-memory-nebula/lib/client.js +105 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/index.js +279 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/ingest.js +230 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/meow.js +320 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/notes.js +305 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/sqlite.js +287 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/util.js +231 -0
- package/vendor/dsh-memory-nebula/lib/host/bridge.js +351 -0
- package/vendor/dsh-memory-nebula/lib/host/compress.js +97 -0
- package/vendor/dsh-memory-nebula/lib/host/demo.js +357 -0
- package/vendor/dsh-memory-nebula/lib/host/diagnostics.js +125 -0
- package/vendor/dsh-memory-nebula/lib/host/doctor.js +270 -0
- package/vendor/dsh-memory-nebula/lib/host/entities.js +230 -0
- package/vendor/dsh-memory-nebula/lib/host/graph.js +1762 -0
- package/vendor/dsh-memory-nebula/lib/host/graphcache.js +290 -0
- package/vendor/dsh-memory-nebula/lib/host/http.js +993 -0
- package/vendor/dsh-memory-nebula/lib/host/injectcfg.js +95 -0
- package/vendor/dsh-memory-nebula/lib/host/ledger.js +175 -0
- package/vendor/dsh-memory-nebula/lib/host/live.js +204 -0
- package/vendor/dsh-memory-nebula/lib/host/nebula.js +2105 -0
- package/vendor/dsh-memory-nebula/lib/host/page.js +75 -0
- package/vendor/dsh-memory-nebula/lib/host/path.js +83 -0
- package/vendor/dsh-memory-nebula/lib/host/recall.js +524 -0
- package/vendor/dsh-memory-nebula/lib/host/share.js +633 -0
- package/vendor/dsh-memory-nebula/lib/host/sources.js +266 -0
- package/vendor/dsh-memory-nebula/lib/host/store.js +333 -0
- package/vendor/dsh-memory-nebula/lib/host/text.js +410 -0
- package/vendor/dsh-memory-nebula/lib/host/transcripts.js +396 -0
- package/vendor/dsh-memory-nebula/lib/index.js +782 -0
- package/vendor/dsh-memory-nebula/lib/standalone.mjs +315 -0
- package/vendor/dsh-memory-nebula/lib/web/api.js +426 -0
- package/vendor/dsh-memory-nebula/lib/web/app.js +4172 -0
- package/vendor/dsh-memory-nebula/lib/web/i18n.js +580 -0
- package/vendor/dsh-memory-nebula/lib/web/index.html +174 -0
- package/vendor/dsh-memory-nebula/lib/web/nav.js +132 -0
- package/vendor/dsh-memory-nebula/lib/web/render.js +1890 -0
- package/vendor/dsh-memory-nebula/lib/web/space3d.js +1883 -0
- package/vendor/dsh-memory-nebula/lib/web/style.css +1402 -0
- package/vendor/dsh-memory-nebula/locale/en.json +7 -0
- package/vendor/dsh-memory-nebula/locale/zh.json +7 -0
- package/vendor/dsh-memory-nebula/market/assets/icon-180.png +0 -0
- package/vendor/dsh-memory-nebula/market/assets/icon.svg +40 -0
- package/vendor/dsh-memory-nebula/market/index.json +101 -0
- package/vendor/dsh-memory-nebula/package.json +87 -0
- package/vendor/dsh-memory-nebula/sidecar.ps1 +119 -0
- package/vendor/meow-memory/LICENSE +21 -0
- package/vendor/meow-memory/README.en.md +192 -0
- package/vendor/meow-memory/README.md +174 -0
- package/vendor/meow-memory/README.pt-br.md +180 -0
- package/vendor/meow-memory/cordis.patch.yml +7 -0
- package/vendor/meow-memory/lib/client.js +3344 -0
- package/vendor/meow-memory/lib/client.js.map +7 -0
- package/vendor/meow-memory/lib/index.js +9802 -0
- package/vendor/meow-memory/lib/index.js.map +7 -0
- package/vendor/meow-memory/lib/prompts/README.md +65 -0
- package/vendor/meow-memory/lib/prompts/en/dream-atomic.md +36 -0
- package/vendor/meow-memory/lib/prompts/en/dream-header.md +9 -0
- package/vendor/meow-memory/lib/prompts/en/dream-project-summary.md +12 -0
- package/vendor/meow-memory/lib/prompts/en/dream-topic.md +27 -0
- package/vendor/meow-memory/lib/prompts/en/external-memory-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/en/first-create-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/en/labels.md +98 -0
- package/vendor/meow-memory/lib/prompts/en/reflect.md +31 -0
- package/vendor/meow-memory/lib/prompts/en/system-guide.md +121 -0
- package/vendor/meow-memory/lib/prompts/en/tools.md +83 -0
- package/vendor/meow-memory/lib/prompts/en/welcome-guide.md +12 -0
- package/vendor/meow-memory/lib/prompts/pt-br/dream-atomic.md +36 -0
- package/vendor/meow-memory/lib/prompts/pt-br/dream-header.md +9 -0
- package/vendor/meow-memory/lib/prompts/pt-br/dream-project-summary.md +12 -0
- package/vendor/meow-memory/lib/prompts/pt-br/dream-topic.md +27 -0
- package/vendor/meow-memory/lib/prompts/pt-br/external-memory-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/pt-br/first-create-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/pt-br/labels.md +98 -0
- package/vendor/meow-memory/lib/prompts/pt-br/reflect.md +31 -0
- package/vendor/meow-memory/lib/prompts/pt-br/system-guide.md +121 -0
- package/vendor/meow-memory/lib/prompts/pt-br/tools.md +83 -0
- package/vendor/meow-memory/lib/prompts/pt-br/welcome-guide.md +12 -0
- package/vendor/meow-memory/lib/prompts/zh/dream-atomic.md +36 -0
- package/vendor/meow-memory/lib/prompts/zh/dream-header.md +9 -0
- package/vendor/meow-memory/lib/prompts/zh/dream-project-summary.md +19 -0
- package/vendor/meow-memory/lib/prompts/zh/dream-topic.md +27 -0
- package/vendor/meow-memory/lib/prompts/zh/external-memory-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/zh/first-create-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/zh/labels.md +98 -0
- package/vendor/meow-memory/lib/prompts/zh/reflect.md +30 -0
- package/vendor/meow-memory/lib/prompts/zh/system-guide.md +119 -0
- package/vendor/meow-memory/lib/prompts/zh/tools.md +83 -0
- package/vendor/meow-memory/lib/prompts/zh/welcome-guide.md +12 -0
- package/vendor/meow-memory/package.json +64 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// 大图分页:把一张图按「重要度 → 度数 → id」的稳定顺序切片。
|
|
2
|
+
//
|
|
3
|
+
// 为什么需要它:5 万条时 /graph 一次性序列化整张图约 44MB(压缩后仍有数 MB),
|
|
4
|
+
// 浏览器要等整包到齐才能出第一帧。分页把「第一屏」压到 2 万条以内,
|
|
5
|
+
// 剩下的按需追加 —— 顺序必须稳定,否则两次请求之间节点会跳页。
|
|
6
|
+
//
|
|
7
|
+
// 这个模块只做纯计算,不碰 IO,方便单测。
|
|
8
|
+
|
|
9
|
+
/** 单页最多返回多少条(超过按 50000 截断,防止一次请求把内存打满)。 */
|
|
10
|
+
export const PAGE_MAX = 50000
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 没显式给 limit 时,大图第一页的上限;小于等于它的库行为不变。
|
|
14
|
+
*
|
|
15
|
+
* 为什么是 10000 而不是 20000:5 万条的整图 JSON 约 22MB,两万条的第一页仍有
|
|
16
|
+
* 8.7MB(压缩后 ~1MB);一万条约 4.4MB,首帧明显更早,而 1 万颗点已经足够
|
|
17
|
+
* 看出整片星云的形状。剩下的按需点「继续加载」。
|
|
18
|
+
*/
|
|
19
|
+
export const FIRST_PAGE_MAX = 10000
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 稳定分页顺序:重要度高的先出现,其次度数高的,最后按 id 兜底。
|
|
23
|
+
* @param {{nodes: {importance?:number, degree?:number, id:string}[]}} graph
|
|
24
|
+
* @returns {Int32Array} order[rank] = 节点下标
|
|
25
|
+
*/
|
|
26
|
+
export function pageOrder(graph) {
|
|
27
|
+
const nodes = graph.nodes || []
|
|
28
|
+
const arr = new Array(nodes.length)
|
|
29
|
+
for (let i = 0; i < nodes.length; i++) arr[i] = i
|
|
30
|
+
arr.sort((a, b) => {
|
|
31
|
+
const A = nodes[a]
|
|
32
|
+
const B = nodes[b]
|
|
33
|
+
const imp = (B.importance || 0) - (A.importance || 0)
|
|
34
|
+
if (imp) return imp
|
|
35
|
+
const deg = (B.degree || 0) - (A.degree || 0)
|
|
36
|
+
if (deg) return deg
|
|
37
|
+
const ai = String(A.id)
|
|
38
|
+
const bi = String(B.id)
|
|
39
|
+
return ai < bi ? -1 : ai > bi ? 1 : 0
|
|
40
|
+
})
|
|
41
|
+
return Int32Array.from(arr)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 决定这一页要哪些节点。
|
|
46
|
+
* @param {{nodes: unknown[]}} graph
|
|
47
|
+
* @param {{limit?: number, offset?: number, order?: Int32Array}} opts
|
|
48
|
+
* @returns {{indices: number[], page: {total:number, offset:number, limit:number, next:number|null, order:string}}}
|
|
49
|
+
*/
|
|
50
|
+
export function selectPage(graph, opts = {}) {
|
|
51
|
+
const total = (graph.nodes || []).length
|
|
52
|
+
const order = opts.order || pageOrder(graph)
|
|
53
|
+
const rawLimit = Number(opts.limit)
|
|
54
|
+
const wanted =
|
|
55
|
+
Number.isFinite(rawLimit) && rawLimit > 0
|
|
56
|
+
? Math.min(PAGE_MAX, Math.floor(rawLimit))
|
|
57
|
+
: total > FIRST_PAGE_MAX
|
|
58
|
+
? FIRST_PAGE_MAX
|
|
59
|
+
: total
|
|
60
|
+
const rawOffset = Number(opts.offset)
|
|
61
|
+
const start = Math.min(Math.max(0, Number.isFinite(rawOffset) ? Math.floor(rawOffset) : 0), total)
|
|
62
|
+
const end = Math.min(total, start + wanted)
|
|
63
|
+
const indices = new Array(Math.max(0, end - start))
|
|
64
|
+
for (let k = start; k < end; k++) indices[k - start] = order[k]
|
|
65
|
+
return {
|
|
66
|
+
indices,
|
|
67
|
+
page: {
|
|
68
|
+
total,
|
|
69
|
+
offset: start,
|
|
70
|
+
limit: indices.length,
|
|
71
|
+
next: end < total ? end : null,
|
|
72
|
+
order: 'importance',
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// 两点之间「最短证据链」——回答“这两条记忆到底怎么连上的”
|
|
2
|
+
// 代价用 -log(w):权重越高的边越便宜,等于优先走证据最硬的那条路。
|
|
3
|
+
// 同时限制跳数,避免 800 节点图上绕一大圈出来一个“六度分隔”式的假解释。
|
|
4
|
+
|
|
5
|
+
const MAX_HOPS = 6
|
|
6
|
+
const MIN_W = 0.05
|
|
7
|
+
|
|
8
|
+
function adjacency(graph) {
|
|
9
|
+
const adj = new Map()
|
|
10
|
+
for (const e of graph.edges) {
|
|
11
|
+
const s = e.s ?? e._s
|
|
12
|
+
const t = e.t ?? e._t
|
|
13
|
+
if (s == null || t == null) continue
|
|
14
|
+
const w = Math.max(MIN_W, e.w || 0)
|
|
15
|
+
if (!adj.has(s)) adj.set(s, [])
|
|
16
|
+
if (!adj.has(t)) adj.set(t, [])
|
|
17
|
+
adj.get(s).push({ to: t, w, e })
|
|
18
|
+
adj.get(t).push({ to: s, w, e })
|
|
19
|
+
}
|
|
20
|
+
return adj
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @returns {null | {found:boolean, ids:number[], edges:object[], hops:number, score:number}}
|
|
25
|
+
*/
|
|
26
|
+
export function findPath(graph, fromIdx, toIdx, opts = {}) {
|
|
27
|
+
const maxHops = opts.maxHops || MAX_HOPS
|
|
28
|
+
if (fromIdx === toIdx) return { found: true, ids: [fromIdx], edges: [], hops: 0, score: 1 }
|
|
29
|
+
const adj = adjacency(graph)
|
|
30
|
+
if (!adj.has(fromIdx) || !adj.has(toIdx)) return null
|
|
31
|
+
|
|
32
|
+
const n = graph.nodes.length
|
|
33
|
+
const dist = new Float64Array(n).fill(Infinity)
|
|
34
|
+
const hops = new Int32Array(n).fill(1 << 30)
|
|
35
|
+
const prevEdge = new Array(n).fill(null)
|
|
36
|
+
const prevNode = new Int32Array(n).fill(-1)
|
|
37
|
+
const seen = new Uint8Array(n)
|
|
38
|
+
dist[fromIdx] = 0
|
|
39
|
+
hops[fromIdx] = 0
|
|
40
|
+
|
|
41
|
+
// 节点数 ~1000,线性取最小就够快,不必上堆
|
|
42
|
+
for (;;) {
|
|
43
|
+
let u = -1
|
|
44
|
+
let best = Infinity
|
|
45
|
+
for (let i = 0; i < n; i += 1) {
|
|
46
|
+
if (!seen[i] && dist[i] < best) {
|
|
47
|
+
best = dist[i]
|
|
48
|
+
u = i
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (u < 0) break
|
|
52
|
+
if (u === toIdx) break
|
|
53
|
+
seen[u] = 1
|
|
54
|
+
if (hops[u] >= maxHops) continue
|
|
55
|
+
for (const link of adj.get(u) || []) {
|
|
56
|
+
if (seen[link.to]) continue
|
|
57
|
+
const nd = dist[u] - Math.log(link.w)
|
|
58
|
+
// 同样便宜时,优先跳数少的(更好解释)
|
|
59
|
+
if (nd < dist[link.to] - 1e-9 || (Math.abs(nd - dist[link.to]) < 1e-9 && hops[u] + 1 < hops[link.to])) {
|
|
60
|
+
dist[link.to] = nd
|
|
61
|
+
hops[link.to] = hops[u] + 1
|
|
62
|
+
prevNode[link.to] = u
|
|
63
|
+
prevEdge[link.to] = link.e
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!Number.isFinite(dist[toIdx])) return { found: false, ids: [], edges: [], hops: 0, score: 0 }
|
|
69
|
+
const ids = []
|
|
70
|
+
const edges = []
|
|
71
|
+
let cur = toIdx
|
|
72
|
+
while (cur !== -1 && ids.length <= maxHops + 1) {
|
|
73
|
+
ids.push(cur)
|
|
74
|
+
const e = prevEdge[cur]
|
|
75
|
+
if (e) edges.push(e)
|
|
76
|
+
if (cur === fromIdx) break
|
|
77
|
+
cur = prevNode[cur]
|
|
78
|
+
}
|
|
79
|
+
ids.reverse()
|
|
80
|
+
edges.reverse()
|
|
81
|
+
const score = edges.reduce((acc, e) => acc * Math.max(MIN_W, e.w || 0), 1)
|
|
82
|
+
return { found: true, ids, edges, hops: edges.length, score }
|
|
83
|
+
}
|
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 扩散激活检索(spreading activation)—— 星图的「神经元」那一半。
|
|
3
|
+
*
|
|
4
|
+
* 朴素 RAG:query → top-k 记忆。这里不是。
|
|
5
|
+
* 这里:query → 种子节点(直接命中)→ 沿星图上的边衰减扩散 → 二次命中。
|
|
6
|
+
* 于是「项目 A 里记的事」能把「项目 B 里记的另一半」一起带出来,
|
|
7
|
+
* 并且每条带出来的记忆都会附上「为什么带它出来」的证据链。
|
|
8
|
+
*/
|
|
9
|
+
import { tokenize, idf, clip } from './text.js'
|
|
10
|
+
// [macro-fusion 0.4.0 · P4] 实体层:查询里点了名(比如「memory_search 的 budget 怎么算」)
|
|
11
|
+
// 就把那个实体下面的记忆全带上。它只加种子能量,不改 nodes/edges,关掉不留痕。
|
|
12
|
+
import { entitySeedsOf, entitySeedNodes } from './entities.js'
|
|
13
|
+
|
|
14
|
+
/** 扩散衰减系数:一跳打这个折,二跳再打一次。 */
|
|
15
|
+
const HOP_DECAY = 0.58
|
|
16
|
+
/** 最多扩散几跳。 */
|
|
17
|
+
const MAX_HOPS = 2
|
|
18
|
+
/** 每跳最多从多少个高能节点继续传。 */
|
|
19
|
+
const FRONTIER_WIDTH = 110
|
|
20
|
+
/**
|
|
21
|
+
* [macro-fusion 0.4.0 · P4] 动态跳数的门槛:目标节点与查询的词面重合低于这个值,
|
|
22
|
+
* 第二跳就不再走过去。0.2 = 六个词里至少沾一个。
|
|
23
|
+
* 这条门槛只在 hop≥1 生效:第一跳是「直接邻居」,那是星图最基本的语义,不该被砍。
|
|
24
|
+
*/
|
|
25
|
+
const DYNAMIC_MIN_MATCH = 0.2
|
|
26
|
+
/**
|
|
27
|
+
* [macro-fusion 0.4.0 · P4] 硬边:这些边本身就是「同一件事」的证据(人工策展、同一标识、
|
|
28
|
+
* 跨工作区同项目),不需要再看词面。动态跳数只对软边(关键词/语义/共激活)要求匹配——
|
|
29
|
+
* 否则「这条记忆写的是半个句子」会被当成「没关系」,把真正的另一半砍掉。
|
|
30
|
+
*/
|
|
31
|
+
const HARD_EDGE = new Set(['explicit', 'entity', 'project'])
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 归一化一组 memory id:允许传完整 node id,也允许传 meow 的裸 uid。
|
|
35
|
+
* @param {object[]} nodes
|
|
36
|
+
* @param {string[]} ids
|
|
37
|
+
*/
|
|
38
|
+
const TOKEN_CACHE = new WeakMap()
|
|
39
|
+
// 正在算的那一趟(同一个 graph 只允许一趟,见 ensureTokenCacheAsync)
|
|
40
|
+
const TOKEN_INFLIGHT = new WeakMap()
|
|
41
|
+
const ADJ_CACHE = new WeakMap()
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 词频表(生成器):每 6ms 让一次,5 万条上整段约 1.3 秒。
|
|
45
|
+
* 结果挂进 TOKEN_CACHE,同步壳与异步壳共用同一份。
|
|
46
|
+
*/
|
|
47
|
+
function* tokenCacheSteps(graph) {
|
|
48
|
+
const nodes = graph.nodes || []
|
|
49
|
+
const n = nodes.length
|
|
50
|
+
const hit = TOKEN_CACHE.get(graph)
|
|
51
|
+
if (hit && hit.n === n) return hit
|
|
52
|
+
const df = new Map()
|
|
53
|
+
const tokensOf = new Array(n)
|
|
54
|
+
let sliceAt = Date.now()
|
|
55
|
+
for (let i = 0; i < n; i++) {
|
|
56
|
+
const node = nodes[i]
|
|
57
|
+
// buildGraph 产出的节点已经带 tokens(title/正文/项目/关键词),直接复用;
|
|
58
|
+
// 手工构造的图(测试、demo)没有,就现场分一次词。
|
|
59
|
+
const bag = node.tokens instanceof Set ? node.tokens : new Set()
|
|
60
|
+
if (!bag.size) {
|
|
61
|
+
for (const k of node.keywords || []) bag.add(String(k).toLowerCase())
|
|
62
|
+
const body = String(node.content || node.preview || node.label || '')
|
|
63
|
+
for (const t of tokenize(node.title || '')) bag.add(t)
|
|
64
|
+
for (const t of tokenize(body)) bag.add(t)
|
|
65
|
+
for (const t of tokenize(node.project || '')) bag.add(t)
|
|
66
|
+
}
|
|
67
|
+
tokensOf[i] = bag
|
|
68
|
+
for (const t of bag) df.set(t, (df.get(t) || 0) + 1)
|
|
69
|
+
if (Date.now() - sliceAt >= 6) {
|
|
70
|
+
sliceAt = Date.now()
|
|
71
|
+
yield 'tokens'
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const value = { n, df, tokensOf }
|
|
75
|
+
TOKEN_CACHE.set(graph, value)
|
|
76
|
+
return value
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 同步壳:一次跑完。 */
|
|
80
|
+
/**
|
|
81
|
+
* 这张图的词频表算好了吗。
|
|
82
|
+
*
|
|
83
|
+
* 只给单测与探针用:预热是后台行为,界面上无从观察,测试得能问一句「好了没」。
|
|
84
|
+
* @param {object} graph
|
|
85
|
+
*/
|
|
86
|
+
export function tokenCacheReady(graph) {
|
|
87
|
+
return TOKEN_CACHE.has(graph)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function ensureTokenCache(graph) {
|
|
91
|
+
const it = tokenCacheSteps(graph)
|
|
92
|
+
let r = it.next()
|
|
93
|
+
while (!r.done) r = it.next()
|
|
94
|
+
return r.value
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 异步壳:每次 yield 后 setImmediate,事件循环不被按住。
|
|
99
|
+
*
|
|
100
|
+
* 正在算的时候再有人要(后台预热还没算完,用户就先搜了),**共用同一趟**:
|
|
101
|
+
* 以前这里每叫一次就从头再跑一遍,于是「预热正在进行 + 第一次搜索」是两份全量分词在抢 CPU,
|
|
102
|
+
* 五万条要 3.5 秒(1.11.33 修:单趟 1.3 秒、两趟互相拖到 3.5 秒)。
|
|
103
|
+
*/
|
|
104
|
+
export function ensureTokenCacheAsync(graph) {
|
|
105
|
+
const done = TOKEN_CACHE.get(graph)
|
|
106
|
+
if (done) return Promise.resolve(done)
|
|
107
|
+
const running = TOKEN_INFLIGHT.get(graph)
|
|
108
|
+
if (running) return running
|
|
109
|
+
const p = (async () => {
|
|
110
|
+
const it = tokenCacheSteps(graph)
|
|
111
|
+
let r = it.next()
|
|
112
|
+
while (!r.done) {
|
|
113
|
+
await new Promise((resolve) => setImmediate(resolve))
|
|
114
|
+
r = it.next()
|
|
115
|
+
}
|
|
116
|
+
return r.value
|
|
117
|
+
})()
|
|
118
|
+
TOKEN_INFLIGHT.set(graph, p)
|
|
119
|
+
// 注意这里是普通函数、不是 async:async 会给每个调用包一层新 promise,
|
|
120
|
+
// 那样「两处同时要」虽然只算一遍,却拿到两个不同的 promise(单测就抓到了这一点)。
|
|
121
|
+
const clear = () => {
|
|
122
|
+
if (TOKEN_INFLIGHT.get(graph) === p) TOKEN_INFLIGHT.delete(graph)
|
|
123
|
+
}
|
|
124
|
+
p.then(clear, clear)
|
|
125
|
+
return p
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 异步检索:先把词频表分片灌满,再走同步逻辑。
|
|
130
|
+
* 第一次查 5 万条的库时,这一步原本会整段按住 1.3 秒。
|
|
131
|
+
*/
|
|
132
|
+
export async function recallAsync(graph, query, opts = {}) {
|
|
133
|
+
await ensureTokenCacheAsync(graph)
|
|
134
|
+
return recall(graph, query, opts)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function resolveIds(nodes, ids) {
|
|
138
|
+
const out = []
|
|
139
|
+
const byId = new Map()
|
|
140
|
+
const byUid = new Map()
|
|
141
|
+
for (const node of nodes) {
|
|
142
|
+
byId.set(node.id, node)
|
|
143
|
+
const list = byUid.get(node.id.split(':').pop())
|
|
144
|
+
if (list) list.push(node)
|
|
145
|
+
else byUid.set(node.id.split(':').pop(), [node])
|
|
146
|
+
}
|
|
147
|
+
for (const raw of ids || []) {
|
|
148
|
+
const key = String(raw || '').trim()
|
|
149
|
+
if (!key) continue
|
|
150
|
+
if (byId.has(key)) {
|
|
151
|
+
out.push(byId.get(key))
|
|
152
|
+
continue
|
|
153
|
+
}
|
|
154
|
+
const list = byUid.get(key)
|
|
155
|
+
if (list && list.length === 1) out.push(list[0])
|
|
156
|
+
else if (list && list.length > 1) out.push(list[0]) // 同一 id 多库时取第一个,够用
|
|
157
|
+
}
|
|
158
|
+
return out
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 文本打分的字段权重:标题/关键词命中比正文命中更有指向性。
|
|
163
|
+
*/
|
|
164
|
+
const FIELD_WEIGHT = { title: 3, keyword: 2.6, project: 1.8, entity: 2.2, content: 1 }
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 扩散激活检索。
|
|
168
|
+
*
|
|
169
|
+
* @param {object} graph buildGraph 的产物
|
|
170
|
+
* @param {string} query
|
|
171
|
+
* @param {object} [opts]
|
|
172
|
+
* @param {number} [opts.limit=12]
|
|
173
|
+
* @param {string[]} [opts.seedIds] 外部工具返回的记忆 id(会当成满能种子)
|
|
174
|
+
* @param {number} [opts.hops=2]
|
|
175
|
+
* @param {boolean} [opts.crossWorkspace=true]
|
|
176
|
+
* @param {string[]} [opts.exclude] 需要排除的 id
|
|
177
|
+
*/
|
|
178
|
+
export function recall(graph, query, opts = {}) {
|
|
179
|
+
const nodes = graph.nodes || []
|
|
180
|
+
const edges = graph.edges || []
|
|
181
|
+
const limit = Math.max(1, Math.min(50, opts.limit || 12))
|
|
182
|
+
const hops = Math.max(0, Math.min(3, opts.hops ?? MAX_HOPS))
|
|
183
|
+
const crossWorkspace = opts.crossWorkspace !== false
|
|
184
|
+
const exclude = new Set(opts.exclude || [])
|
|
185
|
+
// [macro-fusion 0.2.9] 归档/删除的记忆不该再作为检索结果出现。
|
|
186
|
+
// 与 bridge.js 的 statusFactor(active 1 / stale 0.7 / archived 0)同一条口径:
|
|
187
|
+
// 归档等于「用户说这条不用了」,但它仍然可以被 hygiene、nebula_inspect 按 id 看到,
|
|
188
|
+
// 也仍然能在扩散时当中转(见 §4),只是不许自己占一个结果位。
|
|
189
|
+
const includeInactive = opts.includeInactive === true
|
|
190
|
+
/** 这条记忆能不能占一个检索结果位/当种子。 */
|
|
191
|
+
const selectable = (node) => {
|
|
192
|
+
if (includeInactive) return true
|
|
193
|
+
const st = String(node.status ?? 'active').toLowerCase()
|
|
194
|
+
return st !== 'archived' && st !== 'deleted'
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const n = nodes.length
|
|
198
|
+
const energy = new Float64Array(n)
|
|
199
|
+
const seedE = new Float64Array(n)
|
|
200
|
+
/** @type {(null|{from:number,type:string,w:number})[]} */
|
|
201
|
+
const via = new Array(n).fill(null)
|
|
202
|
+
const matchedTerms = new Array(n).fill(null)
|
|
203
|
+
|
|
204
|
+
// 词频表只跟这张图有关:同一张图会被连续查很多次,
|
|
205
|
+
// 每次重新分词/建邻接等于把 1 万条记忆重读一遍。用 WeakMap 挂在图上,
|
|
206
|
+
// 图重建(新对象)时自然失效,不会读到上一版的数据。
|
|
207
|
+
// 5 万条上这一次要 1.3 秒,所以它也是分片的:异步壳先把它灌满再查。
|
|
208
|
+
const { df, tokensOf } = ensureTokenCache(graph)
|
|
209
|
+
const weightOf = (t) => idf(df.get(t) || 1, Math.max(2, n))
|
|
210
|
+
|
|
211
|
+
// ---------- 1. 文本种子 ----------
|
|
212
|
+
const qTokens = tokenize(query || '')
|
|
213
|
+
let unknownTerms = []
|
|
214
|
+
if (qTokens.size > 0) {
|
|
215
|
+
// 语料里根本没有的词(比如刚起名的项目)不参与覆盖率分母,
|
|
216
|
+
// 否则一个「谁都没提过」的词会把整条查询的命中率压成 0。
|
|
217
|
+
const qList = [...qTokens].filter((t) => (df.get(t) || 0) > 0)
|
|
218
|
+
// «短语» 这种合成 token 只用于标题整串加权,不该出现在「没见过的词」里
|
|
219
|
+
const unknownTermsLocal = [...qTokens].filter((t) => !t.startsWith('«') && (df.get(t) || 0) === 0)
|
|
220
|
+
unknownTerms = unknownTermsLocal
|
|
221
|
+
const knownRatio = qTokens.size > 0 ? qList.length / qTokens.size : 0
|
|
222
|
+
const totalWeight = qList.reduce((sum, t) => sum + weightOf(t), 0) || 1
|
|
223
|
+
const qLower = String(query || '').toLowerCase().trim()
|
|
224
|
+
/** @type {Array<{i:number,coverage:number,score:number,matched:string[]}>} */
|
|
225
|
+
const cands = []
|
|
226
|
+
for (let i = 0; i < n; i++) {
|
|
227
|
+
const node = nodes[i]
|
|
228
|
+
if (!selectable(node)) continue // [macro-fusion 0.2.9] 归档的不当种子
|
|
229
|
+
const bag = tokensOf[i]
|
|
230
|
+
const matched = []
|
|
231
|
+
let matchedWeight = 0
|
|
232
|
+
for (const t of qList) {
|
|
233
|
+
if (!bag.has(t)) continue
|
|
234
|
+
matchedWeight += weightOf(t)
|
|
235
|
+
matched.push(t)
|
|
236
|
+
}
|
|
237
|
+
if (matched.length === 0) continue
|
|
238
|
+
const coverage = matchedWeight / totalWeight
|
|
239
|
+
// 覆盖率:只命中查询里的一个小词,不该被当成强种子
|
|
240
|
+
let score = Math.pow(coverage, 1.35) * (0.55 + 0.45 * knownRatio)
|
|
241
|
+
const titleText = String(node.title || node.label || '').toLowerCase()
|
|
242
|
+
if (qLower.length >= 3 && titleText.includes(qLower)) score += 0.22
|
|
243
|
+
// 稀有词恰好落在 keywords 里,是强提示
|
|
244
|
+
for (const k of node.keywords || []) {
|
|
245
|
+
if (qTokens.has(String(k).toLowerCase())) {
|
|
246
|
+
score += 0.12
|
|
247
|
+
break
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
cands.push({ i, coverage, score: Math.min(1, score), matched })
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// 阈值分级:先严后松,保证「查得到」但不让一个通用词污染整张网。
|
|
254
|
+
const tiers = qList.length <= 3 ? [0.5, 0.34, 0.2] : [0.32, 0.22, 0.12]
|
|
255
|
+
let chosen = []
|
|
256
|
+
for (const tier of tiers) {
|
|
257
|
+
chosen = cands.filter((c) => c.coverage >= tier)
|
|
258
|
+
if (chosen.length >= 3) break
|
|
259
|
+
}
|
|
260
|
+
if (chosen.length === 0) chosen = cands.slice()
|
|
261
|
+
chosen.sort((a, b) => b.score - a.score)
|
|
262
|
+
chosen = chosen.slice(0, 80)
|
|
263
|
+
for (const c of chosen) {
|
|
264
|
+
seedE[c.i] = c.score
|
|
265
|
+
energy[c.i] = c.score
|
|
266
|
+
matchedTerms[c.i] = c.matched.slice(0, 6)
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// ---------- 2. 外部种子(别的记忆工具刚查出来的) ----------
|
|
271
|
+
const seedNodes = resolveIds(nodes, opts.seedIds || [])
|
|
272
|
+
for (const node of seedNodes) {
|
|
273
|
+
const i = node.i
|
|
274
|
+
seedE[i] = Math.max(seedE[i], 1)
|
|
275
|
+
energy[i] = Math.max(energy[i], 1)
|
|
276
|
+
via[i] = via[i] || { from: -1, type: 'seed', w: 1 }
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// ---------- 2.5 来源节点当种子 ----------
|
|
280
|
+
// 查「meow-memory」这种名字时,最该亮起来的是那个来源节点本身,
|
|
281
|
+
// 再由它的 mentions 边把「用过它的记忆」全部带出来。
|
|
282
|
+
const qNorm = String(query || '').trim().toLowerCase()
|
|
283
|
+
if (qNorm) {
|
|
284
|
+
for (let i = 0; i < n; i++) {
|
|
285
|
+
const node = nodes[i]
|
|
286
|
+
if (node.level !== 'source') continue
|
|
287
|
+
const title = String(node.title || node.label || '').toLowerCase()
|
|
288
|
+
if (!title) continue
|
|
289
|
+
const exact = title === qNorm
|
|
290
|
+
const inside = !exact && qNorm.includes(title) && title.length >= 4
|
|
291
|
+
if (!exact && !inside) continue
|
|
292
|
+
const e = exact ? 1 : 0.72
|
|
293
|
+
if (e > seedE[i]) {
|
|
294
|
+
seedE[i] = e
|
|
295
|
+
energy[i] = Math.max(energy[i], e)
|
|
296
|
+
via[i] = via[i] || { from: -1, type: 'seed', w: 1 }
|
|
297
|
+
}
|
|
298
|
+
// 文本种子也可能已经命中它(标题里的词),这里保证它出现在 seeds 列表里,
|
|
299
|
+
// 调用方才知道「这次是从哪个来源炸开的」
|
|
300
|
+
if (seedNodes.indexOf(node) === -1) seedNodes.push(node)
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// ---------- 2.6 实体种子(P4) ----------
|
|
305
|
+
// 「关于 memory_search 的一切」这种问法,词面命中只能捞到正文里恰好写了全名的那几条;
|
|
306
|
+
// 实体层知道哪些记忆共享同一个工具名/路径/项目/版本,一次就能把另一半带上来。
|
|
307
|
+
// 能量比文本种子低(ENTITY_SEED = 0.6):实体是「同一件事」的证据,但不如「正文直接命中」硬。
|
|
308
|
+
const entityOn = opts.entityLayer !== false && qNorm.length > 0
|
|
309
|
+
const entityHits = entityOn ? entitySeedsOf(query, graph.entityLayer, { limit: 8 }) : []
|
|
310
|
+
const entityPairs = entityHits.length ? entitySeedNodes(entityHits, graph.entityLayer) : []
|
|
311
|
+
/** 实体成员集合:动态跳数用它给「同一实体的邻居」放行。 */
|
|
312
|
+
const entityMember = new Set()
|
|
313
|
+
for (const s of entityPairs) {
|
|
314
|
+
if (!(s.i >= 0 && s.i < n)) continue
|
|
315
|
+
if (entityMember.has(s.i)) continue
|
|
316
|
+
const node = nodes[s.i]
|
|
317
|
+
if (!selectable(node)) continue
|
|
318
|
+
entityMember.add(s.i)
|
|
319
|
+
if (s.energy > seedE[s.i]) {
|
|
320
|
+
seedE[s.i] = s.energy
|
|
321
|
+
energy[s.i] = Math.max(energy[s.i], s.energy)
|
|
322
|
+
via[s.i] = via[s.i] || { from: -1, type: 'entity', w: Math.round(s.energy * 1000) / 1000 }
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// ---------- 3. 邻接表 ----------
|
|
327
|
+
let adjCached = ADJ_CACHE.get(graph)
|
|
328
|
+
if (!adjCached || adjCached.n !== n || adjCached.m !== edges.length) {
|
|
329
|
+
const adj = new Array(n)
|
|
330
|
+
for (let i = 0; i < n; i++) adj[i] = []
|
|
331
|
+
for (let e = 0; e < edges.length; e++) {
|
|
332
|
+
const edge = edges[e]
|
|
333
|
+
adj[edge.s].push({ to: edge.t, w: edge.w, type: edge.type, e })
|
|
334
|
+
adj[edge.t].push({ to: edge.s, w: edge.w, type: edge.type, e })
|
|
335
|
+
}
|
|
336
|
+
adjCached = { n, m: edges.length, adj }
|
|
337
|
+
ADJ_CACHE.set(graph, adjCached)
|
|
338
|
+
}
|
|
339
|
+
const { adj } = adjCached
|
|
340
|
+
|
|
341
|
+
// ---------- 4. 扩散 ----------
|
|
342
|
+
const propagate = new Float64Array(n)
|
|
343
|
+
// [macro-fusion 0.4.0 · P4] 动态跳数:不再「跳满 hops 为止」,而是每跳先问
|
|
344
|
+
// 「这个邻居与本次查询还有没有关系」。没有就停——省下的不只是预算,
|
|
345
|
+
// 更是不让第二跳把同项目里那些只因为「同一个工作区」而被连上的记忆一起点亮。
|
|
346
|
+
const dynamicHops = opts.dynamicHops !== false && qTokens.size > 0
|
|
347
|
+
const qCap = Math.max(1, Math.min(qTokens.size, 6))
|
|
348
|
+
const matchOf = (j) => {
|
|
349
|
+
if (entityMember.has(j)) return 1
|
|
350
|
+
const bag = tokensOf[j]
|
|
351
|
+
if (!bag) return 0
|
|
352
|
+
let hit = 0
|
|
353
|
+
for (const t of qTokens) if (bag.has(t)) hit++
|
|
354
|
+
return hit / qCap
|
|
355
|
+
}
|
|
356
|
+
let hopsUsed = 0
|
|
357
|
+
let hopsStopped = ''
|
|
358
|
+
let skipped = 0
|
|
359
|
+
let frontier = []
|
|
360
|
+
for (let i = 0; i < n; i++) if (energy[i] > 0) frontier.push(i)
|
|
361
|
+
frontier.sort((a, b) => energy[b] - energy[a])
|
|
362
|
+
frontier = frontier.slice(0, FRONTIER_WIDTH)
|
|
363
|
+
|
|
364
|
+
for (let hop = 0; hop < hops; hop++) {
|
|
365
|
+
const next = new Map()
|
|
366
|
+
for (const from of frontier) {
|
|
367
|
+
const src = nodes[from]
|
|
368
|
+
// [macro-fusion 0.4.0 · P4] 第一跳用种子能量,第二跳起用上一跳给它的那份能量。
|
|
369
|
+
// 原来这里一律读 energy[],而 energy[] 只在种子里写过——于是第二跳每个节点的 e0 都是 0,
|
|
370
|
+
// 被下面那道 `< 0.06` 直接跳过,next 恒空、循环立刻 break。
|
|
371
|
+
// 也就是说 `hops: 2` 从来没有真的走过第二跳,MAX_HOPS 与「二跳再打一次」的注释都是空的。
|
|
372
|
+
const e0 = hop === 0 ? energy[from] : propagate[from] || 0
|
|
373
|
+
if (e0 < 0.06) continue
|
|
374
|
+
// 来源节点是「标签」不是「记忆」:能量落在它身上是对的(能看出这条记忆用过什么),
|
|
375
|
+
// 但不能让它当一个超级中转站,把 20 多条记忆一视同仁地全点亮。
|
|
376
|
+
const fromDamp = src.level === 'source' ? 0.5 : 1
|
|
377
|
+
for (const link of adj[from]) {
|
|
378
|
+
const target = nodes[link.to]
|
|
379
|
+
if (!crossWorkspace && target.ws !== src.ws) continue
|
|
380
|
+
const damp = target.level === 'source' ? 0.55 : 1
|
|
381
|
+
let gain = e0 * link.w * HOP_DECAY * fromDamp * damp
|
|
382
|
+
if (dynamicHops && !HARD_EDGE.has(link.type)) {
|
|
383
|
+
const m = matchOf(link.to)
|
|
384
|
+
// 第二跳起:与查询毫无词面关系的邻居不再走过去(第一跳不受限,见 DYNAMIC_MIN_MATCH)
|
|
385
|
+
if (hop >= 1 && m < DYNAMIC_MIN_MATCH) {
|
|
386
|
+
skipped++
|
|
387
|
+
if (!hopsStopped) hopsStopped = `跳 ${hop + 1}`
|
|
388
|
+
continue
|
|
389
|
+
}
|
|
390
|
+
// 有关系也不是全给:词面各占一半,剩下的看关系本身
|
|
391
|
+
gain *= 0.45 + 0.55 * m
|
|
392
|
+
}
|
|
393
|
+
if (gain < 0.02) continue
|
|
394
|
+
const prev = next.get(link.to) || 0
|
|
395
|
+
if (gain > prev + 1e-6) {
|
|
396
|
+
next.set(link.to, gain)
|
|
397
|
+
if (!via[link.to] || gain > (propagate[link.to] || 0)) {
|
|
398
|
+
via[link.to] = { from, type: link.type, w: link.w }
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (propagate[link.to] < gain) propagate[link.to] = gain
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
if (next.size === 0) break
|
|
405
|
+
hopsUsed = hop + 1
|
|
406
|
+
frontier = [...next.entries()].sort((a, b) => b[1] - a[1]).slice(0, FRONTIER_WIDTH).map((x) => x[0])
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// ---------- 5. 合成 ----------
|
|
410
|
+
// 跨工作区的桥:同一个项目在不同工作区的记忆,正是「检索 A 连带读出 B」要的东西,
|
|
411
|
+
// 所以给它们一点额外能量,别被本地的强邻居全挤掉。
|
|
412
|
+
const seedWs = new Set()
|
|
413
|
+
for (let i = 0; i < n; i++) if (seedE[i] > 0) seedWs.add(nodes[i].ws)
|
|
414
|
+
// 主工作区 = 最强的那颗种子所在的项目。结果里凡是跑到别的项目去的,
|
|
415
|
+
// 都算「跨工作区」,这样用户一眼能看出「这条不是我这个项目的记忆」。
|
|
416
|
+
let primaryWs = ''
|
|
417
|
+
let bestSeed = 0
|
|
418
|
+
for (let i = 0; i < n; i++) {
|
|
419
|
+
if (nodes[i].ws && seedE[i] > bestSeed) {
|
|
420
|
+
bestSeed = seedE[i]
|
|
421
|
+
primaryWs = nodes[i].ws
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
const scored = []
|
|
425
|
+
for (let i = 0; i < n; i++) {
|
|
426
|
+
if (nodes[i].id && exclude.has(nodes[i].id)) continue
|
|
427
|
+
// [macro-fusion 0.2.9] 归档/删除的状态过滤(同 bridge.js statusFactor)
|
|
428
|
+
if (!selectable(nodes[i])) continue
|
|
429
|
+
const direct = seedE[i]
|
|
430
|
+
const spread = propagate[i]
|
|
431
|
+
if (direct <= 0 && spread <= 0) continue
|
|
432
|
+
let total = direct + spread * 0.62
|
|
433
|
+
if (total < 0.05) continue
|
|
434
|
+
// 能量加成仍然只给「种子集合之外的工作区」,避免同一项目里重复计数
|
|
435
|
+
const outsideSeeds = crossWorkspace && seedWs.size > 0 && !seedWs.has(nodes[i].ws)
|
|
436
|
+
if (outsideSeeds) total *= 1.22
|
|
437
|
+
const across = Boolean(primaryWs) && nodes[i].ws !== primaryWs
|
|
438
|
+
scored.push({ i, total, direct, spread, across })
|
|
439
|
+
}
|
|
440
|
+
scored.sort((a, b) => b.total - a.total)
|
|
441
|
+
|
|
442
|
+
// 保证结果里至少出现 2 条跨工作区的「另一半」,否则连带读取就名不副实。
|
|
443
|
+
const picked = scored.slice(0, limit)
|
|
444
|
+
if (crossWorkspace && seedWs.size > 0) {
|
|
445
|
+
const have = picked.filter((x) => x.across).length
|
|
446
|
+
if (have < 2) {
|
|
447
|
+
for (const cand of scored) {
|
|
448
|
+
if (picked.length >= limit) break
|
|
449
|
+
if (!cand.across || picked.includes(cand)) continue
|
|
450
|
+
picked.push(cand)
|
|
451
|
+
if (picked.filter((x) => x.across).length >= 2) break
|
|
452
|
+
}
|
|
453
|
+
picked.sort((a, b) => b.total - a.total)
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const hits = picked.map(({ i, total, direct, spread, across }) => {
|
|
458
|
+
const node = nodes[i]
|
|
459
|
+
const link = via[i]
|
|
460
|
+
const chain = []
|
|
461
|
+
if (link && link.from >= 0) {
|
|
462
|
+
const fromNode = nodes[link.from]
|
|
463
|
+
chain.push({
|
|
464
|
+
fromId: fromNode.id,
|
|
465
|
+
fromLabel: fromNode.label || fromNode.title,
|
|
466
|
+
type: link.type,
|
|
467
|
+
weight: link.w,
|
|
468
|
+
})
|
|
469
|
+
}
|
|
470
|
+
return {
|
|
471
|
+
id: node.id,
|
|
472
|
+
uid: node.id.split(':').pop(),
|
|
473
|
+
ws: node.wsTitle,
|
|
474
|
+
level: node.level,
|
|
475
|
+
label: node.label,
|
|
476
|
+
title: node.title,
|
|
477
|
+
preview: clip(node.preview, 220),
|
|
478
|
+
importance: node.importance,
|
|
479
|
+
project: node.project,
|
|
480
|
+
status: node.status,
|
|
481
|
+
keywords: (node.keywords || []).slice(0, 8),
|
|
482
|
+
score: Math.round(total * 1000) / 1000,
|
|
483
|
+
direct: Math.round(direct * 1000) / 1000,
|
|
484
|
+
spread: Math.round(spread * 1000) / 1000,
|
|
485
|
+
why:
|
|
486
|
+
direct > 0.15
|
|
487
|
+
? across
|
|
488
|
+
? '直接命中查询词(跨工作区)'
|
|
489
|
+
: '直接命中查询词'
|
|
490
|
+
: link
|
|
491
|
+
? `经由「${clip(nodes[link.from].label || nodes[link.from].title, 18)}」的${link.type}关联${across ? '跨工作区' : ''}扩散而来`
|
|
492
|
+
: '关联扩散',
|
|
493
|
+
across,
|
|
494
|
+
chain,
|
|
495
|
+
}
|
|
496
|
+
})
|
|
497
|
+
|
|
498
|
+
return {
|
|
499
|
+
query: query || '',
|
|
500
|
+
seeds: seedNodes.map((x) => ({ id: x.id, label: x.label })),
|
|
501
|
+
unknown: unknownTerms,
|
|
502
|
+
hits,
|
|
503
|
+
scanned: scored.length,
|
|
504
|
+
// [macro-fusion 0.4.0 · P4] 三个数一起报:命中集、平均跳数、被砍掉的串味邻居。
|
|
505
|
+
// 只看「省了预算」不算数——跳数下降如果伴随命中集下降,那是变笨不是变省。
|
|
506
|
+
hops: { asked: hops, used: hopsUsed, stoppedAt: hopsStopped || null, skippedNeighbors: skipped, dynamic: dynamicHops },
|
|
507
|
+
entities: entityHits.map((x) => ({ label: x.label, type: x.type, members: x.members })),
|
|
508
|
+
// [macro-fusion 0.2.39] 图里一共多少节点:空命中要能分清「图是空的」和「图里真没有」。
|
|
509
|
+
// scanned 是「算进候选的条数」,查询词一个都对不上时它本来就是 0——拿它当凭据会把两种空命中混成一种。
|
|
510
|
+
nodes: n,
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* 给外挂工具用:从一批 id 出发,返回「连着它们的记忆」。
|
|
516
|
+
* @param {object} graph
|
|
517
|
+
* @param {string[]} ids
|
|
518
|
+
* @param {number} [limit]
|
|
519
|
+
*/
|
|
520
|
+
export function neighborsOf(graph, ids, limit = 20) {
|
|
521
|
+
const base = resolveIds(graph.nodes || [], ids)
|
|
522
|
+
if (base.length === 0) return { seeds: [], hits: [] }
|
|
523
|
+
return recall(graph, '', { seedIds: base.map((n) => n.id), limit, hops: 2 })
|
|
524
|
+
}
|