@memory-river/core 0.2.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 +201 -0
- package/README.md +222 -0
- package/README.zh-TW.md +186 -0
- package/dist/api.d.ts +100 -0
- package/dist/api.js +156 -0
- package/dist/cognition/causal-attribution.d.ts +36 -0
- package/dist/cognition/causal-attribution.js +239 -0
- package/dist/cognition/causal-engine.d.ts +105 -0
- package/dist/cognition/causal-engine.js +150 -0
- package/dist/cognition/conflict-detector.d.ts +39 -0
- package/dist/cognition/conflict-detector.js +193 -0
- package/dist/cognition/global-working-memory.d.ts +53 -0
- package/dist/cognition/global-working-memory.js +211 -0
- package/dist/cognition/hooks-engine.d.ts +99 -0
- package/dist/cognition/hooks-engine.js +672 -0
- package/dist/cognition/ralph-core.d.ts +28 -0
- package/dist/cognition/ralph-core.js +104 -0
- package/dist/distill/concentrator-adapter.d.ts +167 -0
- package/dist/distill/concentrator-adapter.js +1876 -0
- package/dist/engine.d.ts +402 -0
- package/dist/engine.js +2254 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +3 -0
- package/dist/lifecycle/cleanup-engine.d.ts +80 -0
- package/dist/lifecycle/cleanup-engine.js +162 -0
- package/dist/lifecycle/cleanup-state.d.ts +34 -0
- package/dist/lifecycle/cleanup-state.js +50 -0
- package/dist/lifecycle/night-consolidation.d.ts +102 -0
- package/dist/lifecycle/night-consolidation.js +640 -0
- package/dist/lifecycle/night-recovery.d.ts +40 -0
- package/dist/lifecycle/night-recovery.js +107 -0
- package/dist/paths.d.ts +17 -0
- package/dist/paths.js +16 -0
- package/dist/pipeline/capsule-bridge.d.ts +35 -0
- package/dist/pipeline/capsule-bridge.js +86 -0
- package/dist/pipeline/compact-request.d.ts +30 -0
- package/dist/pipeline/compact-request.js +66 -0
- package/dist/pipeline/inbox-watcher.d.ts +112 -0
- package/dist/pipeline/inbox-watcher.js +1039 -0
- package/dist/ports.d.ts +29 -0
- package/dist/ports.js +1 -0
- package/dist/providers/embedder-v5.d.ts +46 -0
- package/dist/providers/embedder-v5.js +155 -0
- package/dist/providers/ollama-embedding.d.ts +25 -0
- package/dist/providers/ollama-embedding.js +166 -0
- package/dist/retrieval/abstractness-judge.d.ts +14 -0
- package/dist/retrieval/abstractness-judge.js +87 -0
- package/dist/retrieval/coverage-selection.d.ts +3 -0
- package/dist/retrieval/coverage-selection.js +53 -0
- package/dist/retrieval/cross-encoder-gate.d.ts +40 -0
- package/dist/retrieval/cross-encoder-gate.js +239 -0
- package/dist/retrieval/retriever-v4.d.ts +78 -0
- package/dist/retrieval/retriever-v4.js +1200 -0
- package/dist/skills/validate.d.ts +6 -0
- package/dist/skills/validate.js +69 -0
- package/dist/storage.d.ts +19 -0
- package/dist/storage.js +54 -0
- package/dist/store/aux-table-maintenance.d.ts +5 -0
- package/dist/store/aux-table-maintenance.js +64 -0
- package/dist/store/graph-enumerator.d.ts +21 -0
- package/dist/store/graph-enumerator.js +185 -0
- package/dist/store/graph-store.d.ts +107 -0
- package/dist/store/graph-store.js +478 -0
- package/dist/store/status-manager.d.ts +44 -0
- package/dist/store/status-manager.js +235 -0
- package/dist/store/store-v4.d.ts +339 -0
- package/dist/store/store-v4.js +2871 -0
- package/dist/transcript/keyword-search.d.ts +9 -0
- package/dist/transcript/keyword-search.js +67 -0
- package/dist/transcript/rehydrate-keyword.d.ts +6 -0
- package/dist/transcript/rehydrate-keyword.js +29 -0
- package/dist/transcript/rehydrate.d.ts +33 -0
- package/dist/transcript/rehydrate.js +285 -0
- package/dist/transcript/transcript-archive.d.ts +46 -0
- package/dist/transcript/transcript-archive.js +516 -0
- package/dist/types.d.ts +409 -0
- package/dist/types.js +104 -0
- package/dist/util/bounded-map.d.ts +1 -0
- package/dist/util/bounded-map.js +8 -0
- package/dist/util/rate-limiter.d.ts +12 -0
- package/dist/util/rate-limiter.js +54 -0
- package/dist/util/session-identity.d.ts +65 -0
- package/dist/util/session-identity.js +227 -0
- package/dist/util/util-hash.d.ts +1 -0
- package/dist/util/util-hash.js +4 -0
- package/package.json +59 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hooks Engine - 記憶鉤子系統 + 實體關係圖譜
|
|
3
|
+
* memory-lance-v4
|
|
4
|
+
* * 功能:
|
|
5
|
+
* 1. LLM 自動生成 hooks(最多 3 個,含權重)
|
|
6
|
+
* 2. LLM 自動生成 entities(實體關係圖譜)
|
|
7
|
+
* 3. 搜尋時觸發鉤子(層級 1,權重 > 0.5)
|
|
8
|
+
* 4. 智慧防呆:可接受字串、陣列或 JSON 格式的 Query
|
|
9
|
+
* 5. 冷卻時間(1 小時)
|
|
10
|
+
*/
|
|
11
|
+
import { MemoryStore, type MemoryEntry } from "../store/store-v4.js";
|
|
12
|
+
import { Embedder } from "../providers/embedder-v5.js";
|
|
13
|
+
import type { MemoryHook } from "../types.js";
|
|
14
|
+
import type { LlmClient } from "../ports.js";
|
|
15
|
+
import * as lancedb from '@lancedb/lancedb';
|
|
16
|
+
export interface EntityRelation {
|
|
17
|
+
subject: string;
|
|
18
|
+
relation: string;
|
|
19
|
+
object: string;
|
|
20
|
+
}
|
|
21
|
+
export interface HookTriggerResult {
|
|
22
|
+
triggered: boolean;
|
|
23
|
+
relatedMemories: {
|
|
24
|
+
memory: MemoryEntry;
|
|
25
|
+
score: number;
|
|
26
|
+
viaHook: string;
|
|
27
|
+
}[];
|
|
28
|
+
naturalLanguage: string;
|
|
29
|
+
}
|
|
30
|
+
export interface HookQualityRecord {
|
|
31
|
+
keyword: string;
|
|
32
|
+
triggerCount: number;
|
|
33
|
+
successCount: number;
|
|
34
|
+
failCount: number;
|
|
35
|
+
lastTriggeredAt: number | null;
|
|
36
|
+
currentNumericWeight: number;
|
|
37
|
+
adjustedNumericWeight: number;
|
|
38
|
+
}
|
|
39
|
+
export declare class HooksEngine {
|
|
40
|
+
private store;
|
|
41
|
+
private embedder;
|
|
42
|
+
private config;
|
|
43
|
+
private llm?;
|
|
44
|
+
private _db?;
|
|
45
|
+
private cooldown;
|
|
46
|
+
private qualityTracker;
|
|
47
|
+
private hookEmbeddingCache;
|
|
48
|
+
private readonly QUALITY_WINDOW;
|
|
49
|
+
private readonly MAX_QUALITY_TRACKER_SIZE;
|
|
50
|
+
private readonly SUCCESS_RATE_THRESHOLD;
|
|
51
|
+
private readonly HIGH_SUCCESS_RATE;
|
|
52
|
+
private readonly DEGRADE_COOLDOWN;
|
|
53
|
+
private graphStore;
|
|
54
|
+
private debounceTimer?;
|
|
55
|
+
private hardCapTimer?;
|
|
56
|
+
private isDirty;
|
|
57
|
+
constructor(store: MemoryStore, embedder: Embedder, config: {
|
|
58
|
+
enabled?: boolean;
|
|
59
|
+
maxHooksPerMemory?: number;
|
|
60
|
+
maxTriggerDepth?: number;
|
|
61
|
+
minTriggerScore?: number;
|
|
62
|
+
cooldownMs?: number;
|
|
63
|
+
}, llm?: LlmClient | undefined, _db?: lancedb.Connection | undefined);
|
|
64
|
+
/** 注入 GraphStore(由 index.ts 在初始化時呼叫) */
|
|
65
|
+
setGraphStore(graphStore: any): void;
|
|
66
|
+
generateHooks(text: string, category: string): Promise<MemoryHook[]>;
|
|
67
|
+
private getCategoryHint;
|
|
68
|
+
private buildHookPrompt;
|
|
69
|
+
generateEntities(text: string): Promise<EntityRelation[]>;
|
|
70
|
+
private parseEntitiesResponse;
|
|
71
|
+
private parseHooksResponse;
|
|
72
|
+
private fallbackHooks;
|
|
73
|
+
private normalizeWeight;
|
|
74
|
+
private weightToScore;
|
|
75
|
+
private isInCooldown;
|
|
76
|
+
private getAdjustedWeight;
|
|
77
|
+
private setCooldown;
|
|
78
|
+
/**
|
|
79
|
+
* 🛡️ 智慧型 Query 解析器
|
|
80
|
+
* 處理 OpenClaw 傳入的複雜格式,精準提取純文字
|
|
81
|
+
*/
|
|
82
|
+
private safelyExtractUserText;
|
|
83
|
+
triggerHooks(rawQuery: any): Promise<HookTriggerResult>;
|
|
84
|
+
/**
|
|
85
|
+
* CRAG 評估後回報 hook 觸發結果(由 Retriever 呼叫)
|
|
86
|
+
* @param keyword - 觸發的 hook keyword
|
|
87
|
+
* @param wasRetained - CRAG 評估後該記憶是否被保留
|
|
88
|
+
*/
|
|
89
|
+
reportHookOutcome(keyword: string, wasRetained: boolean): Promise<void>;
|
|
90
|
+
private _deleteDBRecord;
|
|
91
|
+
private extractKeywords;
|
|
92
|
+
private generateNaturalLanguage;
|
|
93
|
+
private _setupDebounce;
|
|
94
|
+
private _registerShutdown;
|
|
95
|
+
private schedulePersist;
|
|
96
|
+
flush(): Promise<void>;
|
|
97
|
+
loadStats(): Promise<void>;
|
|
98
|
+
persistStats(): Promise<void>;
|
|
99
|
+
}
|