@oh-my-pi/pi-mnemosyne 15.6.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 (126) hide show
  1. package/README.md +107 -0
  2. package/dist/types/cli.d.ts +35 -0
  3. package/dist/types/config.d.ts +77 -0
  4. package/dist/types/core/aaak.d.ts +55 -0
  5. package/dist/types/core/annotations.d.ts +75 -0
  6. package/dist/types/core/banks.d.ts +33 -0
  7. package/dist/types/core/beam/consolidate.d.ts +32 -0
  8. package/dist/types/core/beam/helpers.d.ts +59 -0
  9. package/dist/types/core/beam/index.d.ts +59 -0
  10. package/dist/types/core/beam/recall.d.ts +32 -0
  11. package/dist/types/core/beam/schema.d.ts +2 -0
  12. package/dist/types/core/beam/store.d.ts +35 -0
  13. package/dist/types/core/beam/types.d.ts +233 -0
  14. package/dist/types/core/binary-vectors.d.ts +54 -0
  15. package/dist/types/core/chat-normalize.d.ts +13 -0
  16. package/dist/types/core/content-sanitizer.d.ts +18 -0
  17. package/dist/types/core/cost-log.d.ts +13 -0
  18. package/dist/types/core/embeddings.d.ts +35 -0
  19. package/dist/types/core/entities.d.ts +7 -0
  20. package/dist/types/core/episodic-graph.d.ts +89 -0
  21. package/dist/types/core/extraction/client.d.ts +31 -0
  22. package/dist/types/core/extraction/diagnostics.d.ts +51 -0
  23. package/dist/types/core/extraction/prompts.d.ts +2 -0
  24. package/dist/types/core/extraction.d.ts +6 -0
  25. package/dist/types/core/index.d.ts +4 -0
  26. package/dist/types/core/llm-backends.d.ts +21 -0
  27. package/dist/types/core/local-llm.d.ts +15 -0
  28. package/dist/types/core/memory.d.ts +160 -0
  29. package/dist/types/core/migrations/e6-triplestore-split.d.ts +17 -0
  30. package/dist/types/core/migrations/index.d.ts +1 -0
  31. package/dist/types/core/mmr.d.ts +8 -0
  32. package/dist/types/core/orchestrator.d.ts +20 -0
  33. package/dist/types/core/patterns.d.ts +61 -0
  34. package/dist/types/core/plugins.d.ts +109 -0
  35. package/dist/types/core/polyphonic-recall.d.ts +66 -0
  36. package/dist/types/core/query-cache.d.ts +47 -0
  37. package/dist/types/core/query-intent.d.ts +20 -0
  38. package/dist/types/core/recall-diagnostics.d.ts +48 -0
  39. package/dist/types/core/runtime-options.d.ts +61 -0
  40. package/dist/types/core/shmr.d.ts +56 -0
  41. package/dist/types/core/streaming.d.ts +136 -0
  42. package/dist/types/core/synonyms.d.ts +46 -0
  43. package/dist/types/core/temporal-parser.d.ts +16 -0
  44. package/dist/types/core/token-counter.d.ts +8 -0
  45. package/dist/types/core/triples.d.ts +63 -0
  46. package/dist/types/core/typed-memory.d.ts +39 -0
  47. package/dist/types/core/veracity-consolidation.d.ts +60 -0
  48. package/dist/types/core/weibull.d.ts +96 -0
  49. package/dist/types/db.d.ts +16 -0
  50. package/dist/types/diagnose.d.ts +24 -0
  51. package/dist/types/dr/index.d.ts +1 -0
  52. package/dist/types/dr/recovery.d.ts +68 -0
  53. package/dist/types/index.d.ts +5 -0
  54. package/dist/types/mcp-server.d.ts +40 -0
  55. package/dist/types/mcp-tools.d.ts +484 -0
  56. package/dist/types/migrations/e6-triplestore-split.d.ts +1 -0
  57. package/dist/types/migrations/index.d.ts +1 -0
  58. package/dist/types/types.d.ts +145 -0
  59. package/dist/types/util/datetime.d.ts +8 -0
  60. package/dist/types/util/env.d.ts +10 -0
  61. package/dist/types/util/ids.d.ts +3 -0
  62. package/dist/types/util/lru.d.ts +12 -0
  63. package/dist/types/util/regex.d.ts +10 -0
  64. package/package.json +82 -0
  65. package/src/cli.ts +390 -0
  66. package/src/config.ts +326 -0
  67. package/src/core/aaak.ts +142 -0
  68. package/src/core/annotations.ts +457 -0
  69. package/src/core/banks.ts +133 -0
  70. package/src/core/beam/consolidate.ts +963 -0
  71. package/src/core/beam/helpers.ts +920 -0
  72. package/src/core/beam/index.ts +353 -0
  73. package/src/core/beam/recall.ts +1091 -0
  74. package/src/core/beam/schema.ts +423 -0
  75. package/src/core/beam/store.ts +818 -0
  76. package/src/core/beam/types.ts +268 -0
  77. package/src/core/binary-vectors.ts +336 -0
  78. package/src/core/chat-normalize.ts +160 -0
  79. package/src/core/content-sanitizer.ts +136 -0
  80. package/src/core/cost-log.ts +103 -0
  81. package/src/core/embeddings.ts +490 -0
  82. package/src/core/entities.ts +259 -0
  83. package/src/core/episodic-graph.ts +708 -0
  84. package/src/core/extraction/client.ts +162 -0
  85. package/src/core/extraction/diagnostics.ts +193 -0
  86. package/src/core/extraction/prompts.ts +31 -0
  87. package/src/core/extraction.ts +335 -0
  88. package/src/core/index.ts +30 -0
  89. package/src/core/llm-backends.ts +51 -0
  90. package/src/core/local-llm.ts +436 -0
  91. package/src/core/memory.ts +617 -0
  92. package/src/core/migrations/e6-triplestore-split.ts +211 -0
  93. package/src/core/migrations/index.ts +1 -0
  94. package/src/core/mmr.ts +71 -0
  95. package/src/core/orchestrator.ts +53 -0
  96. package/src/core/patterns.ts +484 -0
  97. package/src/core/plugins.ts +375 -0
  98. package/src/core/polyphonic-recall.ts +563 -0
  99. package/src/core/query-cache.ts +370 -0
  100. package/src/core/query-intent.ts +139 -0
  101. package/src/core/recall-diagnostics.ts +157 -0
  102. package/src/core/runtime-options.ts +108 -0
  103. package/src/core/shmr.ts +471 -0
  104. package/src/core/streaming.ts +419 -0
  105. package/src/core/synonyms.ts +197 -0
  106. package/src/core/temporal-parser.ts +363 -0
  107. package/src/core/token-counter.ts +30 -0
  108. package/src/core/triples.ts +452 -0
  109. package/src/core/typed-memory.ts +407 -0
  110. package/src/core/veracity-consolidation.ts +477 -0
  111. package/src/core/weibull.ts +124 -0
  112. package/src/db.ts +128 -0
  113. package/src/diagnose.ts +174 -0
  114. package/src/dr/index.ts +1 -0
  115. package/src/dr/recovery.ts +405 -0
  116. package/src/index.ts +32 -0
  117. package/src/mcp-server.ts +155 -0
  118. package/src/mcp-tools.ts +961 -0
  119. package/src/migrations/e6-triplestore-split.ts +1 -0
  120. package/src/migrations/index.ts +1 -0
  121. package/src/types.ts +157 -0
  122. package/src/util/datetime.ts +69 -0
  123. package/src/util/env.ts +65 -0
  124. package/src/util/ids.ts +19 -0
  125. package/src/util/lru.ts +48 -0
  126. package/src/util/regex.ts +165 -0
@@ -0,0 +1,259 @@
1
+ const ENTITY_EXTRACTION_STOP_WORD_VALUES = [
2
+ "the",
3
+ "a",
4
+ "an",
5
+ "and",
6
+ "or",
7
+ "but",
8
+ "in",
9
+ "on",
10
+ "at",
11
+ "to",
12
+ "for",
13
+ "of",
14
+ "with",
15
+ "by",
16
+ "from",
17
+ "as",
18
+ "is",
19
+ "was",
20
+ "are",
21
+ "were",
22
+ "be",
23
+ "been",
24
+ "being",
25
+ "have",
26
+ "has",
27
+ "had",
28
+ "do",
29
+ "does",
30
+ "did",
31
+ "will",
32
+ "would",
33
+ "could",
34
+ "should",
35
+ "may",
36
+ "might",
37
+ "can",
38
+ "shall",
39
+ "i",
40
+ "you",
41
+ "he",
42
+ "she",
43
+ "it",
44
+ "we",
45
+ "they",
46
+ "me",
47
+ "him",
48
+ "her",
49
+ "us",
50
+ "them",
51
+ "my",
52
+ "your",
53
+ "his",
54
+ "its",
55
+ "our",
56
+ "their",
57
+ "this",
58
+ "that",
59
+ "these",
60
+ "those",
61
+ "here",
62
+ "there",
63
+ "where",
64
+ "when",
65
+ "what",
66
+ "which",
67
+ "who",
68
+ "whom",
69
+ "whose",
70
+ "how",
71
+ "why",
72
+ "assistant",
73
+ "user",
74
+ "skill",
75
+ "review",
76
+ "target",
77
+ "class",
78
+ "level",
79
+ "signals",
80
+ "phase",
81
+ "api",
82
+ "pi",
83
+ "summary",
84
+ "added",
85
+ "active",
86
+ "not",
87
+ "whether",
88
+ "all",
89
+ "no",
90
+ "replying",
91
+ "ai",
92
+ "memory",
93
+ "conversation",
94
+ "fact",
95
+ "false",
96
+ "true",
97
+ "none",
98
+ "null",
99
+ "signal",
100
+ "hermes",
101
+ "agent",
102
+ "model",
103
+ "system",
104
+ "note",
105
+ "task",
106
+ "project",
107
+ "result",
108
+ "output",
109
+ "input",
110
+ "data",
111
+ "step",
112
+ "process",
113
+ "point",
114
+ "way",
115
+ "thing",
116
+ "time",
117
+ "work",
118
+ ] as const;
119
+
120
+ export const ENTITY_EXTRACTION_STOP_WORDS: ReadonlySet<string> = new Set(ENTITY_EXTRACTION_STOP_WORD_VALUES);
121
+ const ENTITY_PATTERNS: readonly RegExp[] = [
122
+ /@(\w{2,30})/g,
123
+ /#(\w{2,30})/g,
124
+ /"([^"]{2,50})"/g,
125
+ /'([^']{2,50})'/g,
126
+ /\b([A-Z][a-zA-Z]*(?:\s+[A-Z][a-zA-Z]*){1,4})\b/g,
127
+ /\b([A-Z][a-zA-Z]{1,20})\b/g,
128
+ ];
129
+
130
+ function chars(value: string): string[] {
131
+ return Array.from(value);
132
+ }
133
+
134
+ export function levenshteinDistance(s1: string, s2: string): number {
135
+ let left = chars(s1);
136
+ let right = chars(s2);
137
+ if (left.length < right.length) {
138
+ const tmp = left;
139
+ left = right;
140
+ right = tmp;
141
+ }
142
+ if (right.length === 0) return left.length;
143
+
144
+ let previousRow = new Array<number>(right.length + 1);
145
+ let currentRow = new Array<number>(right.length + 1).fill(0);
146
+ for (let i = 0; i <= right.length; i++) previousRow[i] = i;
147
+
148
+ for (let i = 0; i < left.length; i++) {
149
+ currentRow[0] = i + 1;
150
+ const c1 = left[i];
151
+ for (let j = 0; j < right.length; j++) {
152
+ const insertions = (previousRow[j + 1] ?? 0) + 1;
153
+ const deletions = (currentRow[j] ?? 0) + 1;
154
+ const substitutions = (previousRow[j] ?? 0) + (c1 === right[j] ? 0 : 1);
155
+ currentRow[j + 1] = Math.min(insertions, deletions, substitutions);
156
+ }
157
+ const tmp = previousRow;
158
+ previousRow = currentRow;
159
+ currentRow = tmp;
160
+ }
161
+ return previousRow[right.length] ?? 0;
162
+ }
163
+ export function similarity(s1: string, s2: string): number {
164
+ const s1Lower = s1.toLowerCase().trim();
165
+ const s2Lower = s2.toLowerCase().trim();
166
+ if (s1Lower === s2Lower) return 1.0;
167
+
168
+ const maxLen = Math.max(chars(s1Lower).length, chars(s2Lower).length);
169
+ if (maxLen === 0) return 1.0;
170
+
171
+ if (s1Lower.startsWith(s2Lower) || s2Lower.startsWith(s1Lower)) {
172
+ const longer = Math.max(chars(s1Lower).length, chars(s2Lower).length);
173
+ const shorter = Math.min(chars(s1Lower).length, chars(s2Lower).length);
174
+ if (shorter / longer < 0.3) return 0.0;
175
+ return 0.7 + (shorter / longer) * 0.3;
176
+ }
177
+
178
+ if (s1Lower.includes(s2Lower) || s2Lower.includes(s1Lower)) {
179
+ const longer = Math.max(chars(s1Lower).length, chars(s2Lower).length);
180
+ const shorter = Math.min(chars(s1Lower).length, chars(s2Lower).length);
181
+ return 0.5 + (shorter / longer) * 0.3;
182
+ }
183
+
184
+ const dist = levenshteinDistance(s1Lower, s2Lower);
185
+ return 1.0 - dist / maxLen;
186
+ }
187
+
188
+ function isPureNumber(entity: string): boolean {
189
+ const normalized = entity.replaceAll(".", "").replaceAll(",", "");
190
+ return normalized.length > 0 && /^\d+$/.test(normalized);
191
+ }
192
+
193
+ export function extractEntitiesRegex(text: string): string[] {
194
+ if (typeof text !== "string" || text.length === 0) return [];
195
+
196
+ const entities = new Set<string>();
197
+ for (const sourcePattern of ENTITY_PATTERNS) {
198
+ const pattern = new RegExp(sourcePattern.source, sourcePattern.flags);
199
+ for (const match of text.matchAll(pattern)) {
200
+ const captured = match[1];
201
+ if (captured === undefined) continue;
202
+ const entity = captured.trim();
203
+ if (entity.length < 2) continue;
204
+
205
+ const words = entity.split(/\s+/).filter(word => word.length > 0);
206
+ if (words.length === 1 && ENTITY_EXTRACTION_STOP_WORDS.has(entity.toLowerCase())) continue;
207
+ if (words.some(word => ENTITY_EXTRACTION_STOP_WORDS.has(word.toLowerCase()))) continue;
208
+ if (isPureNumber(entity)) continue;
209
+
210
+ const first = entity[0];
211
+ if (words.length === 1 && first !== undefined && first >= "a" && first <= "z") {
212
+ const groupStart = match.index + match[0].indexOf(captured);
213
+ const prefixChar = groupStart > 0 ? text[groupStart - 1] : undefined;
214
+ if (prefixChar !== "@" && prefixChar !== "#") continue;
215
+ }
216
+
217
+ entities.add(entity);
218
+ }
219
+ }
220
+
221
+ const result = Array.from(entities).sort();
222
+ const filtered = new Set<string>();
223
+ for (const entity of result) {
224
+ let isSubstring = false;
225
+ for (const other of result) {
226
+ if (other === entity || !other.includes(entity)) continue;
227
+ if (entity.startsWith("@") || entity.startsWith("#")) continue;
228
+ if (other.startsWith("@") || other.startsWith("#")) continue;
229
+ isSubstring = true;
230
+ break;
231
+ }
232
+ if (!isSubstring) filtered.add(entity);
233
+ }
234
+ return Array.from(filtered).sort();
235
+ }
236
+ export type SimilarEntity = readonly [entity: string, score: number];
237
+
238
+ export function findSimilarEntities(
239
+ entity: string,
240
+ knownEntities: readonly string[],
241
+ threshold = 0.8,
242
+ ): SimilarEntity[] {
243
+ const matches: SimilarEntity[] = [];
244
+ for (const known of knownEntities) {
245
+ if (known === entity) {
246
+ matches.push([known, 1.0]);
247
+ continue;
248
+ }
249
+ const score = similarity(entity, known);
250
+ if (score >= threshold) matches.push([known, score]);
251
+ }
252
+ matches.sort((left, right) => right[1] - left[1]);
253
+ return matches;
254
+ }
255
+ export function entityExtractionPerformance(text: string, iterations = 1000): number {
256
+ const start = performance.now();
257
+ for (let i = 0; i < iterations; i++) extractEntitiesRegex(text);
258
+ return (performance.now() - start) / iterations;
259
+ }