-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynapse Memory.py
More file actions
1249 lines (1097 loc) · 49.9 KB
/
Copy pathSynapse Memory.py
File metadata and controls
1249 lines (1097 loc) · 49.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
title: Synapse Memory
author: cloph-dsp
version: 7.0.0
license: MIT
required_open_webui_version: 0.5.0
description: The definitive memory engine for OpenWebUI. LLM-driven lifecycle management with smart
deduplication, conflict resolution, MERGE/EXTENDS consolidation, diversity-aware retrieval, and
natural language vault control — all through OpenWebUI's native APIs.
Highlights:
- UserValves: each user can enable/disable memory and toggle status notifications independently.
- Two-stage retrieval: OpenWebUI native vector search narrows the candidate pool first, then LLM
reranks. Profile memories (identity/behavior) always enter the candidate set regardless of score.
- Recency-weighted scoring: final = (LLM score × 0.7) + (recency × 0.3) with exponential decay,
so newer facts beat older ones when relevance is equal.
- Dual-block injection: <user_profile> (who the user is) + <relevant_memories> (what matters now)
— proven format from mem0 and Supermemory research.
- Five extraction operations: NEW, DELETE, UPDATE, EXTENDS (additive enrichment without overwrite),
MERGE (consolidate fragments). LLM chooses the right one per the guidelines.
- LLM intent router: keyword pre-screen skips LLM for normal messages; one LLM call handles all
vault intents ("forget X", "show memories", "find my Python memories", "clear vault").
- Temporal context: current UTC date in every extraction prompt for time-sensitive reasoning.
- Fuzzy deduplication: Jaccard ≥ 0.90 word-overlap blocks near-duplicate facts.
- Correct status events: includes status/done/error fields matching the OpenWebUI event spec.
- Circuit breaker: LLM failures trip after 3 errors; auto-recovers after 30 s.
"""
import asyncio
import json
import logging
import re
import time
from datetime import datetime, timezone
from typing import Any, Awaitable, Callable, Dict, List, Literal, Optional, Set, Tuple
from fastapi import Request
from pydantic import BaseModel, Field
from open_webui.main import app as webui_app
from open_webui.utils.chat import generate_chat_completion
from open_webui.routers.memories import (
AddMemoryForm,
QueryMemoryForm,
Memories,
add_memory,
delete_memory_by_id,
query_memory,
)
logger = logging.getLogger(__name__)
TAG_CATEGORIES: Set[str] = {
"identity", "behavior", "preference", "goal", "relationship", "possession"
}
MAX_INJECTED_MEMORIES = 12 # Cap on memories injected into conversation context
MAX_MEMORY_CONTEXT = 80 # Memories sent to LLM per prompt (inventory ceiling)
MAX_MEMORY_PROMPT_CHARS = 8000 # Character budget for the inventory block in prompts
# ---------------------------------------------------------------------------
# Keyword pre-screen for intent classification
#
# If the message contains NONE of these words it cannot be a memory management
# intent, so we skip the LLM intent call entirely. The set is intentionally
# broad — false negatives here mean an unnecessary LLM call for selection,
# false positives are impossible (we only route to LLM intent if a keyword
# fires, then let the LLM decide the actual intent).
# ---------------------------------------------------------------------------
_INTENT_KEYWORDS: frozenset = frozenset({
"memory", "memories", "remember", "recall", "forget", "forgotten",
"stored", "saved", "know about me", "clear", "wipe", "erase",
"delete my", "remove my", "what you know", "what you have",
})
# ---------------------------------------------------------------------------
# Circuit Breaker
# ---------------------------------------------------------------------------
class CircuitBreakerOpenError(Exception):
pass
class CircuitBreaker:
"""Guards against cascading LLM failures with exponential back-off recovery."""
def __init__(self, failure_threshold: int = 3, recovery_timeout: int = 30) -> None:
self.failure_threshold = failure_threshold
self.recovery_timeout = recovery_timeout
self.failure_count = 0
self.last_failure_time = 0.0
self.state = "CLOSED"
async def call(self, func, *args, **kwargs):
now = time.monotonic()
if self.state == "OPEN":
if now - self.last_failure_time >= self.recovery_timeout:
self.state = "HALF_OPEN"
logger.info("Circuit breaker: HALF_OPEN — testing recovery")
else:
raise CircuitBreakerOpenError("Circuit breaker OPEN — LLM skipped")
try:
result = await func(*args, **kwargs)
if self.state == "HALF_OPEN":
logger.info("Circuit breaker: CLOSED — recovery confirmed")
self.state = "CLOSED"
self.failure_count = 0
return result
except Exception:
self.failure_count += 1
self.last_failure_time = time.monotonic()
if self.failure_count >= self.failure_threshold:
self.state = "OPEN"
logger.warning("Circuit breaker: OPEN after %d failures", self.failure_count)
raise
def reset(self) -> None:
self.state = "CLOSED"
self.failure_count = 0
self.last_failure_time = 0.0
# ---------------------------------------------------------------------------
# Filter
# ---------------------------------------------------------------------------
class Filter:
"""Synapse Memory v7 — the definitive OpenWebUI memory engine."""
class Valves(BaseModel):
llm_model: str = Field(
default="llama3.2:latest",
description="Model used for all reasoning: extraction, selection, and intent detection.",
)
status_mode: Literal["silent", "brief", "detailed"] = Field(
default="brief",
description=(
"Status verbosity: "
"silent = no status updates, "
"brief = key milestones only, "
"detailed = step-by-step."
),
)
class UserValves(BaseModel):
enabled: bool = Field(
default=True,
description="Enable Synapse Memory for your account. Disable to stop all memory activity.",
)
show_status: bool = Field(
default=True,
description="Show memory status updates in the chat UI.",
)
def __init__(self) -> None:
self.valves = self.Valves()
self.circuit_breaker = CircuitBreaker()
logger.info("Synapse Memory v6.0 ready")
# =========================================================================
# PUBLIC HOOKS
# =========================================================================
async def inlet(
self,
body: Dict[str, Any],
__event_emitter__: Optional[Callable[[Any], Awaitable[None]]] = None,
__user__: Optional[Dict[str, Any]] = None,
__request__: Optional[Request] = None,
) -> Dict[str, Any]:
if not __user__ or not __user__.get("id"):
return body
user_valves = self._get_user_valves(__user__)
if not user_valves.enabled:
return body
if not user_valves.show_status:
__event_emitter__ = None # suppress all status updates for this user
messages = body.get("messages", [])
if not messages or messages[-1].get("role") != "user":
return body
user_message = (messages[-1].get("content") or "").strip()
if not user_message:
return body
user_id = __user__["id"]
# ------------------------------------------------------------------
# Intent classification — skip entirely if no memory keywords present
# ------------------------------------------------------------------
msg_lower = user_message.lower()
has_keyword = any(kw in msg_lower for kw in _INTENT_KEYWORDS)
if has_keyword:
intent_result = await self._classify_intent(
user_message, user_id, __request__, __user__
)
intent = intent_result.get("intent", "normal")
detail = intent_result.get("detail", "")
if intent == "clear_all":
return await self._handle_clear_all(
body, user_id, __request__, __event_emitter__, __user__
)
if intent == "forget":
return await self._handle_forget(
body, user_id, detail or user_message,
__request__, __event_emitter__, __user__,
)
if intent == "show_all":
return await self._handle_memory_query(
body, user_id, user_message, "show_all",
__request__, __event_emitter__, __user__,
)
if intent == "search":
return await self._handle_memory_query(
body, user_id, detail or user_message, "search",
__request__, __event_emitter__, __user__,
)
# ------------------------------------------------------------------
# Standard conversation: retrieve and inject relevant memories
# ------------------------------------------------------------------
memories = await self._fetch_user_memories(user_id)
if not memories:
return body
await self._emit_status(__event_emitter__, "🧠 Retrieving relevant memories…", "detailed")
conversation = self._format_conversation(messages, last_n=6)
selected = await self._select_relevant_memories(
conversation, memories, user_id, __request__, __user__
)
if not selected:
await self._emit_status(
__event_emitter__, "💤 No memories relevant to this turn", "detailed", done=True
)
return body
injection = self._format_memory_injection(selected)
if body.get("system"):
body["system"] = f"{injection}\n\n{body['system']}"
elif messages:
messages[0]["content"] = f"{injection}\n\n{messages[0]['content']}"
n = len(selected)
await self._emit_status(
__event_emitter__,
f"✅ {n} memory{'ies' if n != 1 else ''} injected",
"brief",
done=True,
)
return body
async def outlet(
self,
body: Dict[str, Any],
__event_emitter__: Optional[Callable[[Any], Awaitable[None]]] = None,
__user__: Optional[Dict[str, Any]] = None,
__request__: Optional[Request] = None,
) -> Dict[str, Any]:
if not __user__ or not __user__.get("id"):
return body
user_valves = self._get_user_valves(__user__)
if not user_valves.enabled:
return body
if not user_valves.show_status:
__event_emitter__ = None # suppress all status updates for this user
messages = body.get("messages", [])
if len(messages) < 2:
return body
conversation = self._format_conversation(messages, last_n=8)
if len(conversation.strip()) < 10:
return body
user_id = __user__["id"]
memories = await self._fetch_user_memories(user_id)
await self._emit_status(
__event_emitter__, "🧪 Evaluating conversation for memory operations…", "detailed"
)
ops = await self._extract_with_llm(
conversation, memories, user_id, __request__, __user__
)
if not ops:
await self._emit_status(
__event_emitter__, "💭 Nothing to remember from this turn", "detailed", done=True
)
return body
stats = {"created": 0, "deleted": 0, "merged": 0}
for op in ops:
action = (op.get("action") or "").upper()
if action == "DELETE":
target = op.get("target") or {}
tid = target.get("id")
if not tid:
continue
try:
await delete_memory_by_id(tid)
stats["deleted"] += 1
memories = [m for m in memories if m.get("id") != tid]
await self._emit_status(
__event_emitter__,
f"🗑️ Cleared: {target.get('content', '')[:70].strip()}",
"detailed",
)
except Exception as exc:
logger.error("Delete %s failed: %s", tid, exc)
elif action in {"NEW", "CREATE"}:
content = (op.get("content") or "").strip()
tags = op.get("tags") or []
if not content or self._is_too_short(content):
continue
if self._is_duplicate(content, memories):
await self._emit_status(
__event_emitter__, f"↪️ Already known: {content[:70]}", "detailed"
)
continue
if await self._store_memory(content, tags, __user__, __request__):
stats["created"] += 1
memories.append(self._build_memory_record(content, tags))
await self._emit_status(
__event_emitter__, f"✨ Stored: {content[:80]}", "detailed"
)
elif action == "UPDATE":
target = op.get("target") or {}
content = (op.get("content") or "").strip()
tags = op.get("tags") or []
tid = target.get("id")
if tid:
try:
await delete_memory_by_id(tid)
memories = [m for m in memories if m.get("id") != tid]
except Exception as exc:
logger.error("Update-delete %s failed: %s", tid, exc)
if content and not self._is_too_short(content) and not self._is_duplicate(content, memories):
if await self._store_memory(content, tags, __user__, __request__):
stats["created"] += 1
memories.append(self._build_memory_record(content, tags))
old_preview = target.get("content", "")[:40].strip()
await self._emit_status(
__event_emitter__,
f"🔄 Updated: {old_preview} → {content[:40]}",
"detailed",
)
elif action == "EXTENDS":
# Additive enrichment — replace old entry with enriched content
target = op.get("target") or {}
content = (op.get("content") or "").strip()
tags = op.get("tags") or []
tid = target.get("id")
if tid:
try:
await delete_memory_by_id(tid)
memories = [m for m in memories if m.get("id") != tid]
except Exception as exc:
logger.error("Extends-delete %s failed: %s", tid, exc)
if content and not self._is_too_short(content) and not self._is_duplicate(content, memories):
if await self._store_memory(content, tags, __user__, __request__):
stats["created"] += 1
memories.append(self._build_memory_record(content, tags))
old_preview = target.get("content", "")[:40].strip()
await self._emit_status(
__event_emitter__,
f"➕ Enriched: {old_preview} → {content[:40]}",
"detailed",
)
elif action == "MERGE":
sources = op.get("sources") or []
content = (op.get("content") or "").strip()
tags = op.get("tags") or []
if not content or self._is_too_short(content):
continue
deleted_count = 0
for src in sources:
src_id = (src or {}).get("id")
if not src_id:
continue
try:
await delete_memory_by_id(src_id)
memories = [m for m in memories if m.get("id") != src_id]
deleted_count += 1
except Exception as exc:
logger.error("Merge-delete %s failed: %s", src_id, exc)
if await self._store_memory(content, tags, __user__, __request__):
stats["merged"] += 1
memories.append(self._build_memory_record(content, tags))
await self._emit_status(
__event_emitter__,
f"🔗 Merged {deleted_count} → {content[:70]}",
"detailed",
)
parts = []
if stats["created"]:
parts.append(f"{stats['created']} stored")
if stats["deleted"]:
parts.append(f"{stats['deleted']} removed")
if stats["merged"]:
parts.append(f"{stats['merged']} merged")
if parts:
await self._emit_status(
__event_emitter__,
"💾 Memory vault: " + ", ".join(parts),
"brief",
done=True,
)
else:
await self._emit_status(
__event_emitter__, "✅ Memory vault unchanged", "brief", done=True
)
return body
# =========================================================================
# INTENT CLASSIFICATION
# =========================================================================
def _get_user_valves(self, __user__: Optional[Dict[str, Any]]) -> "Filter.UserValves":
"""Extract per-user valves from the __user__ context object."""
if __user__ is None:
return self.UserValves()
uv = __user__.get("valves")
if isinstance(uv, self.UserValves):
return uv
return self.UserValves()
async def _classify_intent(
self,
message: str,
user_id: str,
__request__: Optional[Request],
__user__: Optional[Dict[str, Any]],
) -> Dict[str, str]:
"""
Single LLM call that classifies the user's memory management intent
and extracts any relevant detail in one shot.
Returns a dict with:
intent — one of: normal, show_all, search, forget, clear_all
detail — search topic (for 'search') or forget description (for 'forget'), else ""
Called only after _INTENT_KEYWORDS pre-screen fires, so the vast
majority of normal conversational messages never reach this path.
"""
system = (
"Classify the user's intent regarding their personal memory vault.\n\n"
"Intents:\n"
" normal — Regular conversation, not asking about memories.\n"
" show_all — User wants to see all stored memories about them.\n"
" search — User wants to find memories matching a specific topic.\n"
" forget — User wants a specific memory or fact deleted.\n"
" clear_all — User wants ALL memories erased.\n\n"
"For 'search': set detail to the topic the user is searching for.\n"
"For 'forget': set detail to a concise description of what the user wants forgotten.\n"
"For all others: set detail to an empty string.\n\n"
'Return JSON only: {"intent": "<intent>", "detail": "<detail>"}'
)
response = await self._query_llm(
system, f"Message: {message}",
user_id, __request__, __user__,
temperature=0.0, max_tokens=60,
)
if not response:
return {"intent": "normal", "detail": ""}
try:
data = json.loads(response)
intent = str(data.get("intent", "normal")).lower().strip()
if intent not in {"normal", "show_all", "search", "forget", "clear_all"}:
intent = "normal"
return {"intent": intent, "detail": str(data.get("detail", "")).strip()}
except (json.JSONDecodeError, AttributeError):
return {"intent": "normal", "detail": ""}
# =========================================================================
# MEMORY QUERY HANDLERS
# =========================================================================
async def _handle_memory_query(
self,
body: Dict[str, Any],
user_id: str,
user_message: str,
intent: str,
__request__: Optional[Request],
__event_emitter__: Optional[Callable[[Any], Awaitable[None]]],
__user__: Optional[Dict[str, Any]],
) -> Dict[str, Any]:
memories = await self._fetch_user_memories(user_id)
if not memories:
summary = (
"📝 **Your Memory Vault**\n\n"
"I don't have any memories stored about you yet. "
"Just mention important details and I'll remember them automatically!"
)
elif intent == "search":
# user_message here is already the extracted topic from _classify_intent
summary = self._build_search_summary(memories, user_message)
else:
summary = self._build_vault_summary(memories)
messages = body.get("messages", [])
if body.get("system"):
body["system"] = f"{summary}\n\n{body['system']}"
elif messages:
messages[0]["content"] = f"{summary}\n\n{messages[0]['content']}"
await self._emit_status(
__event_emitter__, "📂 Memory vault details loaded", "brief", done=True
)
return body
async def _handle_forget(
self,
body: Dict[str, Any],
user_id: str,
forget_description: str,
__request__: Optional[Request],
__event_emitter__: Optional[Callable[[Any], Awaitable[None]]],
__user__: Optional[Dict[str, Any]],
) -> Dict[str, Any]:
memories = await self._fetch_user_memories(user_id)
if not memories:
await self._emit_status(
__event_emitter__, "📭 No memories to forget", "brief", done=True
)
return body
inv, index_map = self._build_memory_inventory(memories)
system = (
"The user wants to delete a specific memory. "
"Identify which stored memory best matches what they want forgotten.\n"
'Return JSON only: {"memory_number": <int or null>, "reason": "..."}'
)
user_p = f"What to forget: {forget_description}\n\nStored memories:\n{inv}"
response = await self._query_llm(
system, user_p, user_id, __request__, __user__, temperature=0.0, max_tokens=80
)
if response:
try:
data = json.loads(response)
num = data.get("memory_number")
if num is not None:
target = index_map.get(int(num))
tid = (target or {}).get("id")
if tid:
await delete_memory_by_id(tid)
snippet = (target or {}).get("content", "")[:80]
await self._emit_status(
__event_emitter__, f"🗑️ Forgotten: {snippet}", "brief", done=True
)
return body
except Exception as exc:
logger.error("Forget handler failed: %s", exc)
await self._emit_status(
__event_emitter__, "🤔 No matching memory found to forget", "brief", done=True
)
return body
async def _handle_clear_all(
self,
body: Dict[str, Any],
user_id: str,
__request__: Optional[Request],
__event_emitter__: Optional[Callable[[Any], Awaitable[None]]],
__user__: Optional[Dict[str, Any]],
) -> Dict[str, Any]:
memories = await self._fetch_user_memories(user_id)
deleted = 0
for mem in memories:
mid = mem.get("id")
if not mid:
continue
try:
await delete_memory_by_id(mid)
deleted += 1
except Exception as exc:
logger.error("Clear-all delete %s failed: %s", mid, exc)
msg = (
f"🗑️ Memory vault cleared ({deleted} memories removed)"
if deleted
else "📭 Memory vault was already empty"
)
await self._emit_status(__event_emitter__, msg, "brief", done=True)
return body
# =========================================================================
# VAULT DISPLAY HELPERS
# =========================================================================
def _build_search_summary(self, memories: List[Dict[str, Any]], topic: str) -> str:
"""Return a keyword-filtered memory summary matching the search topic."""
topic_words = set(re.split(r"\W+", topic.lower())) - {"", "a", "an", "the", "my", "i"}
scored: List[Tuple[int, Dict[str, Any]]] = []
for mem in memories:
content_words = set(re.split(r"\W+", mem.get("content", "").lower()))
tag_words = set(" ".join(mem.get("tags", [])).lower().split())
overlap = len(topic_words & (content_words | tag_words))
if overlap > 0:
scored.append((overlap, mem))
scored.sort(key=lambda x: x[0], reverse=True)
matched = [m for _, m in scored[:15]]
if not matched:
return f"📝 **Memory Search: '{topic}'**\n\nNo memories found matching that topic."
lines = [f"📝 **Memory Search: '{topic}'**\n", f"Found **{len(matched)}** relevant memories:\n"]
for i, mem in enumerate(matched, 1):
tags = mem.get("tags") or []
tag_str = f" _{', '.join(tags)}_" if tags else ""
lines.append(f"{i}. {mem.get('content', '')}{tag_str}")
return "\n".join(lines)
def _build_vault_summary(self, memories: List[Dict[str, Any]]) -> str:
"""Build a full categorised memory vault display."""
emoji_map = {
"identity": "👤",
"preference": "❤️",
"behavior": "🔁",
"goal": "🎯",
"relationship": "👥",
"possession": "🎁",
}
lines = [
"📝 **Your Memory Vault**\n",
f"**{len(memories)}** memories stored:\n",
]
category_map: Dict[str, List[str]] = {cat: [] for cat in TAG_CATEGORIES}
untagged: List[str] = []
for mem in memories:
content = mem.get("content", "")
placed = False
for tag in mem.get("tags") or []:
if tag in category_map:
category_map[tag].append(content)
placed = True
break
if not placed:
untagged.append(content)
for tag in TAG_CATEGORIES:
entries = category_map[tag]
if not entries:
continue
lines.append(f"**{emoji_map.get(tag, '📌')} {tag.capitalize()}**")
for entry in entries[:6]:
lines.append(f" • {entry}")
if len(entries) > 6:
lines.append(f" • _…and {len(entries) - 6} more_")
lines.append("")
if untagged:
lines.append("**📌 Other**")
for entry in untagged[:8]:
lines.append(f" • {entry}")
if len(untagged) > 8:
lines.append(f" • _…and {len(untagged) - 8} more_")
return "\n".join(lines)
# =========================================================================
# RETRIEVAL: SELECT RELEVANT MEMORIES
# =========================================================================
async def _select_relevant_memories(
self,
conversation: str,
memories: List[Dict[str, Any]],
user_id: str,
__request__: Optional[Request],
__user__: Optional[Dict[str, Any]],
) -> List[Dict[str, Any]]:
# ------------------------------------------------------------------
# Stage 1: vector search narrows the candidate pool when vault is large.
# Profile memories (identity/behavior) are always added to the candidates
# regardless of semantic score — they define who the user is.
# ------------------------------------------------------------------
profile_tag_set = {"identity", "behavior"}
profile_mems = [m for m in memories if set(m.get("tags") or []) & profile_tag_set]
if len(memories) > 15:
# Use the last user message as the vector query
query = ""
for line in reversed(conversation.split("\n")):
if line.startswith("User:"):
query = line[5:].strip()
break
query = query or conversation[-300:]
vector_hits = await self._query_memory_vector(
query, k=25, __user__=__user__, __request__=__request__
)
if vector_hits:
seen_ids: Set[str] = {str(m.get("id")) for m in vector_hits if m.get("id")}
candidates = list(vector_hits)
for pm in profile_mems:
if str(pm.get("id")) not in seen_ids:
candidates.append(pm)
seen_ids.add(str(pm.get("id")))
else:
# Vector search unavailable — fall back to full list
candidates = memories[:MAX_MEMORY_CONTEXT]
else:
candidates = memories[:MAX_MEMORY_CONTEXT]
if not candidates:
return []
# ------------------------------------------------------------------
# Stage 2: LLM reranking over the narrowed candidate set
# ------------------------------------------------------------------
inv, index_map = self._build_memory_inventory(candidates)
system = f"""You curate memory context for a conversational AI assistant.
Select the stored memories that would help the assistant give a more personalised, accurate response.
Rules:
1. Only include memories DIRECTLY relevant to the current conversation topic or user intent.
2. Always include identity/behavior memories that describe who the user fundamentally is.
3. Select at most {MAX_INJECTED_MEMORIES} memories. Quality over quantity.
4. Score each 0.0–1.0. Only include scores above 0.50.
5. Do NOT select multiple memories that convey the same information — pick the most specific or recent.
6. Keep reasons brief (under 60 chars).
7. Return an empty list if nothing would meaningfully help.
Respond with valid JSON only:
{{"selected": [{{"number": 3, "score": 0.91, "reason": "User is asking about Python"}}, ...]}}"""
user_p = f"Conversation:\n{conversation}\n\nStored memories:\n{inv}"
response = await self._query_llm(
system, user_p, user_id, __request__, __user__, temperature=0.1, max_tokens=600
)
if not response:
return []
try:
data = json.loads(response)
except json.JSONDecodeError:
logger.warning("Memory selection: could not parse LLM JSON")
return []
selected: List[Dict[str, Any]] = []
seen: Set[str] = set()
for entry in data.get("selected", []):
num = entry.get("number")
if num is None:
continue
record = index_map.get(int(num))
if not record:
continue
uid = str(record.get("id") or record.get("content", ""))
if uid in seen:
continue
seen.add(uid)
llm_score = float(entry.get("score", 0.0))
if llm_score < 0.50:
continue
# Stage 3: recency-weighted final score
recency = self._recency_score(record)
final_score = (llm_score * 0.7) + (recency * 0.3)
selected.append({
"record": record,
"score": final_score,
"reason": (entry.get("reason") or "").strip(),
})
selected.sort(key=lambda x: x["score"], reverse=True)
return selected[:MAX_INJECTED_MEMORIES]
# =========================================================================
# EXTRACTION: IDENTIFY MEMORY OPERATIONS
# =========================================================================
async def _extract_with_llm(
self,
conversation: str,
memories: List[Dict[str, Any]],
user_id: str,
__request__: Optional[Request],
__user__: Optional[Dict[str, Any]],
) -> List[Dict[str, Any]]:
limited = memories[:MAX_MEMORY_CONTEXT]
inv, index_map = self._build_memory_inventory(limited)
now_str = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")
system = f"""You manage a personal memory vault for a user.
Current time: {now_str}
Decide what to add, correct, enrich, remove, or consolidate based on this conversation.
Operations (use only when genuinely warranted):
NEW — Store a durable, user-specific fact not already present.
DELETE — Remove an entry (by number) confirmed incorrect or explicitly asked to forget.
UPDATE — Replace an entry (by number) with corrected content (fact changed or was wrong).
EXTENDS — Enrich an entry (by number) with new details that ADD TO it without contradicting it.
MERGE — Combine 2–4 closely related entries (by number list) into one cleaner fact.
Guidelines:
• Skip anything already stored verbatim or near-verbatim.
• Skip transient details: greetings, small-talk, one-off questions, general knowledge.
• Keep each memory concise (under 200 characters) and factual.
• If a new fact CONTRADICTS a stored one → DELETE old + NEW corrected one.
• If a new fact ADDS TO a stored one without contradicting it → use EXTENDS.
• Use tags when helpful: identity, preference, behavior, goal, relationship, possession.
• If 2+ memories cover the same narrow topic, prefer MERGE over adding more fragments.
Return ONLY valid JSON — no prose, no markdown fences:
{{
"operations": [
{{"action": "NEW", "content": "...", "tags": ["preference"]}},
{{"action": "DELETE", "memory_number": 3, "reason": "User corrected job title"}},
{{"action": "UPDATE", "memory_number": 6, "content": "...", "tags": ["identity"]}},
{{"action": "EXTENDS", "memory_number": 4, "content": "...", "tags": ["identity"]}},
{{"action": "MERGE", "sources": [2, 5], "content": "...", "tags": ["preference"]}}
]
}}"""
user_p = f"Conversation:\n{conversation}\n\nExisting memories:\n{inv}"
response = await self._query_llm(
system, user_p, user_id, __request__, __user__, temperature=0.1, max_tokens=1000
)
if not response:
return []
try:
data = json.loads(response)
except json.JSONDecodeError:
logger.warning("Extraction: could not parse LLM JSON")
return []
sanitized: List[Dict[str, Any]] = []
for op in data.get("operations", []):
action = (op.get("action") or "").upper()
if action == "DELETE":
target = self._resolve_by_number(op.get("memory_number"), index_map)
if target:
sanitized.append({"action": "DELETE", "target": target})
elif action in {"NEW", "CREATE"}:
content = (op.get("content") or "").strip()
if not content or self._is_too_short(content):
continue
tags = self._sanitize_tags(op.get("tags"))
sanitized.append({"action": "NEW", "content": content, "tags": tags})
elif action == "UPDATE":
content = (op.get("content") or "").strip()
if not content or self._is_too_short(content):
continue
target = self._resolve_by_number(op.get("memory_number"), index_map)
tags = self._sanitize_tags(op.get("tags"))
sanitized.append({"action": "UPDATE", "content": content, "tags": tags, "target": target or {}})
elif action == "EXTENDS":
# Additive enrichment: replace old with enriched content (same as UPDATE mechanically)
content = (op.get("content") or "").strip()
if not content or self._is_too_short(content):
continue
target = self._resolve_by_number(op.get("memory_number"), index_map)
tags = self._sanitize_tags(op.get("tags"))
sanitized.append({"action": "EXTENDS", "content": content, "tags": tags, "target": target or {}})
elif action == "MERGE":
content = (op.get("content") or "").strip()
if not content or self._is_too_short(content):
continue
source_nums = op.get("sources") or []
source_targets = [
t for num in source_nums
if (t := self._resolve_by_number(num, index_map)) is not None
]
if not source_targets:
continue
tags = self._sanitize_tags(op.get("tags"))
sanitized.append({"action": "MERGE", "content": content, "tags": tags, "sources": source_targets})
return sanitized
# =========================================================================
# LLM INTERFACE
# =========================================================================
async def _query_llm(
self,
system_prompt: str,
user_prompt: str,
user_id: str,
__request__: Optional[Request],
__user__: Optional[Dict[str, Any]],
temperature: float = 0.1,
max_tokens: int = 200,
) -> Optional[str]:
try:
return await self.circuit_breaker.call(
self._execute_llm,
system_prompt, user_prompt,
__request__, __user__,
temperature, max_tokens,
)
except CircuitBreakerOpenError:
logger.warning("LLM skipped — circuit breaker OPEN")
return None
except Exception as exc:
logger.error("LLM query failed: %s", exc)
return None
async def _execute_llm(
self,
system_prompt: str,
user_prompt: str,
__request__: Optional[Request],
__user__: Optional[Dict[str, Any]],
temperature: float,
max_tokens: int,
) -> Optional[str]:
request_obj = __request__ or Request(scope={"type": "http", "app": webui_app})
response = await generate_chat_completion(
request_obj,
{
"model": self.valves.llm_model,
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
"stream": False,
"temperature": temperature,
"max_tokens": max_tokens,
},
user=__user__,
)
raw = response["choices"][0]["message"]["content"]
return self._extract_json(raw)
# =========================================================================
# MEMORY I/O
# =========================================================================
async def _fetch_user_memories(self, user_id: str) -> List[Dict[str, Any]]:
def _load() -> List[Any]:
return list(Memories.get_memories_by_user_id(user_id) or [])
records = await asyncio.to_thread(_load)
snapshot: List[Dict[str, Any]] = []
for item in records:
metadata = getattr(item, "metadata", {}) or {}
created_at = getattr(item, "created_at", None)
created_iso: Optional[str] = None
timestamp = 0.0
if isinstance(created_at, datetime):
created_iso = created_at.astimezone(timezone.utc).isoformat()
timestamp = created_at.timestamp()
elif isinstance(created_at, str):
created_iso = created_at
try:
timestamp = datetime.fromisoformat(
created_at.replace("Z", "+00:00")
).timestamp()
except ValueError:
pass
snapshot.append({
"id": getattr(item, "id", None),
"content": (getattr(item, "content", "") or "").strip(),
"metadata": metadata,