elephantine 0.3.5__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.
@@ -0,0 +1,4 @@
1
+ """Elephantine Cognitive Memory Engine."""
2
+ from elephantine.client.client import ElephantineClient, AsyncElephantineClient
3
+
4
+ __all__ = ["ElephantineClient", "AsyncElephantineClient"]
elephantine/api/app.py ADDED
@@ -0,0 +1,43 @@
1
+ from fastapi import FastAPI
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from elephantine.config import settings
4
+ from elephantine.api.routes.health import router as health_router
5
+ from elephantine.api.routes.memory import router as memory_router
6
+ from elephantine.api.routes.dashboard import router as dashboard_router
7
+
8
+ from contextlib import asynccontextmanager
9
+ from elephantine.api.routes.memory import get_container
10
+
11
+ @asynccontextmanager
12
+ async def lifespan(app: FastAPI):
13
+ # Startup: Start proactive background evaluator daemon
14
+ container = get_container()
15
+ await container.proactive_engine.start()
16
+ yield
17
+ # Shutdown: Stop proactive background evaluator daemon
18
+ await container.proactive_engine.stop()
19
+
20
+ def create_app() -> FastAPI:
21
+ app = FastAPI(
22
+ title="MemAgent Core",
23
+ description="Local-First, CPU-Native Cognitive AI Memory Engine",
24
+ version="0.1.0",
25
+ debug=settings.DEBUG,
26
+ lifespan=lifespan
27
+ )
28
+
29
+ app.add_middleware(
30
+ CORSMiddleware,
31
+ allow_origins=["*"],
32
+ allow_credentials=True,
33
+ allow_methods=["*"],
34
+ allow_headers=["*"],
35
+ )
36
+
37
+ app.include_router(health_router, tags=["Health"])
38
+ app.include_router(memory_router, tags=["Memory Primitives"])
39
+ app.include_router(dashboard_router, tags=["Dashboard & Inspector"])
40
+
41
+ return app
42
+
43
+ app = create_app()
@@ -0,0 +1,66 @@
1
+ from typing import Optional
2
+ from fastapi import Header, HTTPException, Security, Depends
3
+ from fastapi.security import APIKeyHeader, HTTPBearer, HTTPAuthorizationCredentials
4
+ from elephantine.config import settings
5
+ from elephantine.core.auth_interface import TenantContext
6
+ from elephantine.enterprise.rbac import api_key_manager, rbac_auth_engine
7
+
8
+ api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
9
+ bearer_scheme = HTTPBearer(auto_error=False)
10
+
11
+ async def get_current_tenant_context(
12
+ api_key: Optional[str] = Security(api_key_header),
13
+ bearer: Optional[HTTPAuthorizationCredentials] = Security(bearer_scheme)
14
+ ) -> TenantContext:
15
+ """
16
+ Resolves the calling agent's tenant and security context.
17
+ If settings.AUTH_ENABLED is False (Community mode), grants full local access with zero configuration.
18
+ If True, verifies the API key or Bearer token.
19
+ """
20
+ if not settings.AUTH_ENABLED:
21
+ return TenantContext(
22
+ tenant_id="default_tenant",
23
+ agent_id="local",
24
+ roles=["admin"],
25
+ is_enterprise=False
26
+ )
27
+
28
+ token = api_key or (bearer.credentials if bearer else None)
29
+ if not token:
30
+ raise HTTPException(
31
+ status_code=401,
32
+ detail="Unauthorized: Missing API Key. Provide via 'X-API-Key' header or 'Authorization: Bearer <key>'."
33
+ )
34
+
35
+ context = api_key_manager.authenticate(token)
36
+ if not context:
37
+ raise HTTPException(
38
+ status_code=401,
39
+ detail="Unauthorized: Invalid Elephantine API Key."
40
+ )
41
+
42
+ return context
43
+
44
+ async def require_write_permission(
45
+ context: TenantContext = Depends(get_current_tenant_context)
46
+ ) -> TenantContext:
47
+ """Ensures the caller has write authorization."""
48
+ can_write = await rbac_auth_engine.authorize_write(context)
49
+ if not can_write:
50
+ raise HTTPException(
51
+ status_code=403,
52
+ detail="Forbidden: Caller role has insufficient permissions to write memories."
53
+ )
54
+ return context
55
+
56
+ async def require_read_permission(
57
+ context: TenantContext = Depends(get_current_tenant_context)
58
+ ) -> TenantContext:
59
+ """Ensures the caller has read authorization."""
60
+ can_read = await rbac_auth_engine.authorize_read(context)
61
+ if not can_read:
62
+ raise HTTPException(
63
+ status_code=403,
64
+ detail="Forbidden: Caller role has insufficient permissions to read memories."
65
+ )
66
+ return context