Skip to content

Commit 231d5ef

Browse files
Aayush KatariaAayush Kataria
authored andcommitted
refactor store + services
1 parent e87df99 commit 231d5ef

43 files changed

Lines changed: 5706 additions & 3904 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Docs/public_api.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Public API
2+
3+
## Architecture
4+
5+
`CosmosMemoryClient` and `AsyncCosmosMemoryClient` are thin orchestrators. They keep local-buffer state and Cosmos connection lifecycle, then delegate persistence to `MemoryStore` / `AsyncMemoryStore` and higher-level behavior to:
6+
7+
- `ChatClient` / `EmbeddingsClient` (sync) and `AsyncEmbeddingsClient` (async) — Azure OpenAI wrappers.
8+
- `RetrievalService` / `AsyncRetrievalService` for filtering, vector search, and episodic context.
9+
- `PipelineService` for extraction, summaries, procedural synthesis, and reconciliation.
10+
- `InProcessProcessor` / `AsyncInProcessProcessor` / `DurableFunctionProcessor` for immediate or change-feed-driven processing.
11+
- `auto_trigger.maybe_trigger_steps` (sync) and `aio.auto_trigger.maybe_trigger_steps` (async) for threshold-driven step firing after each `push_to_cosmos`.
12+
13+
## CosmosMemoryClient (sync)
14+
15+
### Connection
16+
17+
- `__init__(cosmos_endpoint=None, cosmos_credential=None, cosmos_key=None, cosmos_database=None, cosmos_container=None, cosmos_counter_container=None, cosmos_lease_container=None, cosmos_throughput_mode=None, cosmos_autoscale_max_ru=None, ai_foundry_endpoint=None, ai_foundry_credential=None, ai_foundry_api_key=None, embedding_deployment_name='text-embedding-3-large', embedding_dimensions=None, chat_deployment_name='gpt-4o-mini', use_default_credential=True, processor=None) -> None` — configure local state, model clients, optional Cosmos auto-connect, and optional processing backend.
18+
- `close() -> None` — close Cosmos/model clients and owned credentials.
19+
- `connect_cosmos(endpoint=None, credential=None, key=None, database=None, container=None) -> None` — connect to an existing memory container.
20+
- `create_memory_store(database=None, container=None, counter_container=None, lease_container=None, endpoint=None, credential=None, key=None, embedding_dimensions=None, embedding_data_type=None, distance_function=None, full_text_language=None, throughput_mode=None, autoscale_max_ru=None) -> None` — create/connect the memory, counter, and lease containers.
21+
22+
### Memory CRUD
23+
24+
- `add_local(user_id, role, content, memory_type='turn', agent_id=None, metadata=None, thread_id=None, tags=None, ttl=None, salience=None) -> None` — append a memory to the local buffer.
25+
- `get_local(memory_id=None, user_id=None, role=None, memory_types=None) -> list[dict]` — filter local buffered memories.
26+
- `update_local(memory_id, content=None, role=None, memory_type=None, metadata=None) -> None` — update a local buffered memory.
27+
- `delete_local(memory_id) -> None` — remove a local buffered memory.
28+
- `add_cosmos(user_id, role, content, memory_type='turn', metadata=None, thread_id=None, tags=None, ttl=None, salience=None, embedding=None, embed=None) -> str` — upsert one memory to Cosmos and return its id.
29+
- `push_to_cosmos(batch_size=25) -> None` — flush local buffered memories to Cosmos.
30+
- `get_memories(memory_id=None, user_id=None, thread_id=None, role=None, memory_types=None, recent_k=None, tags=None, any_tags=None, exclude_tags=None, include_superseded=False, min_salience=None, min_confidence=None) -> list[dict]` — retrieve memories with filters.
31+
- `update_cosmos(memory_id, content=None, role=None, memory_type=None, metadata=None) -> None` — update a Cosmos memory.
32+
- `delete_cosmos(memory_id, thread_id, user_id) -> None` — delete a Cosmos memory.
33+
- `get_thread(thread_id, user_id=None, memory_types=None, recent_k=None, tags=None, exclude_tags=None, include_superseded=False) -> list[dict]` — retrieve a thread oldest-first.
34+
- `get_user_summary(user_id) -> Optional[dict]` — retrieve the active user-summary document.
35+
36+
### Retrieval
37+
38+
- `search_cosmos(search_terms, memory_id=None, user_id=None, role=None, memory_types=None, thread_id=None, hybrid_search=False, top_k=5, tags=None, any_tags=None, exclude_tags=None, include_superseded=False, min_salience=None, min_confidence=None) -> list[dict]` — vector or hybrid search memories.
39+
- `get_procedural_prompt(user_id) -> Optional[str]` — read the active procedural prompt.
40+
- `get_procedural_history(user_id, limit=10) -> list[dict]` — read procedural prompt history.
41+
- `get_procedural_memories(user_id, priority=None, category=None, min_salience=None, include_superseded=False) -> list[dict]` — retrieve procedural memory documents.
42+
- `search_episodic_memories(user_id, search_terms, top_k=5, min_salience=None, include_superseded=False) -> list[dict]` — search episodic memories.
43+
- `build_procedural_context(user_id) -> str` — format procedural context for prompts.
44+
- `build_episodic_context(user_id, query, top_k=3) -> str` — format relevant episodic context.
45+
46+
### Processing
47+
48+
- `extract_memories(user_id, thread_id, recent_k=None) -> dict[str, int]` — extract facts/episodic memories from a thread.
49+
- `synthesize_procedural(user_id, *, force=False) -> dict` — synthesize the procedural prompt.
50+
- `generate_thread_summary(user_id, thread_id, recent_k=None, **kwargs) -> dict` — generate and persist a thread summary.
51+
- `generate_user_summary(user_id, thread_ids=None, recent_k=None, **kwargs) -> dict` — generate and persist a user summary.
52+
- `reconcile(user_id, n=None) -> dict[str, int]` — reconcile duplicate or contradictory facts.
53+
- `process_now(*, user_id, thread_id) -> ProcessThreadResult` — run the configured processor immediately.
54+
- `process_now_and_wait(*, user_id, thread_id, timeout=30.0) -> bool` — process and wait for a summary.
55+
56+
### Tagging
57+
58+
- `add_tags(memory_id, user_id, thread_id, tags) -> None` — add tags to a memory.
59+
- `remove_tags(memory_id, user_id, thread_id, tags) -> None` — remove tags from a memory.
60+
61+
## AsyncCosmosMemoryClient
62+
63+
Local-buffer methods remain synchronous in-memory operations; Cosmos, retrieval, and processing methods are `async` and must be awaited.
64+
65+
### Connection
66+
67+
- `__init__(cosmos_endpoint=None, cosmos_credential=None, cosmos_key=None, cosmos_database=None, cosmos_container=None, cosmos_counter_container=None, cosmos_lease_container=None, cosmos_throughput_mode=None, cosmos_autoscale_max_ru=None, ai_foundry_endpoint=None, ai_foundry_credential=None, ai_foundry_api_key=None, embedding_deployment_name='text-embedding-3-large', embedding_dimensions=None, chat_deployment_name='gpt-4o-mini', use_default_credential=True, processor=None) -> None` — configure async local state, model clients, and optional processing backend.
68+
- `async close() -> None` — close async/sync resources and owned credentials.
69+
- `async connect_cosmos(endpoint=None, credential=None, key=None, database=None, container=None) -> None` — connect to an existing memory container.
70+
- `async create_memory_store(database=None, container=None, counter_container=None, lease_container=None, endpoint=None, credential=None, key=None, embedding_dimensions=None, embedding_data_type=None, distance_function=None, full_text_language=None, throughput_mode=None, autoscale_max_ru=None) -> None` — create/connect memory, counter, and lease containers.
71+
72+
### Memory CRUD
73+
74+
- `add_local(user_id, role, content, memory_type='turn', agent_id=None, metadata=None, thread_id=None, tags=None, ttl=None, salience=None) -> None` — append a memory to the local buffer.
75+
- `get_local(memory_id=None, user_id=None, role=None, memory_types=None) -> list[dict]` — filter local buffered memories.
76+
- `update_local(memory_id, content=None, role=None, memory_type=None, metadata=None) -> None` — update a local buffered memory.
77+
- `delete_local(memory_id) -> None` — remove a local buffered memory.
78+
- `async add_cosmos(user_id, role, content, memory_type='turn', metadata=None, thread_id=None, tags=None, ttl=None, salience=None, embedding=None, embed=None) -> str` — upsert one memory to Cosmos and return its id.
79+
- `async push_to_cosmos(batch_size=25) -> None` — flush local buffered memories to Cosmos.
80+
- `async get_memories(memory_id=None, user_id=None, thread_id=None, role=None, memory_types=None, recent_k=None, tags=None, any_tags=None, exclude_tags=None, include_superseded=False, min_salience=None, min_confidence=None) -> list[dict]` — retrieve memories with filters.
81+
- `async update_cosmos(memory_id, content=None, role=None, memory_type=None, metadata=None) -> None` — update a Cosmos memory.
82+
- `async delete_cosmos(memory_id, thread_id, user_id) -> None` — delete a Cosmos memory.
83+
- `async get_thread(thread_id, user_id=None, memory_types=None, recent_k=None, tags=None, exclude_tags=None, include_superseded=False) -> list[dict]` — retrieve a thread oldest-first.
84+
- `async get_user_summary(user_id) -> Optional[dict]` — retrieve the active user-summary document.
85+
86+
### Retrieval
87+
88+
- `async search_cosmos(search_terms, memory_id=None, user_id=None, role=None, memory_types=None, thread_id=None, hybrid_search=False, top_k=5, tags=None, any_tags=None, exclude_tags=None, include_superseded=False, min_salience=None, min_confidence=None) -> list[dict]` — vector or hybrid search memories.
89+
- `async get_procedural_prompt(user_id) -> Optional[str]` — read the active procedural prompt.
90+
- `async get_procedural_history(user_id, limit=10) -> list[dict]` — read procedural prompt history.
91+
- `async get_procedural_memories(user_id, priority=None, category=None, min_salience=None, include_superseded=False) -> list[dict]` — retrieve procedural memory documents.
92+
- `async search_episodic_memories(user_id, search_terms, top_k=5, min_salience=None, include_superseded=False) -> list[dict]` — search episodic memories.
93+
- `async build_procedural_context(user_id) -> str` — format procedural context for prompts.
94+
- `async build_episodic_context(user_id, query, top_k=3) -> str` — format relevant episodic context.
95+
96+
### Processing
97+
98+
- `async extract_memories(user_id, thread_id, recent_k=None) -> dict[str, int]` — extract facts/episodic memories from a thread.
99+
- `async synthesize_procedural(user_id, *, force=False) -> dict` — synthesize the procedural prompt.
100+
- `async generate_thread_summary(user_id, thread_id, recent_k=None, **kwargs) -> dict` — generate and persist a thread summary.
101+
- `async generate_user_summary(user_id, thread_ids=None, recent_k=None, **kwargs) -> dict` — generate and persist a user summary.
102+
- `async reconcile(user_id, n=None) -> dict[str, int]` — reconcile duplicate or contradictory facts.
103+
- `async process_now(*, user_id, thread_id) -> ProcessThreadResult` — run the configured processor immediately.
104+
- `async process_now_and_wait(*, user_id, thread_id, timeout=30.0) -> bool` — process and wait for a summary.
105+
106+
### Tagging
107+
108+
- `async add_tags(memory_id, user_id, thread_id, tags) -> None` — add tags to a memory.
109+
- `async remove_tags(memory_id, user_id, thread_id, tags) -> None` — remove tags from a memory.
110+
111+
## Extension Points
112+
113+
Sync extension protocols live in `agent_memory_toolkit.services`; async variants live in `agent_memory_toolkit.aio.services`.
114+
115+
- `MemoryStoreProtocol` (`agent_memory_toolkit.services`): persistence primitives (`query`, `read_item`, `add_cosmos`, `mark_superseded`) consumed by the pipeline.
116+
117+
Concrete service classes are exported from their respective packages:
118+
119+
- Sync: `RetrievalService`, `PipelineService` from `agent_memory_toolkit.services` (sub-modules `retrieval`, `pipeline`).
120+
- Async: `AsyncRetrievalService` from `agent_memory_toolkit.aio.services` (sub-module `retrieval`). The pipeline is sync-only and is driven through `asyncio.to_thread` by the async client.
121+
- Threshold-driven auto-trigger: `maybe_trigger_steps` from `agent_memory_toolkit.auto_trigger` (sync) and `agent_memory_toolkit.aio.auto_trigger` (async).
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Shared client mixins."""
2+
3+
from agent_memory_toolkit._base.base_client import _BaseMemoryClient
4+
5+
__all__ = ["_BaseMemoryClient"]

0 commit comments

Comments
 (0)