agentic-composite-memory 0.1.0__py3-none-any.whl
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.
- agentic_composite_memory/__init__.py +2 -0
- agentic_composite_memory/py.typed +0 -0
- agentic_composite_memory-0.1.0.dist-info/METADATA +167 -0
- agentic_composite_memory-0.1.0.dist-info/RECORD +31 -0
- agentic_composite_memory-0.1.0.dist-info/WHEEL +4 -0
- agentic_composite_memory-0.1.0.dist-info/entry_points.txt +2 -0
- agentic_composite_memory-0.1.0.dist-info/licenses/LICENSE +17 -0
- agentic_memory_sdk/__init__.py +163 -0
- agentic_memory_sdk/adapters/__init__.py +33 -0
- agentic_memory_sdk/adapters/in_memory.py +439 -0
- agentic_memory_sdk/adapters/neo4j_cypher.py +65 -0
- agentic_memory_sdk/adapters/neptune_cypher.py +309 -0
- agentic_memory_sdk/adapters/opensearch_seed.py +322 -0
- agentic_memory_sdk/adapters/postgres_pgvector.py +484 -0
- agentic_memory_sdk/adapters/postgres_unified.py +511 -0
- agentic_memory_sdk/adapters/redis_playbook.py +157 -0
- agentic_memory_sdk/adapters/redis_unified.py +490 -0
- agentic_memory_sdk/adk_integration.py +250 -0
- agentic_memory_sdk/auth.py +380 -0
- agentic_memory_sdk/cli.py +137 -0
- agentic_memory_sdk/governance.py +713 -0
- agentic_memory_sdk/llm_extractors.py +616 -0
- agentic_memory_sdk/models.py +443 -0
- agentic_memory_sdk/ontology_loader.py +189 -0
- agentic_memory_sdk/ports.py +159 -0
- agentic_memory_sdk/py.typed +0 -0
- agentic_memory_sdk/server/__init__.py +4 -0
- agentic_memory_sdk/server/api.py +328 -0
- agentic_memory_sdk/server/main.py +384 -0
- agentic_memory_sdk/server/mcp_server.py +198 -0
- agentic_memory_sdk/service.py +1214 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Protocol, Sequence, runtime_checkable
|
|
4
|
+
|
|
5
|
+
from .models import (
|
|
6
|
+
CompiledTBox,
|
|
7
|
+
EpisodicRecord,
|
|
8
|
+
ExtractionPayload,
|
|
9
|
+
GraphEdgeMutation,
|
|
10
|
+
GraphFactReadResult,
|
|
11
|
+
GraphVertexUpsert,
|
|
12
|
+
OutboxOperation,
|
|
13
|
+
PlatformTenantContext,
|
|
14
|
+
PlaybookRecord,
|
|
15
|
+
ReasoningTraceStep,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@runtime_checkable
|
|
20
|
+
class RegistryStoreAdapter(Protocol):
|
|
21
|
+
"""Port for Platform Entitlements & Ontology TBox Registry."""
|
|
22
|
+
|
|
23
|
+
async def register_app(
|
|
24
|
+
self,
|
|
25
|
+
tenant_id: str,
|
|
26
|
+
app_name: str,
|
|
27
|
+
read_namespaces: list[str],
|
|
28
|
+
write_namespace: str,
|
|
29
|
+
tbox: CompiledTBox,
|
|
30
|
+
) -> None: ...
|
|
31
|
+
|
|
32
|
+
async def resolve_context(
|
|
33
|
+
self, tenant_id: str, app_name: str, user_id: str
|
|
34
|
+
) -> PlatformTenantContext: ...
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@runtime_checkable
|
|
38
|
+
class EpisodicAndSeedStoreAdapter(Protocol):
|
|
39
|
+
"""
|
|
40
|
+
Port for Tier 1 Episodic Storage, Tier 2 Hybrid RRF Seeding / Entity Resolution,
|
|
41
|
+
and the Transactional Outbox.
|
|
42
|
+
Implementations: PostgresPgVectorAdapter, OpenSearchSeedAdapter, InMemoryUnifiedAdapter.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
async def commit_episodes_and_outbox(
|
|
46
|
+
self,
|
|
47
|
+
ctx: PlatformTenantContext,
|
|
48
|
+
episodes: Sequence[EpisodicRecord],
|
|
49
|
+
vertex_upserts: Sequence[GraphVertexUpsert],
|
|
50
|
+
outbox_op: OutboxOperation | None,
|
|
51
|
+
) -> None: ...
|
|
52
|
+
|
|
53
|
+
async def resolve_existing_vertex_id(
|
|
54
|
+
self,
|
|
55
|
+
ctx: PlatformTenantContext,
|
|
56
|
+
entity_class: str,
|
|
57
|
+
canonical_name: str,
|
|
58
|
+
embedding: list[float],
|
|
59
|
+
similarity_threshold: float = 0.91,
|
|
60
|
+
) -> str | None: ...
|
|
61
|
+
|
|
62
|
+
async def hybrid_seed_search(
|
|
63
|
+
self,
|
|
64
|
+
ctx: PlatformTenantContext,
|
|
65
|
+
query_text: str,
|
|
66
|
+
query_embedding: list[float],
|
|
67
|
+
limit: int = 8,
|
|
68
|
+
) -> list[GraphVertexUpsert]: ...
|
|
69
|
+
|
|
70
|
+
async def search_episodes(
|
|
71
|
+
self,
|
|
72
|
+
ctx: PlatformTenantContext,
|
|
73
|
+
query_embedding: list[float],
|
|
74
|
+
limit: int = 4,
|
|
75
|
+
) -> list[EpisodicRecord]: ...
|
|
76
|
+
|
|
77
|
+
async def fetch_pending_outbox(self, batch_size: int = 25) -> list[OutboxOperation]: ...
|
|
78
|
+
|
|
79
|
+
async def mark_outbox_processed(self, outbox_id: str) -> None: ...
|
|
80
|
+
|
|
81
|
+
async def mark_outbox_failed(self, outbox_id: str, error_msg: str) -> None: ...
|
|
82
|
+
|
|
83
|
+
async def purge_user_data(self, ctx: PlatformTenantContext) -> list[str]:
|
|
84
|
+
"""Deletes user episodes and returns purged episode_ids for graph lineage cleanup."""
|
|
85
|
+
...
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@runtime_checkable
|
|
89
|
+
class GraphStoreAdapter(Protocol):
|
|
90
|
+
"""
|
|
91
|
+
Port for Tier 2 Bi-Temporal Semantic Graph & Tier 3 Procedural Reasoning Traces.
|
|
92
|
+
Implementations: NeptuneOpenCypherAdapter, Neo4jGraphAdapter, InMemoryUnifiedAdapter.
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
async def upsert_vertices_and_bitemporal_edges(
|
|
96
|
+
self,
|
|
97
|
+
tenant_id: str,
|
|
98
|
+
vertices: Sequence[GraphVertexUpsert],
|
|
99
|
+
edges: Sequence[GraphEdgeMutation],
|
|
100
|
+
) -> None: ...
|
|
101
|
+
|
|
102
|
+
async def expand_active_subgraph(
|
|
103
|
+
self,
|
|
104
|
+
ctx: PlatformTenantContext,
|
|
105
|
+
seed_vertex_ids: Sequence[str],
|
|
106
|
+
as_of_iso: str | None = None,
|
|
107
|
+
max_hops: int = 2,
|
|
108
|
+
limit: int = 25,
|
|
109
|
+
) -> list[GraphFactReadResult]: ...
|
|
110
|
+
|
|
111
|
+
async def record_reasoning_trace(
|
|
112
|
+
self,
|
|
113
|
+
ctx: PlatformTenantContext,
|
|
114
|
+
run_id: str,
|
|
115
|
+
session_id: str,
|
|
116
|
+
steps: Sequence[ReasoningTraceStep],
|
|
117
|
+
) -> None: ...
|
|
118
|
+
|
|
119
|
+
async def purge_episode_lineage(
|
|
120
|
+
self, tenant_id: str, episode_ids: Sequence[str], user_id: str
|
|
121
|
+
) -> None: ...
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@runtime_checkable
|
|
125
|
+
class PlaybookStoreAdapter(Protocol):
|
|
126
|
+
"""
|
|
127
|
+
Port for Tier 3 Procedural Playbook Caching & Semantic Workflow Recall.
|
|
128
|
+
Implementations:
|
|
129
|
+
- `RedisPlaybookAndWorkingAdapter` (Redis / ElastiCache / MemoryDB / Azure Managed Redis)
|
|
130
|
+
- `PostgresPlaybookAdapter` (PostgreSQL + pgvector)
|
|
131
|
+
- `InMemoryUnifiedAdapter` / `PostgresUnifiedAdapter` / `RedisUnifiedAdapter`
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
async def upsert_playbook(self, playbook: PlaybookRecord) -> PlaybookRecord: ...
|
|
135
|
+
|
|
136
|
+
async def match_playbooks(
|
|
137
|
+
self,
|
|
138
|
+
tenant_id: str,
|
|
139
|
+
read_namespaces: list[str],
|
|
140
|
+
query_embedding: list[float],
|
|
141
|
+
similarity_threshold: float = 0.85,
|
|
142
|
+
limit: int = 2,
|
|
143
|
+
) -> list[PlaybookRecord]: ...
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@runtime_checkable
|
|
147
|
+
class EmbeddingAdapter(Protocol):
|
|
148
|
+
"""Port for generating dense vector embeddings."""
|
|
149
|
+
|
|
150
|
+
async def embed(self, text: str) -> list[float]: ...
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@runtime_checkable
|
|
154
|
+
class OntologyExtractorAdapter(Protocol):
|
|
155
|
+
"""Port for TBox-constrained LLM extraction."""
|
|
156
|
+
|
|
157
|
+
async def extract(
|
|
158
|
+
self, transcript: str, tbox: CompiledTBox
|
|
159
|
+
) -> ExtractionPayload: ...
|
|
File without changes
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from contextlib import asynccontextmanager
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from fastapi import FastAPI, HTTPException
|
|
7
|
+
from pydantic import BaseModel, Field
|
|
8
|
+
|
|
9
|
+
from ..governance import PlatformOntologyRegistrar
|
|
10
|
+
from ..models import (
|
|
11
|
+
AppMemoryManifest,
|
|
12
|
+
DomainPackSpec,
|
|
13
|
+
ReasoningTraceStep,
|
|
14
|
+
)
|
|
15
|
+
from ..ontology_loader import DomainPackLoader
|
|
16
|
+
from ..service import CompositeMemoryService, Event, types
|
|
17
|
+
from .mcp_server import MemoryMcpServerTools, create_mcp_server
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class McpToolCallRequest(BaseModel):
|
|
21
|
+
tool_name: str
|
|
22
|
+
arguments: dict[str, Any] = Field(default_factory=dict)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class TurtleDomainPackRequest(BaseModel):
|
|
26
|
+
pack_id: str
|
|
27
|
+
turtle_content: str
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class EventTurnPayload(BaseModel):
|
|
31
|
+
author: str = "user"
|
|
32
|
+
text: str
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class IngestEventsRequest(BaseModel):
|
|
36
|
+
app_name: str
|
|
37
|
+
user_id: str
|
|
38
|
+
session_id: str = "default_session"
|
|
39
|
+
events: list[EventTurnPayload]
|
|
40
|
+
custom_metadata: dict[str, Any] = Field(default_factory=dict)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class SearchMemoryRequest(BaseModel):
|
|
44
|
+
app_name: str
|
|
45
|
+
user_id: str
|
|
46
|
+
query: str
|
|
47
|
+
as_of_iso: str | None = None
|
|
48
|
+
max_hops: int = 2
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class RecordTraceRequest(BaseModel):
|
|
52
|
+
app_name: str
|
|
53
|
+
user_id: str
|
|
54
|
+
run_id: str
|
|
55
|
+
session_id: str
|
|
56
|
+
steps: list[ReasoningTraceStep]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class PurgeLineageRequest(BaseModel):
|
|
60
|
+
app_name: str
|
|
61
|
+
user_id: str
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def create_memory_platform_app(
|
|
65
|
+
service: CompositeMemoryService,
|
|
66
|
+
registrar: PlatformOntologyRegistrar | None = None,
|
|
67
|
+
enable_outbox_worker: bool = True,
|
|
68
|
+
outbox_interval_sec: float = 2.0,
|
|
69
|
+
mount_mcp: bool = True,
|
|
70
|
+
) -> FastAPI:
|
|
71
|
+
"""
|
|
72
|
+
Creates the Unified Multi-Tenant Memory-as-a-Service FastAPI + MCP application.
|
|
73
|
+
Serves BOTH:
|
|
74
|
+
1. FastAPI REST Control & Data Plane (`/v1/...`) for `RemoteCompositeMemoryService`
|
|
75
|
+
2. Model Context Protocol (MCP) Server (`/mcp/sse`, `/mcp/http`, `/v1/mcp/tools`, `/v1/mcp/call`)
|
|
76
|
+
from a single ASGI process / Kubernetes Pod.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
@asynccontextmanager
|
|
80
|
+
async def lifespan(_app: FastAPI):
|
|
81
|
+
if enable_outbox_worker:
|
|
82
|
+
await service.start_outbox_worker(interval_sec=outbox_interval_sec)
|
|
83
|
+
try:
|
|
84
|
+
yield
|
|
85
|
+
finally:
|
|
86
|
+
if enable_outbox_worker:
|
|
87
|
+
await service.stop_outbox_worker()
|
|
88
|
+
|
|
89
|
+
app = FastAPI(
|
|
90
|
+
title="ADK Enterprise Composite Memory Platform API",
|
|
91
|
+
version="0.1.0",
|
|
92
|
+
description=(
|
|
93
|
+
"Domain-Agnostic Multi-Tenant Memory Service combining Episodic Memory, "
|
|
94
|
+
"Stratified Ontology (TBox), Bi-Temporal Graph KB (ABox), and Reasoning Traces."
|
|
95
|
+
),
|
|
96
|
+
lifespan=lifespan,
|
|
97
|
+
)
|
|
98
|
+
ont_registrar = registrar or PlatformOntologyRegistrar(service.registry)
|
|
99
|
+
|
|
100
|
+
@app.get("/health")
|
|
101
|
+
async def health() -> dict[str, Any]:
|
|
102
|
+
return {
|
|
103
|
+
"status": "ok",
|
|
104
|
+
"circuit_breaker_state": service.circuit_breaker.state,
|
|
105
|
+
"registered_domain_packs": sorted(
|
|
106
|
+
{p.pack_id for p in ont_registrar._domain_packs.values()}
|
|
107
|
+
),
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
# ------------------------------------------------------------------------
|
|
111
|
+
# CONTROL PLANE: Domain Pack Catalog & ADK App Registration
|
|
112
|
+
# ------------------------------------------------------------------------
|
|
113
|
+
@app.post("/v1/domains/packs")
|
|
114
|
+
async def register_domain_pack(pack: DomainPackSpec) -> dict[str, Any]:
|
|
115
|
+
"""Registers a plug-and-play Layer 1 Federated Domain Pack (JSON/Pydantic)."""
|
|
116
|
+
ont_registrar.register_domain_pack(pack)
|
|
117
|
+
return {
|
|
118
|
+
"status": "registered",
|
|
119
|
+
"pack_id": pack.pack_id,
|
|
120
|
+
"domain_name": pack.domain_name,
|
|
121
|
+
"classes_count": len(pack.classes),
|
|
122
|
+
"relations_count": len(pack.relations),
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
@app.post("/v1/domains/packs/review")
|
|
126
|
+
async def review_and_refine_domain_pack(
|
|
127
|
+
pack: DomainPackSpec, auto_register: bool = True
|
|
128
|
+
) -> dict[str, Any]:
|
|
129
|
+
"""
|
|
130
|
+
Reviews a proposed Domain Pack via Ontology Intelligence Steward:
|
|
131
|
+
roots unanchored classes under Layer 0, normalizes predicates, infers functional/transitive
|
|
132
|
+
flags, and records an immutable OntologyAuditLogEntry.
|
|
133
|
+
"""
|
|
134
|
+
refined, audit = ont_registrar.review_and_refine_domain_pack(
|
|
135
|
+
pack, auto_register=auto_register
|
|
136
|
+
)
|
|
137
|
+
return {
|
|
138
|
+
"status": "reviewed_and_registered" if auto_register else "reviewed",
|
|
139
|
+
"refined_pack": refined.model_dump(),
|
|
140
|
+
"audit_entry": audit.model_dump(),
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
@app.post("/v1/domains/evolve/{app_name}")
|
|
144
|
+
async def evolve_app_ontology(
|
|
145
|
+
app_name: str, min_confidence: float = 0.75
|
|
146
|
+
) -> dict[str, Any]:
|
|
147
|
+
"""
|
|
148
|
+
Bottom-Up Ontology Evolution: scans quarantined overflow in `app:{tenant}:{app}`,
|
|
149
|
+
promotes recurring high-confidence candidate classes & predicates into the governed TBox
|
|
150
|
+
and ABox, and records provenance back to the originating `source_episode_ids`.
|
|
151
|
+
"""
|
|
152
|
+
audit = await service.evolve_app_ontology_from_overflow(
|
|
153
|
+
app_name=app_name,
|
|
154
|
+
min_confidence_to_promote=min_confidence,
|
|
155
|
+
registrar=ont_registrar,
|
|
156
|
+
)
|
|
157
|
+
return {"status": "evolved", "audit_entry": audit.model_dump()}
|
|
158
|
+
|
|
159
|
+
@app.get("/v1/audit/ontology")
|
|
160
|
+
async def get_ontology_audit_log() -> dict[str, Any]:
|
|
161
|
+
"""Returns the immutable schema provenance audit log."""
|
|
162
|
+
return {
|
|
163
|
+
"audit_count": len(ont_registrar.audit_log),
|
|
164
|
+
"entries": [e.model_dump() for e in ont_registrar.audit_log],
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
@app.post("/v1/domains/packs/turtle")
|
|
168
|
+
async def register_domain_pack_from_turtle(
|
|
169
|
+
req: TurtleDomainPackRequest,
|
|
170
|
+
) -> dict[str, Any]:
|
|
171
|
+
"""Compiles and registers a W3C OWL/RDFS Turtle (.ttl) ontology as a Domain Pack."""
|
|
172
|
+
pack = DomainPackLoader.from_turtle(req.pack_id, req.turtle_content)
|
|
173
|
+
ont_registrar.register_domain_pack(pack)
|
|
174
|
+
return {
|
|
175
|
+
"status": "registered",
|
|
176
|
+
"pack_id": pack.pack_id,
|
|
177
|
+
"domain_name": pack.domain_name,
|
|
178
|
+
"classes": pack.classes,
|
|
179
|
+
"relations": {k: v.model_dump() for k, v in pack.relations.items()},
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
@app.post("/v1/apps/register")
|
|
183
|
+
async def register_adk_app(manifest: AppMemoryManifest) -> dict[str, Any]:
|
|
184
|
+
"""Registers an ADK agent app's Governance Tier, Domain Pack subscriptions, and custom types."""
|
|
185
|
+
compiled_tbox = await ont_registrar.register_adk_app(manifest)
|
|
186
|
+
return {
|
|
187
|
+
"status": "registered",
|
|
188
|
+
"tenant_id": manifest.tenant_id,
|
|
189
|
+
"app_name": manifest.app_name,
|
|
190
|
+
"governance_tier": manifest.governance_tier.value,
|
|
191
|
+
"compiled_classes": list(compiled_tbox.classes.keys()),
|
|
192
|
+
"compiled_predicates": list(compiled_tbox.relations.keys()),
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
# ------------------------------------------------------------------------
|
|
196
|
+
# DATA PLANE: Write, Read, Trace, Outbox & Lineage Purge
|
|
197
|
+
# ------------------------------------------------------------------------
|
|
198
|
+
@app.post("/v1/memory/events")
|
|
199
|
+
async def ingest_events(req: IngestEventsRequest) -> dict[str, Any]:
|
|
200
|
+
adk_events = [
|
|
201
|
+
Event(
|
|
202
|
+
author=e.author,
|
|
203
|
+
content=types.Content(
|
|
204
|
+
role="user",
|
|
205
|
+
parts=[types.Part.from_text(text=e.text)],
|
|
206
|
+
),
|
|
207
|
+
)
|
|
208
|
+
for e in req.events
|
|
209
|
+
]
|
|
210
|
+
try:
|
|
211
|
+
await service.add_events_to_memory(
|
|
212
|
+
app_name=req.app_name,
|
|
213
|
+
user_id=req.user_id,
|
|
214
|
+
events=adk_events,
|
|
215
|
+
session_id=req.session_id,
|
|
216
|
+
custom_metadata=req.custom_metadata,
|
|
217
|
+
)
|
|
218
|
+
except RuntimeError as exc:
|
|
219
|
+
raise HTTPException(status_code=429, detail=str(exc)) from exc
|
|
220
|
+
return {"status": "committed", "events_ingested": len(adk_events)}
|
|
221
|
+
|
|
222
|
+
@app.post("/v1/memory/search")
|
|
223
|
+
async def search_memory(req: SearchMemoryRequest) -> dict[str, Any]:
|
|
224
|
+
resp = await service.search_memory(
|
|
225
|
+
app_name=req.app_name,
|
|
226
|
+
user_id=req.user_id,
|
|
227
|
+
query=req.query,
|
|
228
|
+
as_of_iso=req.as_of_iso,
|
|
229
|
+
max_hops=req.max_hops,
|
|
230
|
+
)
|
|
231
|
+
serialized = []
|
|
232
|
+
for m in resp.memories:
|
|
233
|
+
text_val = ""
|
|
234
|
+
if getattr(m, "content", None) and getattr(m.content, "parts", None):
|
|
235
|
+
text_val = "\n".join(
|
|
236
|
+
p.text for p in m.content.parts if getattr(p, "text", None)
|
|
237
|
+
)
|
|
238
|
+
serialized.append(
|
|
239
|
+
{
|
|
240
|
+
"author": getattr(m, "author", "memory"),
|
|
241
|
+
"timestamp": getattr(m, "timestamp", None),
|
|
242
|
+
"text": text_val,
|
|
243
|
+
}
|
|
244
|
+
)
|
|
245
|
+
return {"memories": serialized}
|
|
246
|
+
|
|
247
|
+
@app.post("/v1/traces")
|
|
248
|
+
async def record_trace(req: RecordTraceRequest) -> dict[str, Any]:
|
|
249
|
+
await service.record_decision_trace(
|
|
250
|
+
app_name=req.app_name,
|
|
251
|
+
user_id=req.user_id,
|
|
252
|
+
run_id=req.run_id,
|
|
253
|
+
session_id=req.session_id,
|
|
254
|
+
steps=req.steps,
|
|
255
|
+
)
|
|
256
|
+
return {"status": "recorded", "run_id": req.run_id, "steps": len(req.steps)}
|
|
257
|
+
|
|
258
|
+
@app.post("/v1/outbox/flush")
|
|
259
|
+
async def flush_outbox(batch_size: int = 25) -> dict[str, Any]:
|
|
260
|
+
processed = await service.process_outbox_batch(batch_size=batch_size)
|
|
261
|
+
return {"status": "flushed", "processed_count": processed}
|
|
262
|
+
|
|
263
|
+
@app.post("/v1/lineage/purge")
|
|
264
|
+
async def purge_lineage(req: PurgeLineageRequest) -> dict[str, Any]:
|
|
265
|
+
purged_ids = await service.purge_user_lineage(
|
|
266
|
+
app_name=req.app_name, user_id=req.user_id
|
|
267
|
+
)
|
|
268
|
+
return {
|
|
269
|
+
"status": "purged",
|
|
270
|
+
"user_id": req.user_id,
|
|
271
|
+
"purged_episode_ids": purged_ids,
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
@app.post("/v1/maintenance/run")
|
|
275
|
+
async def run_maintenance(
|
|
276
|
+
min_episodes_to_compact: int = 4,
|
|
277
|
+
min_overflow_confidence: float = 0.55,
|
|
278
|
+
) -> dict[str, Any]:
|
|
279
|
+
"""Triggers behind-the-scenes Auto-Summarization (Episodic Compaction) and Auto-Cleanup."""
|
|
280
|
+
stats = await service.run_memory_maintenance(
|
|
281
|
+
min_episodes_to_compact=min_episodes_to_compact,
|
|
282
|
+
min_overflow_confidence=min_overflow_confidence,
|
|
283
|
+
)
|
|
284
|
+
return {"status": "completed", **stats}
|
|
285
|
+
|
|
286
|
+
# ------------------------------------------------------------------------
|
|
287
|
+
# UNIFIED MCP LAYER: Co-hosted MCP SSE, Streamable HTTP & Stateless Tool RPC
|
|
288
|
+
# ------------------------------------------------------------------------
|
|
289
|
+
if mount_mcp:
|
|
290
|
+
mcp_tools = MemoryMcpServerTools(service=service, registrar=ont_registrar)
|
|
291
|
+
mcp_server = create_mcp_server(service=service, registrar=ont_registrar)
|
|
292
|
+
|
|
293
|
+
@app.get("/v1/mcp/tools")
|
|
294
|
+
async def list_mcp_tools() -> dict[str, Any]:
|
|
295
|
+
"""Lists all MCP tools exposed by this unified Memory Platform pod."""
|
|
296
|
+
return {
|
|
297
|
+
"server_name": "adk-composite-memory",
|
|
298
|
+
"transports": {
|
|
299
|
+
"sse": "/mcp/sse",
|
|
300
|
+
"streamable_http": "/mcp/http",
|
|
301
|
+
"stateless_rpc": "/v1/mcp/call",
|
|
302
|
+
},
|
|
303
|
+
"tools": [
|
|
304
|
+
"recall_context",
|
|
305
|
+
"commit_episode",
|
|
306
|
+
"record_decision_trace",
|
|
307
|
+
"register_app_manifest",
|
|
308
|
+
"purge_user_lineage",
|
|
309
|
+
],
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
@app.post("/v1/mcp/call")
|
|
313
|
+
async def call_mcp_tool(req: McpToolCallRequest) -> dict[str, Any]:
|
|
314
|
+
"""Stateless Kubernetes-friendly HTTP endpoint to invoke any MCP memory tool."""
|
|
315
|
+
fn = getattr(mcp_tools, req.tool_name, None)
|
|
316
|
+
if fn is None or req.tool_name.startswith("_"):
|
|
317
|
+
raise HTTPException(
|
|
318
|
+
status_code=404, detail=f"Unknown MCP tool: {req.tool_name}"
|
|
319
|
+
)
|
|
320
|
+
res = await fn(**req.arguments)
|
|
321
|
+
return {"tool_name": req.tool_name, "result": res}
|
|
322
|
+
|
|
323
|
+
if hasattr(mcp_server, "sse_app"):
|
|
324
|
+
app.mount("/mcp/sse", mcp_server.sse_app())
|
|
325
|
+
if hasattr(mcp_server, "streamable_http_app"):
|
|
326
|
+
app.mount("/mcp/http", mcp_server.streamable_http_app())
|
|
327
|
+
|
|
328
|
+
return app
|