@linxiraos/pi-mnemopi 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.
Files changed (135) hide show
  1. package/CHANGELOG.md +248 -0
  2. package/README.md +108 -0
  3. package/dist/types/cli.d.ts +35 -0
  4. package/dist/types/config.d.ts +108 -0
  5. package/dist/types/core/aaak.d.ts +55 -0
  6. package/dist/types/core/annotations.d.ts +75 -0
  7. package/dist/types/core/banks.d.ts +33 -0
  8. package/dist/types/core/beam/consolidate.d.ts +38 -0
  9. package/dist/types/core/beam/helpers.d.ts +69 -0
  10. package/dist/types/core/beam/index.d.ts +59 -0
  11. package/dist/types/core/beam/recall.d.ts +52 -0
  12. package/dist/types/core/beam/schema.d.ts +2 -0
  13. package/dist/types/core/beam/store.d.ts +57 -0
  14. package/dist/types/core/beam/types.d.ts +268 -0
  15. package/dist/types/core/binary-vectors.d.ts +54 -0
  16. package/dist/types/core/chat-normalize.d.ts +13 -0
  17. package/dist/types/core/content-sanitizer.d.ts +18 -0
  18. package/dist/types/core/cost-log.d.ts +13 -0
  19. package/dist/types/core/embeddings.d.ts +93 -0
  20. package/dist/types/core/entities.d.ts +9 -0
  21. package/dist/types/core/episodic-graph.d.ts +89 -0
  22. package/dist/types/core/extraction/client.d.ts +35 -0
  23. package/dist/types/core/extraction/diagnostics.d.ts +51 -0
  24. package/dist/types/core/extraction/prompts.d.ts +2 -0
  25. package/dist/types/core/extraction.d.ts +34 -0
  26. package/dist/types/core/fastembed-model-cache.d.ts +2 -0
  27. package/dist/types/core/fastembed-runtime.d.ts +41 -0
  28. package/dist/types/core/index.d.ts +6 -0
  29. package/dist/types/core/llm-backends.d.ts +23 -0
  30. package/dist/types/core/local-llm.d.ts +20 -0
  31. package/dist/types/core/memory.d.ts +186 -0
  32. package/dist/types/core/migrations/e6-triplestore-split.d.ts +17 -0
  33. package/dist/types/core/migrations/index.d.ts +1 -0
  34. package/dist/types/core/mmr.d.ts +8 -0
  35. package/dist/types/core/orchestrator.d.ts +20 -0
  36. package/dist/types/core/patterns.d.ts +61 -0
  37. package/dist/types/core/plugins.d.ts +109 -0
  38. package/dist/types/core/polyphonic-recall.d.ts +66 -0
  39. package/dist/types/core/query-cache.d.ts +45 -0
  40. package/dist/types/core/query-intent.d.ts +20 -0
  41. package/dist/types/core/recall-diagnostics.d.ts +48 -0
  42. package/dist/types/core/runtime-options.d.ts +76 -0
  43. package/dist/types/core/shmr.d.ts +62 -0
  44. package/dist/types/core/streaming.d.ts +136 -0
  45. package/dist/types/core/synonyms.d.ts +46 -0
  46. package/dist/types/core/temporal-parser.d.ts +16 -0
  47. package/dist/types/core/token-counter.d.ts +8 -0
  48. package/dist/types/core/triples.d.ts +63 -0
  49. package/dist/types/core/typed-memory.d.ts +39 -0
  50. package/dist/types/core/vector-index.d.ts +16 -0
  51. package/dist/types/core/vector-math.d.ts +1 -0
  52. package/dist/types/core/veracity-consolidation.d.ts +60 -0
  53. package/dist/types/core/weibull.d.ts +96 -0
  54. package/dist/types/db.d.ts +18 -0
  55. package/dist/types/diagnose.d.ts +24 -0
  56. package/dist/types/dr/index.d.ts +1 -0
  57. package/dist/types/dr/recovery.d.ts +68 -0
  58. package/dist/types/index.d.ts +6 -0
  59. package/dist/types/mcp-server.d.ts +40 -0
  60. package/dist/types/mcp-tools.d.ts +484 -0
  61. package/dist/types/migrations/e6-triplestore-split.d.ts +1 -0
  62. package/dist/types/migrations/index.d.ts +1 -0
  63. package/dist/types/types.d.ts +145 -0
  64. package/dist/types/util/datetime.d.ts +8 -0
  65. package/dist/types/util/env.d.ts +10 -0
  66. package/dist/types/util/ids.d.ts +3 -0
  67. package/dist/types/util/lru.d.ts +12 -0
  68. package/dist/types/util/regex.d.ts +11 -0
  69. package/package.json +108 -0
  70. package/src/cli.ts +398 -0
  71. package/src/config.ts +373 -0
  72. package/src/core/aaak.ts +142 -0
  73. package/src/core/annotations.ts +456 -0
  74. package/src/core/banks.ts +133 -0
  75. package/src/core/beam/consolidate.ts +1132 -0
  76. package/src/core/beam/helpers.ts +831 -0
  77. package/src/core/beam/index.ts +358 -0
  78. package/src/core/beam/recall.ts +1225 -0
  79. package/src/core/beam/schema.ts +426 -0
  80. package/src/core/beam/store.ts +1083 -0
  81. package/src/core/beam/types.ts +303 -0
  82. package/src/core/binary-vectors.ts +317 -0
  83. package/src/core/chat-normalize.ts +160 -0
  84. package/src/core/content-sanitizer.ts +136 -0
  85. package/src/core/cost-log.ts +101 -0
  86. package/src/core/embeddings.ts +624 -0
  87. package/src/core/entities.ts +263 -0
  88. package/src/core/episodic-graph.ts +708 -0
  89. package/src/core/extraction/client.ts +187 -0
  90. package/src/core/extraction/diagnostics.ts +193 -0
  91. package/src/core/extraction/prompts.ts +31 -0
  92. package/src/core/extraction.ts +491 -0
  93. package/src/core/fastembed-model-cache.ts +39 -0
  94. package/src/core/fastembed-runtime.ts +172 -0
  95. package/src/core/index.ts +39 -0
  96. package/src/core/llm-backends.ts +54 -0
  97. package/src/core/local-llm.ts +475 -0
  98. package/src/core/memory.ts +680 -0
  99. package/src/core/migrations/e6-triplestore-split.ts +211 -0
  100. package/src/core/migrations/index.ts +1 -0
  101. package/src/core/mmr.ts +101 -0
  102. package/src/core/orchestrator.ts +62 -0
  103. package/src/core/patterns.ts +484 -0
  104. package/src/core/plugins.ts +375 -0
  105. package/src/core/polyphonic-recall.ts +563 -0
  106. package/src/core/query-cache.ts +351 -0
  107. package/src/core/query-intent.ts +139 -0
  108. package/src/core/recall-diagnostics.ts +157 -0
  109. package/src/core/runtime-options.ts +130 -0
  110. package/src/core/shmr.ts +567 -0
  111. package/src/core/streaming.ts +419 -0
  112. package/src/core/synonyms.ts +197 -0
  113. package/src/core/temporal-parser.ts +363 -0
  114. package/src/core/token-counter.ts +30 -0
  115. package/src/core/triples.ts +452 -0
  116. package/src/core/typed-memory.ts +407 -0
  117. package/src/core/vector-index.ts +83 -0
  118. package/src/core/vector-math.ts +23 -0
  119. package/src/core/veracity-consolidation.ts +477 -0
  120. package/src/core/weibull.ts +124 -0
  121. package/src/db.ts +186 -0
  122. package/src/diagnose.ts +174 -0
  123. package/src/dr/index.ts +1 -0
  124. package/src/dr/recovery.ts +405 -0
  125. package/src/index.ts +33 -0
  126. package/src/mcp-server.ts +155 -0
  127. package/src/mcp-tools.ts +970 -0
  128. package/src/migrations/e6-triplestore-split.ts +1 -0
  129. package/src/migrations/index.ts +1 -0
  130. package/src/types.ts +157 -0
  131. package/src/util/datetime.ts +69 -0
  132. package/src/util/env.ts +65 -0
  133. package/src/util/ids.ts +19 -0
  134. package/src/util/lru.ts +48 -0
  135. package/src/util/regex.ts +165 -0
@@ -0,0 +1,407 @@
1
+ export const MemoryType = {
2
+ FACT: "fact",
3
+ PREFERENCE: "preference",
4
+ DECISION: "decision",
5
+ COMMITMENT: "commitment",
6
+ GOAL: "goal",
7
+ EVENT: "event",
8
+ INSTRUCTION: "instruction",
9
+ RELATIONSHIP: "relationship",
10
+ CONTEXT: "context",
11
+ LEARNING: "learning",
12
+ OBSERVATION: "observation",
13
+ ERROR: "error",
14
+ ARTIFACT: "artifact",
15
+ UNKNOWN: "unknown",
16
+ } as const;
17
+
18
+ export type MemoryType = (typeof MemoryType)[keyof typeof MemoryType];
19
+ export type TypePriority =
20
+ | "stable"
21
+ | "moderate"
22
+ | "high"
23
+ | "time_critical"
24
+ | "decaying"
25
+ | "accumulating"
26
+ | "evolving"
27
+ | "persistent"
28
+ | "reference";
29
+
30
+ export interface TypeMatch {
31
+ memory_type: MemoryType;
32
+ memoryType: MemoryType;
33
+ confidence: number;
34
+ matched_pattern: string;
35
+ matchedPattern: string;
36
+ priority: TypePriority;
37
+ }
38
+
39
+ export type TypePattern = readonly [
40
+ pattern: string,
41
+ memoryType: MemoryType,
42
+ baseConfidence: number,
43
+ priority: TypePriority,
44
+ ];
45
+ type CompiledTypePattern = readonly [
46
+ pattern: RegExp,
47
+ matchedPattern: string,
48
+ memoryType: MemoryType,
49
+ baseConfidence: number,
50
+ priority: TypePriority,
51
+ ];
52
+ const MEMORY_TYPE_ORDER: readonly MemoryType[] = [
53
+ MemoryType.FACT,
54
+ MemoryType.PREFERENCE,
55
+ MemoryType.DECISION,
56
+ MemoryType.COMMITMENT,
57
+ MemoryType.GOAL,
58
+ MemoryType.EVENT,
59
+ MemoryType.INSTRUCTION,
60
+ MemoryType.RELATIONSHIP,
61
+ MemoryType.CONTEXT,
62
+ MemoryType.LEARNING,
63
+ MemoryType.OBSERVATION,
64
+ MemoryType.ERROR,
65
+ MemoryType.ARTIFACT,
66
+ MemoryType.UNKNOWN,
67
+ ];
68
+
69
+ function typePattern(
70
+ pattern: string,
71
+ memoryType: MemoryType,
72
+ baseConfidence: number,
73
+ priority: TypePriority,
74
+ ): TypePattern {
75
+ return [pattern, memoryType, baseConfidence, priority];
76
+ }
77
+
78
+ export const TYPE_PATTERNS: readonly TypePattern[] = [
79
+ // FACT: Objective, verifiable information
80
+ typePattern(String.raw`\b(is|are|was|were)\s+(a|an|the)\s+\w+`, MemoryType.FACT, 0.6, "stable"),
81
+ typePattern(String.raw`\b(has|have|had)\s+\d+`, MemoryType.FACT, 0.7, "stable"),
82
+ typePattern(String.raw`\b(contains|consists?|comprises?)\b`, MemoryType.FACT, 0.8, "stable"),
83
+ typePattern(String.raw`\b(version|v)\s*\d+\.?\d*`, MemoryType.FACT, 0.9, "stable"),
84
+ typePattern(String.raw`\b(API|endpoint|URL|database|DB)\s+(is|at|points?\s+to)`, MemoryType.FACT, 0.8, "stable"),
85
+ typePattern(String.raw`\b(created|modified|updated)\s+(on|at)\s+\d{4}`, MemoryType.FACT, 0.8, "stable"),
86
+
87
+ // PREFERENCE: User/system preferences
88
+ typePattern(String.raw`\b(prefer|likes?|enjoys?|loves?|hates?|dislikes?)\b`, MemoryType.PREFERENCE, 0.8, "moderate"),
89
+ typePattern(String.raw`\b(want|wants|wanted)\s+(to|the|a|an)\b`, MemoryType.PREFERENCE, 0.6, "moderate"),
90
+ typePattern(String.raw`\b(rather|instead|alternative)\b`, MemoryType.PREFERENCE, 0.5, "moderate"),
91
+ typePattern(String.raw`\b(dark\s+mode|light\s+mode|theme|color\s+scheme)\b`, MemoryType.PREFERENCE, 0.9, "moderate"),
92
+ typePattern(String.raw`\b(usually|typically|normally|generally)\b`, MemoryType.PREFERENCE, 0.6, "moderate"),
93
+
94
+ // DECISION: Choices affecting future
95
+ typePattern(String.raw`\b(decided|chose|selected|picked|opted)\b`, MemoryType.DECISION, 0.9, "high"),
96
+ typePattern(String.raw`\b(going\s+with|settled\s+on|locked\s+in)\b`, MemoryType.DECISION, 0.8, "high"),
97
+ typePattern(String.raw`\b(choose|select|pick)\s+(between|from|among)\b`, MemoryType.DECISION, 0.7, "high"),
98
+ typePattern(String.raw`\b(final\s+decision|final\s+call|final\s+choice)\b`, MemoryType.DECISION, 0.9, "high"),
99
+ typePattern(String.raw`\b(will\s+use|using|adopt|adopting)\s+(the|a|an)?\s*\w+`, MemoryType.DECISION, 0.7, "high"),
100
+
101
+ // COMMITMENT: Promises, obligations, deadlines
102
+ typePattern(
103
+ String.raw`\b(will|shall|must|need\s+to)\s+\w+\s+(by|before|until)\b`,
104
+ MemoryType.COMMITMENT,
105
+ 0.8,
106
+ "time_critical",
107
+ ),
108
+ typePattern(String.raw`\b(deadline|due\s+date|due|milestone)\b`, MemoryType.COMMITMENT, 0.9, "time_critical"),
109
+ typePattern(String.raw`\b(promise|committed|pledged|obligated)\b`, MemoryType.COMMITMENT, 0.9, "time_critical"),
110
+ typePattern(
111
+ String.raw`\b(deliver|ship|release|deploy)\s+(by|before|on)\b`,
112
+ MemoryType.COMMITMENT,
113
+ 0.8,
114
+ "time_critical",
115
+ ),
116
+ typePattern(
117
+ String.raw`\b(EOD|COB|end\s+of\s+day|close\s+of\s+business)\b`,
118
+ MemoryType.COMMITMENT,
119
+ 0.7,
120
+ "time_critical",
121
+ ),
122
+ typePattern(
123
+ String.raw`\b(tomorrow|next\s+week|Monday|Friday)\s+(by|at)\b`,
124
+ MemoryType.COMMITMENT,
125
+ 0.6,
126
+ "time_critical",
127
+ ),
128
+
129
+ // GOAL: Objectives to achieve
130
+ typePattern(String.raw`\b(goal|objective|target|aim|purpose)\b`, MemoryType.GOAL, 0.9, "high"),
131
+ typePattern(String.raw`\b(achieve|reach|hit|attain|accomplish)\s+\d+`, MemoryType.GOAL, 0.8, "high"),
132
+ typePattern(String.raw`\b(KPI|metric|OKR|success\s+criteria)\b`, MemoryType.GOAL, 0.9, "high"),
133
+ typePattern(String.raw`\b(roadmap|plan|strategy)\s+(for|to)\b`, MemoryType.GOAL, 0.7, "high"),
134
+ typePattern(
135
+ String.raw`\b(reach|get\s+to|grow\s+to)\s+\d+[KkMm]?\s+(users|customers|revenue)\b`,
136
+ MemoryType.GOAL,
137
+ 0.8,
138
+ "high",
139
+ ),
140
+
141
+ // EVENT: Historical occurrences
142
+ typePattern(
143
+ String.raw`\b(meeting|call|discussion|conversation)\s+(with|about)\b`,
144
+ MemoryType.EVENT,
145
+ 0.7,
146
+ "decaying",
147
+ ),
148
+ typePattern(String.raw`\b(happened|occurred|took\s+place|went\s+down)\b`, MemoryType.EVENT, 0.8, "decaying"),
149
+ typePattern(String.raw`\b(yesterday|last\s+week|last\s+month|earlier\s+today)\b`, MemoryType.EVENT, 0.6, "decaying"),
150
+ typePattern(String.raw`\b(scheduled|planned|booked|set\s+up)\s+(for|at)\b`, MemoryType.EVENT, 0.7, "decaying"),
151
+ typePattern(String.raw`\b(incident|outage|bug|issue)\s+#?\d+`, MemoryType.EVENT, 0.8, "decaying"),
152
+ typePattern(String.raw`\b( launched|released|shipped|deployed)\s+(on|at)\b`, MemoryType.EVENT, 0.8, "decaying"),
153
+
154
+ // INSTRUCTION: Rules, guidelines
155
+ typePattern(String.raw`\b(always|never|must|should|shall|do\s+not|don't)\b`, MemoryType.INSTRUCTION, 0.7, "stable"),
156
+ typePattern(String.raw`\b(rule|policy|guideline|procedure|protocol)\b`, MemoryType.INSTRUCTION, 0.9, "stable"),
157
+ typePattern(String.raw`\b(how\s+to|steps?\s+to|guide\s+to|tutorial)\b`, MemoryType.INSTRUCTION, 0.8, "stable"),
158
+ typePattern(String.raw`\b(remember\s+to|make\s+sure|ensure|verify)\b`, MemoryType.INSTRUCTION, 0.6, "stable"),
159
+ typePattern(String.raw`\b(first|then|next|finally)\s*,?\s*\w+`, MemoryType.INSTRUCTION, 0.5, "stable"),
160
+ typePattern(String.raw`\b(if\s+.+\s+then\s+.+)`, MemoryType.INSTRUCTION, 0.7, "stable"),
161
+
162
+ // RELATIONSHIP: Entity connections
163
+ typePattern(String.raw`\b(manages?|reports?\s+to|supervises?|leads?)\b`, MemoryType.RELATIONSHIP, 0.9, "stable"),
164
+ typePattern(String.raw`\b(owns?|belongs?\s+to|part\s+of|member\s+of)\b`, MemoryType.RELATIONSHIP, 0.8, "stable"),
165
+ typePattern(
166
+ String.raw`\b(works?\s+with|collaborates?\s+with|partners?\s+with)\b`,
167
+ MemoryType.RELATIONSHIP,
168
+ 0.8,
169
+ "stable",
170
+ ),
171
+ typePattern(String.raw`\b(depends?\s+on|requires?|needs?)\b`, MemoryType.RELATIONSHIP, 0.7, "stable"),
172
+ typePattern(String.raw`\b(related\s+to|connected\s+to|associated\s+with)\b`, MemoryType.RELATIONSHIP, 0.6, "stable"),
173
+ typePattern(
174
+ String.raw`\b(is\s+a|is\s+an)\s+(type\s+of|kind\s+of|form\s+of)\b`,
175
+ MemoryType.RELATIONSHIP,
176
+ 0.7,
177
+ "stable",
178
+ ),
179
+
180
+ // CONTEXT: Situational information
181
+ typePattern(String.raw`\b(currently|right\s+now|at\s+the\s+moment|presently)\b`, MemoryType.CONTEXT, 0.7, "high"),
182
+ typePattern(String.raw`\b(working\s+on|focusing\s+on|dealing\s+with)\b`, MemoryType.CONTEXT, 0.8, "high"),
183
+ typePattern(String.raw`\b(status|state|phase|stage)\s+(is|of)\b`, MemoryType.CONTEXT, 0.7, "high"),
184
+ typePattern(String.raw`\b(in\s+progress|ongoing|active|pending|blocked)\b`, MemoryType.CONTEXT, 0.8, "high"),
185
+ typePattern(String.raw`\b(environment|setup|configuration|settings?)\b`, MemoryType.CONTEXT, 0.6, "high"),
186
+ typePattern(String.raw`\b(today|this\s+week|this\s+sprint|this\s+quarter)\b`, MemoryType.CONTEXT, 0.5, "high"),
187
+
188
+ // LEARNING: Lessons from experience
189
+ typePattern(String.raw`\b(learned|realized|discovered|found\s+out)\b`, MemoryType.LEARNING, 0.8, "accumulating"),
190
+ typePattern(String.raw`\b(lesson|takeaway|insight|finding)\b`, MemoryType.LEARNING, 0.9, "accumulating"),
191
+ typePattern(String.raw`\b(turns?\s+out|surprisingly|interestingly)\b`, MemoryType.LEARNING, 0.7, "accumulating"),
192
+ typePattern(String.raw`\b(should\s+have|could\s+have|would\s+have)\b`, MemoryType.LEARNING, 0.6, "accumulating"),
193
+ typePattern(
194
+ String.raw`\b(best\s+practice|lessons?\s+learned|post[-\s]?mortem)\b`,
195
+ MemoryType.LEARNING,
196
+ 0.9,
197
+ "accumulating",
198
+ ),
199
+
200
+ // OBSERVATION: Patterns noticed
201
+ typePattern(String.raw`\b(noticed|observed|saw|seems?)\b`, MemoryType.OBSERVATION, 0.7, "evolving"),
202
+ typePattern(String.raw`\b(pattern|trend|correlation|tends?\s+to)\b`, MemoryType.OBSERVATION, 0.9, "evolving"),
203
+ typePattern(
204
+ String.raw`\b(often|frequently|sometimes|rarely|usually)\s+\w+`,
205
+ MemoryType.OBSERVATION,
206
+ 0.6,
207
+ "evolving",
208
+ ),
209
+ typePattern(String.raw`\b(appears?|looks?\s+like|seems?\s+like)\b`, MemoryType.OBSERVATION, 0.6, "evolving"),
210
+ typePattern(
211
+ String.raw`\b(increasing|decreasing|growing|shrinking|stable)\b`,
212
+ MemoryType.OBSERVATION,
213
+ 0.7,
214
+ "evolving",
215
+ ),
216
+ typePattern(String.raw`\b(every\s+time|whenever|each\s+time)\b`, MemoryType.OBSERVATION, 0.8, "evolving"),
217
+
218
+ // ERROR: Mistakes to avoid
219
+ typePattern(String.raw`\b(error|bug|issue|problem|failure|crash)\b`, MemoryType.ERROR, 0.7, "persistent"),
220
+ typePattern(String.raw`\b(broke|broken|failed|failing|doesn't\s+work)\b`, MemoryType.ERROR, 0.8, "persistent"),
221
+ typePattern(
222
+ String.raw`\b(do\s+not|never|avoid|watch\s+out|be\s+careful)\s+\w+\s+(error|bug|issue)\b`,
223
+ MemoryType.ERROR,
224
+ 0.9,
225
+ "persistent",
226
+ ),
227
+ typePattern(String.raw`\b(deprecated|obsolete|legacy|outdated)\b`, MemoryType.ERROR, 0.8, "persistent"),
228
+ typePattern(String.raw`\b(exception|timeout|crash|hang|freeze)\b`, MemoryType.ERROR, 0.8, "persistent"),
229
+ typePattern(String.raw`\b(workaround|hotfix|patch|kludge)\b`, MemoryType.ERROR, 0.7, "persistent"),
230
+
231
+ // ARTIFACT: Document/code references
232
+ typePattern(String.raw`\b(document|doc|spreadsheet|sheet|slide)\b`, MemoryType.ARTIFACT, 0.6, "reference"),
233
+ typePattern(String.raw`\b(file|folder|directory|path)\s+(name|called|at)\b`, MemoryType.ARTIFACT, 0.7, "reference"),
234
+ typePattern(String.raw`\b(PR|pull\s+request|issue|ticket|ticket)\s+#?\d+`, MemoryType.ARTIFACT, 0.9, "reference"),
235
+ typePattern(String.raw`\b(commit|branch|tag|release)\s+[a-f0-9]{7,40}\b`, MemoryType.ARTIFACT, 0.9, "reference"),
236
+ typePattern(String.raw`\b(repo|repository|project|codebase)\s+(at|on|in)\b`, MemoryType.ARTIFACT, 0.7, "reference"),
237
+ typePattern(String.raw`\b(link|URL|href|reference)\s+(to|for)\b`, MemoryType.ARTIFACT, 0.6, "reference"),
238
+ typePattern(String.raw`\b(README|CHANGELOG|LICENSE|CONTRIBUTING)\b`, MemoryType.ARTIFACT, 0.9, "reference"),
239
+ ];
240
+
241
+ const COMPILED_TYPE_PATTERNS: readonly CompiledTypePattern[] = TYPE_PATTERNS.map(
242
+ ([pattern, memoryType, baseConfidence, priority]): CompiledTypePattern => [
243
+ new RegExp(pattern, "i"),
244
+ pattern,
245
+ memoryType,
246
+ baseConfidence,
247
+ priority,
248
+ ],
249
+ );
250
+
251
+ export const CONFIDENCE_BOOSTERS: Readonly<Record<MemoryType, readonly string[]>> = {
252
+ [MemoryType.FACT]: ["verified", "confirmed", "official", "documented", "according to", "data shows"],
253
+ [MemoryType.PREFERENCE]: ["always", "never", "absolutely", "definitely", "strongly"],
254
+ [MemoryType.DECISION]: ["final", "official", "approved", "agreed", "consensus"],
255
+ [MemoryType.COMMITMENT]: ["promise", "guarantee", "committed", "deadline", "SLA"],
256
+ [MemoryType.GOAL]: ["target", "objective", "KPI", "OKR", "success metric"],
257
+ [MemoryType.EVENT]: ["specifically", "exactly", "precisely", "at", "on"],
258
+ [MemoryType.INSTRUCTION]: ["mandatory", "required", "critical", "important"],
259
+ [MemoryType.RELATIONSHIP]: ["directly", "reports to", "managed by", "owned by"],
260
+ [MemoryType.CONTEXT]: ["currently", "right now", "active", "in progress"],
261
+ [MemoryType.LEARNING]: ["key lesson", "important finding", "critical insight"],
262
+ [MemoryType.OBSERVATION]: ["consistently", "repeatedly", "over time", "pattern"],
263
+ [MemoryType.ERROR]: ["critical", "severe", "blocking", "P0", "P1"],
264
+ [MemoryType.ARTIFACT]: ["official", "canonical", "source of truth", "reference"],
265
+ [MemoryType.UNKNOWN]: [],
266
+ };
267
+
268
+ function makeTypeMatch(
269
+ memoryType: MemoryType,
270
+ confidence: number,
271
+ matchedPattern: string,
272
+ priority: TypePriority,
273
+ ): TypeMatch {
274
+ return {
275
+ memory_type: memoryType,
276
+ memoryType,
277
+ confidence,
278
+ matched_pattern: matchedPattern,
279
+ matchedPattern,
280
+ priority,
281
+ };
282
+ }
283
+
284
+ export function classifyMemory(content: string): TypeMatch {
285
+ if (content.trim().length === 0) {
286
+ return makeTypeMatch(MemoryType.UNKNOWN, 0.0, "", "stable");
287
+ }
288
+
289
+ const contentLower = content.toLowerCase();
290
+ let bestMatch: TypeMatch | null = null;
291
+ let bestScore = 0.0;
292
+
293
+ for (const [pattern, matchedPattern, memoryType, baseConfidence, priority] of COMPILED_TYPE_PATTERNS) {
294
+ const match = pattern.exec(contentLower);
295
+ if (match === null) {
296
+ continue;
297
+ }
298
+
299
+ let confidence = baseConfidence;
300
+ const matchText = match[0] ?? "";
301
+ if (matchText.length > 20) {
302
+ confidence += 0.1;
303
+ } else if (matchText.length > 10) {
304
+ confidence += 0.05;
305
+ }
306
+
307
+ for (const booster of CONFIDENCE_BOOSTERS[memoryType]) {
308
+ if (contentLower.includes(booster.toLowerCase())) {
309
+ confidence += 0.05;
310
+ }
311
+ }
312
+
313
+ confidence = Math.min(confidence, 1.0);
314
+ const score = confidence * (1.0 + 0.1 * MEMORY_TYPE_ORDER.indexOf(memoryType));
315
+ if (score > bestScore) {
316
+ bestScore = score;
317
+ bestMatch = makeTypeMatch(memoryType, confidence, matchedPattern, priority);
318
+ }
319
+ }
320
+
321
+ if (bestMatch === null) {
322
+ return content.trim().split(/\s+/).length < 5
323
+ ? makeTypeMatch(MemoryType.FACT, 0.3, "default_short", "stable")
324
+ : makeTypeMatch(MemoryType.CONTEXT, 0.3, "default_long", "high");
325
+ }
326
+
327
+ return bestMatch;
328
+ }
329
+
330
+ export function classifyBatch(contents: readonly string[]): TypeMatch[] {
331
+ return contents.map(content => classifyMemory(content));
332
+ }
333
+
334
+ export function getTypePriority(memoryType: MemoryType | string): number {
335
+ switch (memoryType) {
336
+ case MemoryType.INSTRUCTION:
337
+ return 10;
338
+ case MemoryType.COMMITMENT:
339
+ return 9;
340
+ case MemoryType.ERROR:
341
+ return 8;
342
+ case MemoryType.GOAL:
343
+ return 7;
344
+ case MemoryType.DECISION:
345
+ return 6;
346
+ case MemoryType.PREFERENCE:
347
+ return 5;
348
+ case MemoryType.FACT:
349
+ case MemoryType.RELATIONSHIP:
350
+ return 4;
351
+ case MemoryType.LEARNING:
352
+ case MemoryType.OBSERVATION:
353
+ return 3;
354
+ case MemoryType.EVENT:
355
+ case MemoryType.CONTEXT:
356
+ return 2;
357
+ case MemoryType.ARTIFACT:
358
+ return 1;
359
+ default:
360
+ return 0;
361
+ }
362
+ }
363
+
364
+ const CONSOLIDATABLE_MEMORY_TYPES: ReadonlySet<string> = new Set<string>([
365
+ MemoryType.FACT,
366
+ MemoryType.PREFERENCE,
367
+ MemoryType.DECISION,
368
+ MemoryType.GOAL,
369
+ MemoryType.LEARNING,
370
+ MemoryType.OBSERVATION,
371
+ MemoryType.RELATIONSHIP,
372
+ MemoryType.INSTRUCTION,
373
+ ]);
374
+
375
+ export function shouldConsolidate(memoryType: MemoryType | string): boolean {
376
+ return CONSOLIDATABLE_MEMORY_TYPES.has(memoryType);
377
+ }
378
+
379
+ export function getDecayRate(memoryType: MemoryType | string): number {
380
+ switch (memoryType) {
381
+ case MemoryType.CONTEXT:
382
+ return 0.9;
383
+ case MemoryType.EVENT:
384
+ return 0.7;
385
+ case MemoryType.OBSERVATION:
386
+ case MemoryType.UNKNOWN:
387
+ return 0.5;
388
+ case MemoryType.GOAL:
389
+ return 0.4;
390
+ case MemoryType.LEARNING:
391
+ case MemoryType.DECISION:
392
+ return 0.3;
393
+ case MemoryType.PREFERENCE:
394
+ return 0.2;
395
+ case MemoryType.FACT:
396
+ case MemoryType.RELATIONSHIP:
397
+ case MemoryType.ARTIFACT:
398
+ return 0.1;
399
+ case MemoryType.INSTRUCTION:
400
+ case MemoryType.ERROR:
401
+ return 0.05;
402
+ case MemoryType.COMMITMENT:
403
+ return 0.5;
404
+ default:
405
+ return 0.3;
406
+ }
407
+ }
@@ -0,0 +1,83 @@
1
+ import { vectorIndexTopK } from "@linxiraos/pi-natives";
2
+
3
+ export interface ExactVectorSearchHit<TId> {
4
+ id: TId;
5
+ score: number;
6
+ }
7
+
8
+ export interface ExactVectorIndex<TId> {
9
+ readonly ids: readonly TId[];
10
+ readonly matrix: Float32Array;
11
+ readonly dimensions: number;
12
+ readonly count: number;
13
+ }
14
+
15
+ export interface VectorIndexRow<TId> {
16
+ id: TId;
17
+ vector: readonly number[] | null | undefined;
18
+ }
19
+
20
+ export function buildExactVectorIndex<TId>(rows: readonly VectorIndexRow<TId>[]): ExactVectorIndex<TId> {
21
+ const valid: Array<{ id: TId; vector: readonly number[]; norm: number }> = [];
22
+ let dimensions = 0;
23
+ for (const row of rows) {
24
+ const vector = row.vector;
25
+ if (!vector || vector.length === 0) continue;
26
+ let normSq = 0;
27
+ for (let i = 0; i < vector.length; i += 1) {
28
+ const value = vector[i] ?? 0;
29
+ if (!Number.isFinite(value)) {
30
+ normSq = 0;
31
+ break;
32
+ }
33
+ normSq += value * value;
34
+ }
35
+ if (normSq <= 0) continue;
36
+ valid.push({ id: row.id, vector, norm: Math.sqrt(normSq) });
37
+ if (vector.length > dimensions) dimensions = vector.length;
38
+ }
39
+
40
+ // Float32Array keeps a compact contiguous matrix that matches the shape we'd
41
+ // feed into future ANN/quantized backends. Exact cosine ranking remains sound
42
+ // here because we store normalized vectors and only compare normalized dots.
43
+ const matrix = new Float32Array(valid.length * dimensions);
44
+ const ids: TId[] = [];
45
+ for (let row = 0; row < valid.length; row += 1) {
46
+ const item = valid[row];
47
+ ids.push(item.id);
48
+ const offset = row * dimensions;
49
+ for (let col = 0; col < item.vector.length; col += 1) {
50
+ matrix[offset + col] = (item.vector[col] ?? 0) / item.norm;
51
+ }
52
+ }
53
+
54
+ return { ids, matrix, dimensions, count: ids.length };
55
+ }
56
+
57
+ export function searchExactVectorIndex<TId>(
58
+ index: ExactVectorIndex<TId>,
59
+ query: readonly number[],
60
+ limit: number,
61
+ ): ExactVectorSearchHit<TId>[] {
62
+ const k = Math.max(0, Math.trunc(limit));
63
+ if (k === 0 || index.count === 0 || index.dimensions === 0 || query.length === 0) return [];
64
+
65
+ let queryNormSq = 0;
66
+ for (const value of query) {
67
+ if (!Number.isFinite(value)) return [];
68
+ queryNormSq += value * value;
69
+ }
70
+ if (queryNormSq <= 0) return [];
71
+ // Native batch kernel: one N-API crossing scores every row and ranks the
72
+ // top k with the same stable ordering as the TS sort. TS guards above
73
+ // (finite query, positive norm, non-empty index) are preserved. Clamp k to
74
+ // the row count before the u32 boundary: Infinity or >= 2**32 would
75
+ // otherwise wrap (ToUint32) and return no hits.
76
+ const topK = vectorIndexTopK(index.matrix, index.dimensions, Float64Array.from(query), Math.min(k, index.count));
77
+ const hits: ExactVectorSearchHit<TId>[] = [];
78
+ for (let i = 0; i < topK.indices.length; i += 1) {
79
+ const row = topK.indices[i] ?? 0;
80
+ hits.push({ id: index.ids[row] as TId, score: topK.scores[i] ?? 0 });
81
+ }
82
+ return hits;
83
+ }
@@ -0,0 +1,23 @@
1
+ export function cosineSimilarity(a: ArrayLike<number>, b: ArrayLike<number>): number {
2
+ const length = a.length > b.length ? a.length : b.length;
3
+ if (length === 0) {
4
+ return 0;
5
+ }
6
+
7
+ let dot = 0;
8
+ let normA = 0;
9
+ let normB = 0;
10
+ for (let i = 0; i < length; i += 1) {
11
+ const rawA = a[i] ?? 0;
12
+ const rawB = b[i] ?? 0;
13
+ const av = Number.isFinite(rawA) ? rawA : 0;
14
+ const bv = Number.isFinite(rawB) ? rawB : 0;
15
+ dot += av * bv;
16
+ normA += av * av;
17
+ normB += bv * bv;
18
+ }
19
+ if (normA === 0 || normB === 0) {
20
+ return 0;
21
+ }
22
+ return dot / (Math.sqrt(normA) * Math.sqrt(normB));
23
+ }