AsyncEmotionalMemory¶
emotional_memory.async_engine.AsyncEmotionalMemory(store: AsyncMemoryStore, embedder: AsyncEmbedder, appraisal_engine: AsyncAppraisalEngine | None = None, config: EmotionalMemoryConfig | None = None, state_store: AffectiveStateStore | None = None, query_classifier: QueryClassifier | None = None)
¶
Async-native EmotionalMemory using the AFT pipeline.
All public methods are coroutines. An internal asyncio.Lock protects
the affective state from interleaved mutations across concurrent
encode / encode_batch calls. Synchronous helpers
(set_affect, reset_state, load_state) are intended for setup
before concurrent work begins and are not lock-protected — do not
call them while encode or observe coroutines may be running.
When using a state_store backed by network I/O (e.g.
RedisAffectiveStateStore), constructing the engine will briefly block
the event loop to load the persisted snapshot. For fully non-blocking
initialisation, pass state_store=None and explicitly call
await engine.restore_persisted_state() after construction.
Source code in src/emotional_memory/async_engine.py
clear_persisted_state() -> None
async
¶
Remove the persisted affective-state snapshot, if configured.
close() -> None
async
¶
Release resources held by store, state store, and appraisal engine.
Awaits store.close() when available; falls back to sync close()
via asyncio.to_thread; silently skips collaborators without
cleanup. The embedder is not owned by the engine and remains the
caller's responsibility.
Source code in src/emotional_memory/async_engine.py
count() -> int
async
¶
delete(memory_id: str) -> None
async
¶
elaborate(memory_id: str) -> Memory | None
async
¶
Run full appraisal on a fast-path (pending) memory and blend core_affect.
Async version of EmotionalMemory.elaborate().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
memory_id
|
str
|
ID of the memory to elaborate. |
required |
Returns:
| Type | Description |
|---|---|
Memory | None
|
The updated Memory, or None if the memory does not exist, |
Memory | None
|
is not pending appraisal, or no appraisal engine is configured. |
Source code in src/emotional_memory/async_engine.py
elaborate_pending() -> list[Memory]
async
¶
Elaborate all memories with pending_appraisal=True (async).
Returns:
| Type | Description |
|---|---|
list[Memory]
|
List of updated Memory objects (only those that were elaborated). |
Source code in src/emotional_memory/async_engine.py
encode(content: str, appraisal: AppraisalVector | None = None, metadata: dict[str, Any] | None = None) -> Memory
async
¶
Encode content into emotional memory (async).
Mirrors EmotionalMemory.encode with awaited I/O calls.
Source code in src/emotional_memory/async_engine.py
encode_batch(contents: list[str], metadata: list[dict[str, Any]] | None = None) -> list[Memory]
async
¶
Encode multiple contents using a single embed_batch call (async).
Source code in src/emotional_memory/async_engine.py
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 | |
export_memories() -> list[dict[str, Any]]
async
¶
Export all memories as a list of JSON-serialisable dicts (async).
get(memory_id: str) -> Memory | None
async
¶
get_current_mood(now: datetime | None = None) -> MoodField
¶
Return the mood field regressed to now without modifying state.
Source code in src/emotional_memory/async_engine.py
get_state() -> AffectiveState
¶
import_memories(data: list[dict[str, Any]], *, overwrite: bool = False) -> int
async
¶
Import memories from a list of dicts produced by export_memories() (async).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[dict[str, Any]]
|
List of memory dicts (as returned by |
required |
overwrite
|
bool
|
When True, existing memories with the same ID are replaced. When False (default), duplicates are skipped silently. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
Number of memories actually written. |
Source code in src/emotional_memory/async_engine.py
list_all() -> list[Memory]
async
¶
load_state(data: dict[str, Any]) -> None
¶
observe(content: str, appraisal: AppraisalVector | None = None, metadata: dict[str, Any] | None = None) -> EmotionalTag
async
¶
Update affective state from content without storing a retrievable memory.
Source code in src/emotional_memory/async_engine.py
persist_state() -> dict[str, Any]
async
¶
Return and, when configured, persist the current affective state.
prune(threshold: float = 0.05) -> int
async
¶
Remove memories whose effective strength has fallen below threshold (async).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Minimum acceptable strength in [0, 1]. Defaults to 0.05 (5%). |
0.05
|
Returns:
| Type | Description |
|---|---|
int
|
Number of memories removed. |
Source code in src/emotional_memory/async_engine.py
reset_state() -> None
¶
restore_persisted_state() -> bool
async
¶
Load the last persisted affective state from the configured state store.
Source code in src/emotional_memory/async_engine.py
retrieve(query: str, top_k: int = 5, *, query_affect: CoreAffect | None = None, precomputed_weights: NDArray[np.float64] | None = None) -> list[Memory]
async
¶
Retrieve the top-k most relevant memories for the query (async).
Mirrors EmotionalMemory.retrieve with awaited I/O calls. query_affect
overrides the affect-proximity signal source (default: runtime state).
Source code in src/emotional_memory/async_engine.py
retrieve_query_gated(query: str, top_k: int = 5, *, tau: float | None = None) -> list[Memory]
async
¶
Retrieve with the Addendum Y query-affect gate (Branch A, async).
Mirrors EmotionalMemory.retrieve_query_gated.
Source code in src/emotional_memory/async_engine.py
retrieve_with_explanations(query: str, top_k: int = 5, *, query_affect: CoreAffect | None = None, precomputed_weights: NDArray[np.float64] | None = None) -> list[RetrievalExplanation]
async
¶
Async retrieval variant that exposes the ranking breakdown.
query_affect overrides the affect-proximity signal source (default:
runtime state), as in retrieve().
Source code in src/emotional_memory/async_engine.py
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 | |
retrieve_with_query_appraisal(query: str, top_k: int = 5) -> list[Memory]
async
¶
Retrieve using the query's own appraised affect (Addendum T, async).
Mirrors EmotionalMemory.retrieve_with_query_appraisal: appraises the
query text and uses the resulting affect as query_affect without
mutating runtime state. Pairs best with DIRECT_VAD_SCHEMA.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if no appraisal engine is configured. |
Source code in src/emotional_memory/async_engine.py
save_state() -> dict[str, Any]
¶
set_affect(core_affect: CoreAffect) -> None
¶
Manually inject a CoreAffect, updating state synchronously.
Source code in src/emotional_memory/async_engine.py
update_memory(memory_id: str, *, content: str | None = None, tag: EmotionalTag | None = None, metadata: dict[str, Any] | None = None) -> Memory
async
¶
Replace selected fields of an existing memory asynchronously.