-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcognitive_memory_api.py
More file actions
1026 lines (897 loc) · 37.8 KB
/
cognitive_memory_api.py
File metadata and controls
1026 lines (897 loc) · 37.8 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
"""
Cognitive Memory API
Thin async client for the Postgres-backed cognitive memory system.
Design:
- The database owns state and behavior (functions/views in schema.sql).
- This module is a convenience layer for application integration.
"""
from __future__ import annotations
import asyncio
from contextlib import asynccontextmanager
from dataclasses import dataclass
from datetime import datetime, timezone
from enum import Enum
from typing import Any, AsyncIterator, Iterable, Optional
from uuid import UUID
import asyncpg
from prompt_resources import compose_personhood_prompt
class MemoryType(str, Enum):
EPISODIC = "episodic"
SEMANTIC = "semantic"
PROCEDURAL = "procedural"
STRATEGIC = "strategic"
class GoalPriority(str, Enum):
ACTIVE = "active"
QUEUED = "queued"
BACKBURNER = "backburner"
COMPLETED = "completed"
ABANDONED = "abandoned"
class GoalSource(str, Enum):
CURIOSITY = "curiosity"
USER_REQUEST = "user_request"
IDENTITY = "identity"
DERIVED = "derived"
EXTERNAL = "external"
class RelationshipType(str, Enum):
TEMPORAL_NEXT = "TEMPORAL_NEXT"
CAUSES = "CAUSES"
DERIVED_FROM = "DERIVED_FROM"
CONTRADICTS = "CONTRADICTS"
SUPPORTS = "SUPPORTS"
INSTANCE_OF = "INSTANCE_OF"
PARENT_OF = "PARENT_OF"
ASSOCIATED = "ASSOCIATED"
@dataclass(frozen=True)
class Memory:
id: UUID
type: MemoryType
content: str
importance: float
relevance_score: float | None = None
similarity: float | None = None
source: str | None = None # retrieval source: 'vector', 'association', 'temporal'
trust_level: float | None = None # epistemic trust [0..1] (DB-computed)
source_attribution: dict[str, Any] | None = None # primary provenance (DB-stored JSON)
created_at: datetime | None = None
emotional_valence: float | None = None
@dataclass(frozen=True)
class PartialActivation:
cluster_id: UUID
cluster_name: str
keywords: list[str]
emotional_signature: dict[str, Any] | None
cluster_similarity: float
best_memory_similarity: float
@dataclass(frozen=True)
class RecallResult:
memories: list[Memory]
partial_activations: list[PartialActivation]
query: str
@dataclass(frozen=True)
class HydratedContext:
memories: list[Memory]
partial_activations: list[PartialActivation]
identity: list[dict[str, Any]]
worldview: list[dict[str, Any]]
emotional_state: dict[str, Any] | None
goals: dict[str, Any] | None
urgent_drives: list[dict[str, Any]]
@dataclass(frozen=True)
class MemoryInput:
content: str
type: MemoryType = MemoryType.EPISODIC
importance: float = 0.5
emotional_valence: float = 0.0
context: dict[str, Any] | None = None
concepts: list[str] | None = None
source_attribution: dict[str, Any] | None = None
source_references: Any | None = None # JSONB for semantic memories (dict or list[dict])
trust_level: float | None = None
@dataclass(frozen=True)
class RelationshipInput:
from_id: UUID
to_id: UUID
relationship_type: RelationshipType
confidence: float = 0.8
context: str | None = None
async def _init_connection(conn: asyncpg.Connection) -> None:
try:
await conn.execute("LOAD 'age';")
except Exception:
pass
try:
await conn.execute("SET search_path = ag_catalog, public;")
except Exception:
pass
def _to_jsonb_arg(val: Any) -> Any:
if val is None:
return None
if isinstance(val, (dict, list)):
import json
return json.dumps(val)
return val
def _cypher_escape(value: str) -> str:
return value.replace("'", "''")
class CognitiveMemory:
"""
Async client for the cognitive memory database.
Two common flows:
- RAG hydration: `hydrate()`
- Agent operations: `recall()`, `remember()`, `connect_memories()`
"""
def __init__(self, pool: asyncpg.Pool):
self._pool = pool
@classmethod
@asynccontextmanager
async def connect(
cls,
dsn: str,
**pool_kwargs: Any,
) -> AsyncIterator["CognitiveMemory"]:
"""
Async context manager that owns the underlying pool.
Usage:
async with CognitiveMemory.connect(dsn) as mem:
ctx = await mem.hydrate("...")
"""
pool = await asyncpg.create_pool(dsn, init=_init_connection, **pool_kwargs)
client = cls(pool)
try:
yield client
finally:
await pool.close()
@classmethod
async def create(cls, dsn: str, **pool_kwargs: Any) -> "CognitiveMemory":
"""Create a pool and return a client; call `close()` when done."""
pool = await asyncpg.create_pool(dsn, init=_init_connection, **pool_kwargs)
return cls(pool)
async def close(self) -> None:
await self._pool.close()
# =========================================================================
# RAG: HYDRATION
# =========================================================================
async def hydrate(
self,
query: str,
*,
memory_limit: int = 10,
include_partial: bool = True,
include_identity: bool = True,
include_worldview: bool = True,
include_emotional_state: bool = True,
include_goals: bool = False,
include_drives: bool = True,
) -> HydratedContext:
"""
Hydrate a query with relevant context for RAG prompt augmentation.
This uses:
- `fast_recall(query, limit)` for relevant memories
- `find_partial_activations(query)` for tip-of-tongue clusters (optional)
- `gather_turn_context()` for identity/worldview/emotions/drives/goals (optional subsets)
"""
async with self._pool.acquire() as conn:
memories = await self._recall_memories(conn, query, memory_limit)
partial = await self._find_partial_activations(conn, query) if include_partial else []
ctx_row = await conn.fetchval("SELECT gather_turn_context()")
ctx = _coerce_json(ctx_row)
identity = ctx.get("identity", []) if include_identity else []
worldview = ctx.get("worldview", []) if include_worldview else []
emotional_state = ctx.get("emotional_state") if include_emotional_state else None
goals = ctx.get("goals") if include_goals else None
urgent_drives = ctx.get("urgent_drives", []) if include_drives else []
return HydratedContext(
memories=memories,
partial_activations=partial,
identity=list(identity) if isinstance(identity, list) else [],
worldview=list(worldview) if isinstance(worldview, list) else [],
emotional_state=dict(emotional_state) if isinstance(emotional_state, dict) else None,
goals=dict(goals) if isinstance(goals, dict) else None,
urgent_drives=list(urgent_drives) if isinstance(urgent_drives, list) else [],
)
async def hydrate_batch(
self,
queries: list[str],
*,
max_concurrency: int = 5,
**kwargs: Any,
) -> list[HydratedContext]:
"""
Hydrate multiple queries concurrently (pool-backed).
Note: `asyncpg.Connection` cannot run concurrent queries, so batching here
means concurrent hydrations across pooled connections.
"""
sem = asyncio.Semaphore(max(1, max_concurrency))
async def _one(q: str) -> HydratedContext:
async with sem:
return await self.hydrate(q, **kwargs)
return list(await asyncio.gather(*[_one(q) for q in queries]))
# =========================================================================
# RECALL
# =========================================================================
async def recall(
self,
query: str,
*,
limit: int = 10,
memory_types: list[MemoryType] | None = None,
min_importance: float = 0.0,
include_partial: bool = True,
) -> RecallResult:
async with self._pool.acquire() as conn:
memories = await self._recall_memories(
conn,
query,
limit,
memory_types=memory_types,
min_importance=min_importance,
)
partial = await self._find_partial_activations(conn, query) if include_partial else []
return RecallResult(memories=memories, partial_activations=partial, query=query)
async def recall_by_id(self, memory_id: UUID) -> Memory | None:
async with self._pool.acquire() as conn:
row = await conn.fetchrow(
"""
SELECT
m.id,
m.type,
m.content,
m.importance,
m.trust_level,
m.source_attribution,
m.created_at,
em.emotional_valence
FROM memories m
LEFT JOIN episodic_memories em ON m.id = em.memory_id
WHERE m.id = $1
""",
memory_id,
)
if not row:
return None
return Memory(
id=row["id"],
type=MemoryType(row["type"]),
content=row["content"],
importance=float(row["importance"]),
trust_level=float(row["trust_level"]) if row["trust_level"] is not None else None,
source_attribution=_coerce_json(row["source_attribution"]) if row["source_attribution"] is not None else None,
created_at=row["created_at"],
emotional_valence=row["emotional_valence"],
)
async def recall_recent(
self,
*,
limit: int = 10,
memory_type: MemoryType | None = None,
) -> list[Memory]:
async with self._pool.acquire() as conn:
if memory_type is None:
rows = await conn.fetch(
"""
SELECT
m.id,
m.type,
m.content,
m.importance,
m.trust_level,
m.source_attribution,
m.created_at,
em.emotional_valence
FROM memories m
LEFT JOIN episodic_memories em ON m.id = em.memory_id
WHERE m.status = 'active'
ORDER BY m.created_at DESC
LIMIT $1
""",
limit,
)
else:
rows = await conn.fetch(
"""
SELECT
m.id,
m.type,
m.content,
m.importance,
m.trust_level,
m.source_attribution,
m.created_at,
em.emotional_valence
FROM memories m
LEFT JOIN episodic_memories em ON m.id = em.memory_id
WHERE m.status = 'active' AND m.type = $2::memory_type
ORDER BY m.created_at DESC
LIMIT $1
""",
limit,
memory_type.value,
)
return [self._row_to_memory(row) for row in rows]
# =========================================================================
# REMEMBER
# =========================================================================
async def remember(
self,
content: str,
*,
type: MemoryType = MemoryType.EPISODIC,
importance: float = 0.5,
emotional_valence: float = 0.0,
context: dict[str, Any] | None = None,
concepts: list[str] | None = None,
source_attribution: dict[str, Any] | None = None,
source_references: Any | None = None,
trust_level: float | None = None,
) -> UUID:
async with self._pool.acquire() as conn:
memory_id = await self._create_memory(
conn,
content,
type,
importance,
emotional_valence,
context,
source_attribution=source_attribution,
source_references=source_references,
trust_level=trust_level,
)
if concepts:
for concept in concepts:
await conn.fetchval("SELECT link_memory_to_concept($1::uuid, $2::text, 1.0)", memory_id, concept)
return memory_id
async def add_source(self, memory_id: UUID, source: dict[str, Any]) -> None:
"""Attach an additional source reference to a semantic memory and recompute trust."""
async with self._pool.acquire() as conn:
await conn.execute(
"SELECT add_semantic_source_reference($1::uuid, $2::jsonb)",
memory_id,
_to_jsonb_arg(source),
)
async def get_truth_profile(self, memory_id: UUID) -> dict[str, Any]:
"""Return DB-computed provenance/trust details for a memory."""
async with self._pool.acquire() as conn:
row = await conn.fetchrow("SELECT get_memory_truth_profile($1::uuid) AS profile", memory_id)
if not row or row["profile"] is None:
return {}
return dict(_coerce_json(row["profile"]))
async def remember_batch(self, memories: Iterable[MemoryInput]) -> list[UUID]:
async with self._pool.acquire() as conn:
items: list[dict[str, Any]] = []
mem_list = list(memories)
for m in mem_list:
item: dict[str, Any] = {"type": m.type.value, "content": m.content, "importance": m.importance}
if m.source_attribution is not None:
item["source_attribution"] = m.source_attribution
if m.trust_level is not None:
item["trust_level"] = m.trust_level
if m.type == MemoryType.EPISODIC:
item["context"] = m.context
item["emotional_valence"] = m.emotional_valence
elif m.type == MemoryType.SEMANTIC:
item["source_references"] = m.source_references if m.source_references is not None else m.context
elif m.type == MemoryType.PROCEDURAL:
item["steps"] = m.context if m.context is not None else {"steps": []}
elif m.type == MemoryType.STRATEGIC:
item["supporting_evidence"] = m.context
items.append(item)
import json
created = await conn.fetchval("SELECT batch_create_memories($1::jsonb)", json.dumps(items))
ids = list(created or [])
# Link concepts (still per-memory).
for mid, m in zip(ids, mem_list):
if m.concepts:
for concept in m.concepts:
await conn.fetchval("SELECT link_memory_to_concept($1::uuid, $2::text, 1.0)", mid, concept)
return ids
async def remember_batch_raw(
self,
contents: list[str],
embeddings: list[list[float]],
*,
type: MemoryType = MemoryType.EPISODIC,
importance: float = 0.5,
) -> list[UUID]:
"""
Insert memories with pre-computed embeddings (bypasses get_embedding()).
Notes:
- Graph nodes are created to keep AGE state consistent.
- Embedding dimension must match the DB typmod.
"""
if len(contents) != len(embeddings):
raise ValueError("contents and embeddings must have same length")
async with self._pool.acquire() as conn:
expected_dim = int(await conn.fetchval("SELECT embedding_dimension()"))
for embedding in embeddings:
if len(embedding) != expected_dim:
raise ValueError(f"embedding dimension mismatch: expected {expected_dim}, got {len(embedding)}")
created = await conn.fetchval(
"""
SELECT batch_create_memories_with_embeddings(
$1::memory_type,
$2::text[],
$3::jsonb,
$4::float
)
""",
type.value,
contents,
_to_jsonb_arg(embeddings),
float(importance),
)
return list(created or [])
async def touch_memories(self, memory_ids: Iterable[UUID]) -> int:
"""Increment access_count/last_accessed for the given memory ids."""
ids = list(memory_ids)
if not ids:
return 0
async with self._pool.acquire() as conn:
n = await conn.execute(
"""
UPDATE memories
SET access_count = access_count + 1,
last_accessed = CURRENT_TIMESTAMP
WHERE id = ANY($1::uuid[])
""",
ids,
)
# asyncpg returns "UPDATE <count>"
try:
return int(str(n).split()[-1])
except Exception:
return 0
# =========================================================================
# GRAPH / RELATIONSHIPS
# =========================================================================
async def connect_memories(
self,
from_id: UUID,
to_id: UUID,
relationship: RelationshipType,
*,
confidence: float = 0.8,
context: str | None = None,
) -> None:
async with self._pool.acquire() as conn:
await conn.execute(
"""
SELECT discover_relationship($1::uuid, $2::uuid, $3::graph_edge_type, $4::float, 'api', NULL, $5::text)
""",
from_id,
to_id,
relationship.value,
confidence,
context,
)
async def connect_batch(self, relationships: Iterable[RelationshipInput]) -> None:
async with self._pool.acquire() as conn:
for r in relationships:
await conn.execute(
"""
SELECT discover_relationship($1::uuid, $2::uuid, $3::graph_edge_type, $4::float, 'api', NULL, $5::text)
""",
r.from_id,
r.to_id,
r.relationship_type.value,
r.confidence,
r.context,
)
async def find_causes(self, memory_id: UUID, *, depth: int = 3) -> list[dict[str, Any]]:
async with self._pool.acquire() as conn:
rows = await conn.fetch("SELECT * FROM find_causal_chain($1::uuid, $2::int)", memory_id, depth)
return [dict(row) for row in rows]
async def find_contradictions(self, memory_id: UUID | None = None) -> list[dict[str, Any]]:
async with self._pool.acquire() as conn:
rows = await conn.fetch("SELECT * FROM find_contradictions($1::uuid)", memory_id)
return [dict(row) for row in rows]
async def find_supporting_evidence(self, worldview_id: UUID) -> list[dict[str, Any]]:
async with self._pool.acquire() as conn:
rows = await conn.fetch("SELECT * FROM find_supporting_evidence($1::uuid)", worldview_id)
return [dict(row) for row in rows]
# =========================================================================
# CONCEPTS
# =========================================================================
async def link_concept(self, memory_id: UUID, concept: str, *, strength: float = 1.0) -> UUID:
async with self._pool.acquire() as conn:
return await conn.fetchval("SELECT link_memory_to_concept($1::uuid, $2::text, $3::float)", memory_id, concept, strength)
async def find_by_concept(self, concept: str, *, limit: int = 10) -> list[Memory]:
async with self._pool.acquire() as conn:
rows = await conn.fetch(
"""
SELECT m.id, m.type, m.content, m.importance, m.created_at, em.emotional_valence
FROM memories m
JOIN memory_concepts mc ON m.id = mc.memory_id
JOIN concepts c ON mc.concept_id = c.id
LEFT JOIN episodic_memories em ON m.id = em.memory_id
WHERE c.name = $1 AND m.status = 'active'
ORDER BY mc.strength DESC, m.importance DESC
LIMIT $2
""",
concept,
limit,
)
return [self._row_to_memory(row) for row in rows]
# =========================================================================
# WORKING MEMORY
# =========================================================================
async def hold(self, content: str, *, ttl_seconds: int = 3600) -> UUID:
async with self._pool.acquire() as conn:
return await conn.fetchval(
"SELECT add_to_working_memory($1::text, $2::int * interval '1 second')",
content,
ttl_seconds,
)
async def search_working(self, query: str, *, limit: int = 5) -> list[dict[str, Any]]:
async with self._pool.acquire() as conn:
rows = await conn.fetch("SELECT * FROM search_working_memory($1::text, $2::int)", query, limit)
return [dict(row) for row in rows]
# =========================================================================
# STATE / INTROSPECTION
# =========================================================================
async def get_emotional_state(self) -> dict[str, Any] | None:
async with self._pool.acquire() as conn:
row = await conn.fetchrow("SELECT * FROM current_emotional_state")
return dict(row) if row else None
async def get_drives(self) -> list[dict[str, Any]]:
async with self._pool.acquire() as conn:
rows = await conn.fetch("SELECT * FROM drive_status")
return [dict(row) for row in rows]
async def get_health(self) -> dict[str, Any]:
async with self._pool.acquire() as conn:
row = await conn.fetchrow("SELECT * FROM cognitive_health")
return dict(row) if row else {}
async def get_identity(self) -> list[dict[str, Any]]:
async with self._pool.acquire() as conn:
rows = await conn.fetch(
"""
SELECT aspect_type, content, stability
FROM identity_aspects
WHERE stability > 0.3
ORDER BY stability DESC
LIMIT 5
"""
)
return [dict(row) for row in rows]
async def get_worldview(self) -> list[dict[str, Any]]:
async with self._pool.acquire() as conn:
rows = await conn.fetch(
"""
SELECT category, belief, confidence
FROM worldview_primitives
WHERE confidence > 0.5
ORDER BY confidence DESC
LIMIT 5
"""
)
return [dict(row) for row in rows]
async def get_goals(self, *, priority: GoalPriority | None = None) -> list[dict[str, Any]]:
async with self._pool.acquire() as conn:
if priority is None:
rows = await conn.fetch(
"""
SELECT * FROM goals
WHERE priority IN ('active', 'queued')
ORDER BY priority, last_touched DESC
"""
)
else:
rows = await conn.fetch(
"SELECT * FROM goals WHERE priority = $1::goal_priority ORDER BY last_touched DESC",
priority.value,
)
return [dict(row) for row in rows]
async def create_goal(
self,
title: str,
*,
description: str | None = None,
source: GoalSource | str = GoalSource.USER_REQUEST,
priority: GoalPriority | str = GoalPriority.QUEUED,
parent_id: UUID | None = None,
due_at: datetime | None = None,
) -> UUID:
async with self._pool.acquire() as conn:
return await conn.fetchval(
"""
SELECT create_goal(
$1,
$2,
$3::goal_source,
$4::goal_priority,
$5::uuid,
$6::timestamptz
)
""",
title,
description,
(source.value if isinstance(source, GoalSource) else str(source)),
(priority.value if isinstance(priority, GoalPriority) else str(priority)),
parent_id,
due_at,
)
async def queue_user_message(
self,
message: str,
*,
intent: str | None = None,
context: dict[str, Any] | None = None,
) -> UUID:
async with self._pool.acquire() as conn:
return await conn.fetchval(
"SELECT queue_user_message($1, $2, $3::jsonb)",
message,
intent,
_to_jsonb_arg(context or {}),
)
async def get_ingestion_receipts(self, source_file: str, content_hashes: list[str]) -> dict[str, UUID]:
async with self._pool.acquire() as conn:
rows = await conn.fetch(
"SELECT * FROM get_ingestion_receipts($1::text, $2::text[])",
source_file,
content_hashes,
)
out: dict[str, UUID] = {}
for r in rows:
try:
out[str(r["content_hash"])] = UUID(str(r["memory_id"]))
except Exception:
continue
return out
async def record_ingestion_receipts(self, items: list[dict[str, Any]]) -> int:
async with self._pool.acquire() as conn:
import json
return int(
await conn.fetchval(
"SELECT record_ingestion_receipts($1::jsonb)",
json.dumps(items),
)
or 0
)
# =========================================================================
# INTERNALS
# =========================================================================
async def _create_memory(
self,
conn: asyncpg.Connection,
content: str,
type: MemoryType,
importance: float,
emotional_valence: float,
context: dict[str, Any] | None,
*,
source_attribution: dict[str, Any] | None = None,
source_references: Any | None = None,
trust_level: float | None = None,
) -> UUID:
if type == MemoryType.EPISODIC:
return await conn.fetchval(
"SELECT create_episodic_memory($1::text, NULL, $2::jsonb, NULL, $3::float, CURRENT_TIMESTAMP, $4::float, $5::jsonb, $6::float)",
content,
_to_jsonb_arg(context),
emotional_valence,
importance,
_to_jsonb_arg(source_attribution),
trust_level,
)
if type == MemoryType.SEMANTIC:
sources = source_references if source_references is not None else context
return await conn.fetchval(
"SELECT create_semantic_memory($1::text, 0.8::float, NULL, NULL, $2::jsonb, $3::float, $4::jsonb, $5::float)",
content,
_to_jsonb_arg(sources),
importance,
_to_jsonb_arg(source_attribution),
trust_level,
)
if type == MemoryType.PROCEDURAL:
steps = context if context is not None else {}
return await conn.fetchval(
"SELECT create_procedural_memory($1::text, $2::jsonb, NULL, $3::float, $4::jsonb, $5::float)",
content,
_to_jsonb_arg(steps),
importance,
_to_jsonb_arg(source_attribution),
trust_level,
)
if type == MemoryType.STRATEGIC:
return await conn.fetchval(
"SELECT create_strategic_memory($1::text, $2::text, 0.8::float, $3::jsonb, NULL, $4::float, $5::jsonb, $6::float)",
content,
content,
_to_jsonb_arg(context),
importance,
_to_jsonb_arg(source_attribution),
trust_level,
)
raise ValueError(f"Unknown memory type: {type}")
async def _recall_memories(
self,
conn: asyncpg.Connection,
query: str,
limit: int,
memory_types: list[MemoryType] | None = None,
min_importance: float = 0.0,
) -> list[Memory]:
rows = await conn.fetch(
"""
SELECT
fr.memory_id,
fr.content,
fr.memory_type,
fr.score,
fr.source,
m.importance,
m.trust_level,
m.source_attribution,
m.created_at,
em.emotional_valence
FROM fast_recall($1::text, $2::int) fr
JOIN memories m ON m.id = fr.memory_id
LEFT JOIN episodic_memories em ON em.memory_id = fr.memory_id
WHERE m.importance >= $3::float
""",
query,
limit,
min_importance,
)
memories: list[Memory] = []
for row in rows:
mt = MemoryType(row["memory_type"])
if memory_types is not None and mt not in set(memory_types):
continue
memories.append(
Memory(
id=row["memory_id"],
type=mt,
content=row["content"],
importance=float(row["importance"]),
similarity=float(row["score"]),
source=row["source"],
trust_level=float(row["trust_level"]) if row["trust_level"] is not None else None,
source_attribution=_coerce_json(row["source_attribution"]) if row["source_attribution"] is not None else None,
created_at=row["created_at"],
emotional_valence=row["emotional_valence"],
)
)
return memories
async def _find_partial_activations(self, conn: asyncpg.Connection, query: str) -> list[PartialActivation]:
rows = await conn.fetch("SELECT * FROM find_partial_activations($1::text)", query)
out: list[PartialActivation] = []
for row in rows:
out.append(
PartialActivation(
cluster_id=row["cluster_id"],
cluster_name=row["cluster_name"],
keywords=list(row["keywords"] or []),
emotional_signature=_coerce_json(row["emotional_signature"]) if row["emotional_signature"] is not None else None,
cluster_similarity=float(row["cluster_similarity"]),
best_memory_similarity=float(row["best_memory_similarity"]),
)
)
return out
def _row_to_memory(self, row: asyncpg.Record) -> Memory:
return Memory(
id=row["id"],
type=MemoryType(row["type"]),
content=row["content"],
importance=float(row["importance"]),
trust_level=float(row["trust_level"]) if "trust_level" in row and row["trust_level"] is not None else None,
source_attribution=_coerce_json(row["source_attribution"])
if "source_attribution" in row and row["source_attribution"] is not None
else None,
created_at=row["created_at"] if "created_at" in row else None,
emotional_valence=row["emotional_valence"] if "emotional_valence" in row else None,
)
class CognitiveMemorySync:
"""Synchronous wrapper around CognitiveMemory for non-async call sites."""
def __init__(self, async_client: CognitiveMemory, loop: asyncio.AbstractEventLoop):
self._async = async_client
self._loop = loop
@classmethod
def connect(cls, dsn: str, **kwargs: Any) -> "CognitiveMemorySync":
loop = asyncio.new_event_loop()
try:
client = loop.run_until_complete(CognitiveMemory.create(dsn, **kwargs))
except Exception:
loop.close()
raise
return cls(client, loop)
def close(self) -> None:
self._loop.run_until_complete(self._async.close())
self._loop.close()
def hydrate(self, query: str, **kwargs: Any) -> HydratedContext:
return self._loop.run_until_complete(self._async.hydrate(query, **kwargs))
def recall(self, query: str, **kwargs: Any) -> RecallResult:
return self._loop.run_until_complete(self._async.recall(query, **kwargs))
def remember(self, content: str, **kwargs: Any) -> UUID:
return self._loop.run_until_complete(self._async.remember(content, **kwargs))
def remember_batch(self, memories: Iterable[MemoryInput]) -> list[UUID]:
return self._loop.run_until_complete(self._async.remember_batch(memories))
def remember_batch_raw(self, contents: list[str], embeddings: list[list[float]], **kwargs: Any) -> list[UUID]:
return self._loop.run_until_complete(self._async.remember_batch_raw(contents, embeddings, **kwargs))
def connect_memories(self, from_id: UUID, to_id: UUID, relationship: RelationshipType, **kwargs: Any) -> None:
return self._loop.run_until_complete(self._async.connect_memories(from_id, to_id, relationship, **kwargs))
def touch_memories(self, memory_ids: Iterable[UUID]) -> int:
return self._loop.run_until_complete(self._async.touch_memories(memory_ids))
def create_goal(
self,
title: str,
*,
description: str | None = None,
source: GoalSource | str = GoalSource.USER_REQUEST,
priority: GoalPriority | str = GoalPriority.QUEUED,
parent_id: UUID | None = None,
due_at: datetime | None = None,
) -> UUID:
return self._loop.run_until_complete(
self._async.create_goal(
title,
description=description,
source=source,
priority=priority,
parent_id=parent_id,
due_at=due_at,
)
)
def queue_user_message(self, message: str, *, intent: str | None = None, context: dict[str, Any] | None = None) -> UUID:
return self._loop.run_until_complete(self._async.queue_user_message(message, intent=intent, context=context))
def get_ingestion_receipts(self, source_file: str, content_hashes: list[str]) -> dict[str, UUID]:
return self._loop.run_until_complete(self._async.get_ingestion_receipts(source_file, content_hashes))
def record_ingestion_receipts(self, items: list[dict[str, Any]]) -> int:
return self._loop.run_until_complete(self._async.record_ingestion_receipts(items))
def format_context_for_prompt(context: HydratedContext, *, max_memories: int = 5, max_partials: int = 3) -> str:
parts: list[str] = []
if context.memories:
parts.append("## Relevant Memories")
for m in context.memories[:max_memories]:
score = f" (score: {m.similarity:.2f})" if m.similarity is not None else ""
trust = f", trust: {m.trust_level:.2f}" if m.trust_level is not None else ""
src_kind = ""
if m.source_attribution and isinstance(m.source_attribution, dict):
kind = m.source_attribution.get("kind")
ref = m.source_attribution.get("ref")
if kind and ref:
src_kind = f", source: {kind} ({ref})"
elif kind:
src_kind = f", source: {kind}"
parts.append(f"- {m.content}{score}{trust}{src_kind}")
if context.partial_activations:
parts.append("\n## Vague Recollections (tip-of-tongue)")
for pa in context.partial_activations[:max_partials]:
keywords = ", ".join(pa.keywords[:5]) if pa.keywords else "unknown"
parts.append(f"- Theme '{pa.cluster_name}': {keywords}")
if context.identity:
parts.append("\n## Identity")
for aspect in context.identity[:3]:
parts.append(f"- {aspect.get('aspect_type', 'unknown')}: {aspect.get('content', {})}")
if context.worldview:
parts.append("\n## Beliefs")
for belief in context.worldview[:3]:
conf = belief.get("confidence", 0)
parts.append(f"- {belief.get('belief', '')} (confidence: {conf:.1f})")
if context.emotional_state:
es = context.emotional_state
parts.append("\n## Current Emotional State")
parts.append(f"- Feeling: {es.get('primary_emotion', 'neutral')}")
parts.append(f"- Valence: {es.get('valence', 0):.2f}, Arousal: {es.get('arousal', 0.5):.2f}")
if context.urgent_drives:
parts.append("\n## Urgent Drives")
for drive in context.urgent_drives: