remdb 0.3.7__py3-none-any.whl → 0.3.133__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.
Files changed (107) hide show
  1. rem/__init__.py +129 -2
  2. rem/agentic/README.md +76 -0
  3. rem/agentic/__init__.py +15 -0
  4. rem/agentic/agents/__init__.py +16 -2
  5. rem/agentic/agents/sse_simulator.py +502 -0
  6. rem/agentic/context.py +51 -25
  7. rem/agentic/llm_provider_models.py +301 -0
  8. rem/agentic/mcp/tool_wrapper.py +112 -17
  9. rem/agentic/otel/setup.py +93 -4
  10. rem/agentic/providers/phoenix.py +314 -132
  11. rem/agentic/providers/pydantic_ai.py +215 -26
  12. rem/agentic/schema.py +361 -21
  13. rem/agentic/tools/rem_tools.py +3 -3
  14. rem/api/README.md +238 -1
  15. rem/api/deps.py +255 -0
  16. rem/api/main.py +154 -37
  17. rem/api/mcp_router/resources.py +1 -1
  18. rem/api/mcp_router/server.py +26 -5
  19. rem/api/mcp_router/tools.py +465 -7
  20. rem/api/middleware/tracking.py +172 -0
  21. rem/api/routers/admin.py +494 -0
  22. rem/api/routers/auth.py +124 -0
  23. rem/api/routers/chat/completions.py +402 -20
  24. rem/api/routers/chat/models.py +88 -10
  25. rem/api/routers/chat/otel_utils.py +33 -0
  26. rem/api/routers/chat/sse_events.py +542 -0
  27. rem/api/routers/chat/streaming.py +642 -45
  28. rem/api/routers/dev.py +81 -0
  29. rem/api/routers/feedback.py +268 -0
  30. rem/api/routers/messages.py +473 -0
  31. rem/api/routers/models.py +78 -0
  32. rem/api/routers/query.py +360 -0
  33. rem/api/routers/shared_sessions.py +406 -0
  34. rem/auth/middleware.py +126 -27
  35. rem/cli/commands/README.md +237 -64
  36. rem/cli/commands/ask.py +13 -10
  37. rem/cli/commands/cluster.py +1808 -0
  38. rem/cli/commands/configure.py +5 -6
  39. rem/cli/commands/db.py +396 -139
  40. rem/cli/commands/experiments.py +469 -74
  41. rem/cli/commands/process.py +22 -15
  42. rem/cli/commands/scaffold.py +47 -0
  43. rem/cli/commands/schema.py +97 -50
  44. rem/cli/main.py +29 -6
  45. rem/config.py +10 -3
  46. rem/models/core/core_model.py +7 -1
  47. rem/models/core/experiment.py +54 -0
  48. rem/models/core/rem_query.py +5 -2
  49. rem/models/entities/__init__.py +21 -0
  50. rem/models/entities/domain_resource.py +38 -0
  51. rem/models/entities/feedback.py +123 -0
  52. rem/models/entities/message.py +30 -1
  53. rem/models/entities/session.py +83 -0
  54. rem/models/entities/shared_session.py +180 -0
  55. rem/models/entities/user.py +10 -3
  56. rem/registry.py +373 -0
  57. rem/schemas/agents/rem.yaml +7 -3
  58. rem/services/content/providers.py +92 -133
  59. rem/services/content/service.py +92 -20
  60. rem/services/dreaming/affinity_service.py +2 -16
  61. rem/services/dreaming/moment_service.py +2 -15
  62. rem/services/embeddings/api.py +24 -17
  63. rem/services/embeddings/worker.py +16 -16
  64. rem/services/phoenix/EXPERIMENT_DESIGN.md +3 -3
  65. rem/services/phoenix/client.py +302 -28
  66. rem/services/postgres/README.md +159 -15
  67. rem/services/postgres/__init__.py +2 -1
  68. rem/services/postgres/diff_service.py +531 -0
  69. rem/services/postgres/pydantic_to_sqlalchemy.py +427 -129
  70. rem/services/postgres/repository.py +132 -0
  71. rem/services/postgres/schema_generator.py +291 -9
  72. rem/services/postgres/service.py +6 -6
  73. rem/services/rate_limit.py +113 -0
  74. rem/services/rem/README.md +14 -0
  75. rem/services/rem/parser.py +44 -9
  76. rem/services/rem/service.py +36 -2
  77. rem/services/session/compression.py +24 -1
  78. rem/services/session/reload.py +1 -1
  79. rem/services/user_service.py +98 -0
  80. rem/settings.py +399 -29
  81. rem/sql/background_indexes.sql +21 -16
  82. rem/sql/migrations/001_install.sql +387 -54
  83. rem/sql/migrations/002_install_models.sql +2320 -393
  84. rem/sql/migrations/003_optional_extensions.sql +326 -0
  85. rem/sql/migrations/004_cache_system.sql +548 -0
  86. rem/utils/__init__.py +18 -0
  87. rem/utils/constants.py +97 -0
  88. rem/utils/date_utils.py +228 -0
  89. rem/utils/embeddings.py +17 -4
  90. rem/utils/files.py +167 -0
  91. rem/utils/mime_types.py +158 -0
  92. rem/utils/model_helpers.py +156 -1
  93. rem/utils/schema_loader.py +282 -35
  94. rem/utils/sql_paths.py +146 -0
  95. rem/utils/sql_types.py +3 -1
  96. rem/utils/vision.py +9 -14
  97. rem/workers/README.md +14 -14
  98. rem/workers/__init__.py +3 -1
  99. rem/workers/db_listener.py +579 -0
  100. rem/workers/db_maintainer.py +74 -0
  101. rem/workers/unlogged_maintainer.py +463 -0
  102. {remdb-0.3.7.dist-info → remdb-0.3.133.dist-info}/METADATA +460 -303
  103. {remdb-0.3.7.dist-info → remdb-0.3.133.dist-info}/RECORD +105 -74
  104. {remdb-0.3.7.dist-info → remdb-0.3.133.dist-info}/WHEEL +1 -1
  105. rem/sql/002_install_models.sql +0 -1068
  106. rem/sql/install_models.sql +0 -1038
  107. {remdb-0.3.7.dist-info → remdb-0.3.133.dist-info}/entry_points.txt +0 -0
rem/api/main.py CHANGED
@@ -26,10 +26,10 @@ Endpoints:
26
26
  - /health : Health check
27
27
  - /api/v1/mcp : MCP endpoint (HTTP transport)
28
28
  - /api/v1/chat/completions : OpenAI-compatible chat completions (streaming & non-streaming)
29
- - /api/v1/query : REM query execution (TODO)
29
+ - /api/v1/query : REM query execution (rem-dialect or natural-language)
30
30
  - /api/v1/resources : Resource CRUD (TODO)
31
31
  - /api/v1/moments : Moment CRUD (TODO)
32
- - /api/auth/* : OAuth/OIDC authentication (TODO)
32
+ - /api/auth/* : OAuth/OIDC authentication
33
33
  - /docs : OpenAPI documentation
34
34
 
35
35
  Headers → AgentContext Mapping:
@@ -59,8 +59,16 @@ Running:
59
59
  hypercorn rem.api.main:app --bind 0.0.0.0:8000
60
60
  """
61
61
 
62
+ import importlib.metadata
62
63
  import secrets
64
+ import sys
63
65
  import time
66
+
67
+ # Get package version for API responses
68
+ try:
69
+ __version__ = importlib.metadata.version("remdb")
70
+ except importlib.metadata.PackageNotFoundError:
71
+ __version__ = "0.0.0-dev"
64
72
  from contextlib import asynccontextmanager
65
73
 
66
74
  from fastapi import FastAPI, Request
@@ -73,6 +81,23 @@ from starlette.middleware.sessions import SessionMiddleware
73
81
  from .mcp_router.server import create_mcp_server
74
82
  from ..settings import settings
75
83
 
84
+ # Configure loguru based on settings
85
+ # Remove default handler and add one with configured level
86
+ logger.remove()
87
+
88
+ # Configure level icons - only warnings and errors get visual indicators
89
+ logger.level("DEBUG", icon=" ")
90
+ logger.level("INFO", icon=" ")
91
+ logger.level("WARNING", icon="🟠")
92
+ logger.level("ERROR", icon="🔴")
93
+ logger.level("CRITICAL", icon="🔴")
94
+
95
+ logger.add(
96
+ sys.stderr,
97
+ level=settings.api.log_level.upper(),
98
+ format="<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | {level.icon} <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
99
+ )
100
+
76
101
 
77
102
  class RequestLoggingMiddleware(BaseHTTPMiddleware):
78
103
  """
@@ -82,26 +107,64 @@ class RequestLoggingMiddleware(BaseHTTPMiddleware):
82
107
  - Logs request method, path, client, user-agent
83
108
  - Logs response status, content-type, duration
84
109
  - Essential for debugging OAuth flow and MCP sessions
110
+ - Health checks and 404s logged at DEBUG level to reduce noise
111
+ - Scanner/exploit attempts (common vulnerability probes) logged at DEBUG
85
112
  """
86
113
 
114
+ # Paths to log at DEBUG level (health checks, probes)
115
+ DEBUG_PATHS = {"/health", "/healthz", "/ready", "/readyz", "/livez"}
116
+
117
+ # Path patterns that indicate vulnerability scanners (log at DEBUG)
118
+ SCANNER_PATTERNS = (
119
+ "/vendor/", # PHP composer exploits
120
+ "/.git/", # Git config exposure
121
+ "/.env", # Environment file exposure
122
+ "/wp-", # WordPress exploits
123
+ "/phpunit/", # PHPUnit RCE
124
+ "/eval-stdin", # PHP eval exploits
125
+ "/console/", # Console exposure
126
+ "/actuator/", # Spring Boot actuator
127
+ "/debug/", # Debug endpoints
128
+ "/admin/", # Admin panel probes (when we don't have one)
129
+ )
130
+
131
+ def _should_log_at_debug(self, path: str, status_code: int) -> bool:
132
+ """Determine if request should be logged at DEBUG level."""
133
+ # Health checks
134
+ if path in self.DEBUG_PATHS:
135
+ return True
136
+ # 404 responses (not found - includes scanner probes)
137
+ if status_code == 404:
138
+ return True
139
+ # Known scanner patterns
140
+ if any(pattern in path for pattern in self.SCANNER_PATTERNS):
141
+ return True
142
+ return False
143
+
87
144
  async def dispatch(self, request: Request, call_next):
88
145
  start_time = time.time()
146
+ path = request.url.path
89
147
 
90
- # Log incoming request
148
+ # Log incoming request (preliminary - may adjust after response)
91
149
  client_host = request.client.host if request.client else "unknown"
92
- logger.info(
93
- f"→ REQUEST: {request.method} {request.url.path} | "
94
- f"Client: {client_host} | "
95
- f"User-Agent: {request.headers.get('user-agent', 'unknown')[:100]}"
96
- )
150
+ user_agent = request.headers.get('user-agent', 'unknown')[:100]
97
151
 
98
152
  # Process request
99
153
  response = await call_next(request)
100
154
 
101
- # Log response
155
+ # Determine log level based on path AND response status
102
156
  duration_ms = (time.time() - start_time) * 1000
103
- logger.info(
104
- f"← RESPONSE: {request.method} {request.url.path} | "
157
+ use_debug = self._should_log_at_debug(path, response.status_code)
158
+ log_fn = logger.debug if use_debug else logger.info
159
+
160
+ # Log request and response together
161
+ log_fn(
162
+ f"→ REQUEST: {request.method} {path} | "
163
+ f"Client: {client_host} | "
164
+ f"User-Agent: {user_agent}"
165
+ )
166
+ log_fn(
167
+ f"← RESPONSE: {request.method} {path} | "
105
168
  f"Status: {response.status_code} | "
106
169
  f"Duration: {duration_ms:.2f}ms"
107
170
  )
@@ -154,7 +217,8 @@ async def lifespan(app: FastAPI):
154
217
  "and history lookups are unavailable. Enable database with POSTGRES__ENABLED=true"
155
218
  )
156
219
  else:
157
- logger.info(f"Database enabled: {settings.postgres.connection_string}")
220
+ # Log database host only - never log credentials
221
+ logger.info(f"Database enabled: {settings.postgres.host}:{settings.postgres.port}/{settings.postgres.database}")
158
222
 
159
223
  yield
160
224
 
@@ -163,7 +227,22 @@ async def lifespan(app: FastAPI):
163
227
 
164
228
  def create_app() -> FastAPI:
165
229
  """
166
- Create and configure the FastAPI application.
230
+ Create and configure the FastAPI application with MCP server.
231
+
232
+ The returned app exposes `app.mcp_server` (FastMCP instance) for adding
233
+ custom tools, resources, and prompts:
234
+
235
+ app = create_app()
236
+
237
+ @app.mcp_server.tool()
238
+ async def my_tool(query: str) -> dict:
239
+ '''Custom MCP tool.'''
240
+ return {"result": query}
241
+
242
+ @app.mcp_server.resource("custom://data")
243
+ async def my_resource() -> str:
244
+ '''Custom resource.'''
245
+ return '{"data": "value"}'
167
246
 
168
247
  Design Pattern:
169
248
  1. Create MCP server
@@ -174,9 +253,10 @@ def create_app() -> FastAPI:
174
253
  6. Define health endpoints
175
254
  7. Register API routers
176
255
  8. Mount MCP app
256
+ 9. Expose mcp_server on app for extension
177
257
 
178
258
  Returns:
179
- Configured FastAPI application
259
+ Configured FastAPI application with .mcp_server attribute
180
260
  """
181
261
  # Create MCP server and get HTTP app
182
262
  # path="/" creates routes at root, then mount at /api/v1/mcp
@@ -198,15 +278,42 @@ def create_app() -> FastAPI:
198
278
  yield
199
279
 
200
280
  app = FastAPI(
201
- title="REM API",
202
- description="Resources Entities Moments system for agentic AI",
203
- version="0.1.0",
281
+ title=f"{settings.app_name} API",
282
+ description=f"{settings.app_name} - Resources Entities Moments system for agentic AI",
283
+ version=__version__,
204
284
  lifespan=combined_lifespan,
205
285
  root_path=settings.root_path if settings.root_path else "",
206
286
  redirect_slashes=False, # Don't redirect /mcp/ -> /mcp
207
287
  )
208
288
 
289
+ # Add request logging middleware
290
+ app.add_middleware(RequestLoggingMiddleware)
291
+
292
+ # Add SSE buffering middleware (for MCP SSE transport)
293
+ app.add_middleware(SSEBufferingMiddleware)
294
+
295
+ # Add Anonymous Tracking & Rate Limiting (Runs AFTER Auth if Auth is enabled)
296
+ # Must be added BEFORE AuthMiddleware in code to be INNER in the stack
297
+ from .middleware.tracking import AnonymousTrackingMiddleware
298
+ app.add_middleware(AnonymousTrackingMiddleware)
299
+
300
+ # Add authentication middleware
301
+ # Always load middleware for dev token support, but allow anonymous when auth disabled
302
+ from ..auth.middleware import AuthMiddleware
303
+
304
+ app.add_middleware(
305
+ AuthMiddleware,
306
+ protected_paths=["/api/v1"],
307
+ excluded_paths=["/api/auth", "/api/dev", "/api/v1/mcp/auth"],
308
+ # Allow anonymous when auth is disabled, otherwise use setting
309
+ allow_anonymous=(not settings.auth.enabled) or settings.auth.allow_anonymous,
310
+ # MCP requires auth only when auth is fully enabled
311
+ mcp_requires_auth=settings.auth.enabled and settings.auth.mcp_requires_auth,
312
+ )
313
+
209
314
  # Add session middleware for OAuth state management
315
+ # Must be added AFTER AuthMiddleware in code so it runs BEFORE (middleware runs in reverse)
316
+ # AuthMiddleware needs request.session to be available
210
317
  session_secret = settings.auth.session_secret or secrets.token_hex(32)
211
318
  if not settings.auth.session_secret:
212
319
  logger.warning(
@@ -223,27 +330,12 @@ def create_app() -> FastAPI:
223
330
  https_only=settings.environment == "production",
224
331
  )
225
332
 
226
- # Add request logging middleware
227
- app.add_middleware(RequestLoggingMiddleware)
228
-
229
- # Add SSE buffering middleware (for MCP SSE transport)
230
- app.add_middleware(SSEBufferingMiddleware)
231
-
232
- # Add authentication middleware (if enabled)
233
- if settings.auth.enabled:
234
- from ..auth.middleware import AuthMiddleware
235
-
236
- app.add_middleware(
237
- AuthMiddleware,
238
- protected_paths=["/api/v1"],
239
- excluded_paths=["/api/auth", "/api/v1/mcp/auth"],
240
- )
241
-
242
333
  # Add CORS middleware LAST (runs first in middleware chain)
243
334
  # Must expose mcp-session-id header for MCP session management
244
335
  CORS_ORIGIN_WHITELIST = [
245
- "http://localhost:5173", # Local development (Vite)
246
336
  "http://localhost:3000", # Local development (React)
337
+ "http://localhost:5000", # Local development (Flask/other)
338
+ "http://localhost:5173", # Local development (Vite)
247
339
  ]
248
340
 
249
341
  app.add_middleware(
@@ -261,8 +353,8 @@ def create_app() -> FastAPI:
261
353
  """API information endpoint."""
262
354
  # TODO: If auth enabled and no user, return 401 with WWW-Authenticate
263
355
  return {
264
- "name": "REM API",
265
- "version": "0.1.0",
356
+ "name": f"{settings.app_name} API",
357
+ "version": __version__,
266
358
  "mcp_endpoint": "/api/v1/mcp",
267
359
  "docs": "/docs",
268
360
  }
@@ -271,12 +363,27 @@ def create_app() -> FastAPI:
271
363
  @app.get("/health")
272
364
  async def health():
273
365
  """Health check endpoint."""
274
- return {"status": "healthy", "version": "0.1.0"}
366
+ return {"status": "healthy", "version": __version__}
275
367
 
276
368
  # Register API routers
277
369
  from .routers.chat import router as chat_router
370
+ from .routers.models import router as models_router
371
+ from .routers.messages import router as messages_router
372
+ from .routers.feedback import router as feedback_router
373
+ from .routers.admin import router as admin_router
374
+ from .routers.shared_sessions import router as shared_sessions_router
375
+ from .routers.query import router as query_router
278
376
 
279
377
  app.include_router(chat_router)
378
+ app.include_router(models_router)
379
+ # shared_sessions_router MUST be before messages_router
380
+ # because messages_router has /sessions/{session_id} which would match
381
+ # before the more specific /sessions/shared-with-me routes
382
+ app.include_router(shared_sessions_router)
383
+ app.include_router(messages_router)
384
+ app.include_router(feedback_router)
385
+ app.include_router(admin_router)
386
+ app.include_router(query_router)
280
387
 
281
388
  # Register auth router (if enabled)
282
389
  if settings.auth.enabled:
@@ -284,6 +391,12 @@ def create_app() -> FastAPI:
284
391
 
285
392
  app.include_router(auth_router)
286
393
 
394
+ # Register dev router (non-production only)
395
+ if settings.environment != "production":
396
+ from .routers.dev import router as dev_router
397
+
398
+ app.include_router(dev_router)
399
+
287
400
  # TODO: Register additional routers
288
401
  # from .routers.query import router as query_router
289
402
  # from .routers.resources import router as resources_router
@@ -305,6 +418,10 @@ def create_app() -> FastAPI:
305
418
  # Mount MCP app at /api/v1/mcp
306
419
  app.mount("/api/v1/mcp", mcp_app)
307
420
 
421
+ # Expose MCP server on app for extension
422
+ # Users can add tools/resources/prompts via app.mcp_server
423
+ app.mcp_server = mcp_server # type: ignore[attr-defined]
424
+
308
425
  return app
309
426
 
310
427
 
@@ -181,7 +181,7 @@ Parameters:
181
181
  - table_name (required): Table to search (resources, moments, etc.)
182
182
  - field_name (optional): Field to search (defaults to "content")
183
183
  - provider (optional): Embedding provider (default: from LLM__EMBEDDING_PROVIDER setting)
184
- - min_similarity (optional): Minimum similarity 0.0-1.0 (default: 0.7)
184
+ - min_similarity (optional): Minimum similarity 0.0-1.0 (default: 0.3)
185
185
  - limit (optional): Max results (default: 10)
186
186
  - user_id (optional): User scoping
187
187
 
@@ -19,10 +19,18 @@ FastMCP Features:
19
19
  - Built-in auth that can be disabled for testing
20
20
  """
21
21
 
22
+ import importlib.metadata
23
+
22
24
  from fastmcp import FastMCP
23
25
 
24
26
  from ...settings import settings
25
27
 
28
+ # Get package version
29
+ try:
30
+ __version__ = importlib.metadata.version("remdb")
31
+ except importlib.metadata.PackageNotFoundError:
32
+ __version__ = "0.0.0-dev"
33
+
26
34
 
27
35
  def create_mcp_server(is_local: bool = False) -> FastMCP:
28
36
  """
@@ -52,7 +60,7 @@ def create_mcp_server(is_local: bool = False) -> FastMCP:
52
60
  """
53
61
  mcp = FastMCP(
54
62
  name=f"REM MCP Server ({settings.team}/{settings.environment})",
55
- version="0.1.0",
63
+ version=__version__,
56
64
  instructions=(
57
65
  "REM (Resource-Entity-Moment) MCP Server - Unified memory infrastructure for agentic systems.\n\n"
58
66
  "═══════════════════════════════════════════════════════════════════════════\n"
@@ -119,10 +127,12 @@ def create_mcp_server(is_local: bool = False) -> FastMCP:
119
127
  "AVAILABLE TOOLS\n"
120
128
  "═══════════════════════════════════════════════════════════════════════════\n"
121
129
  "\n"
122
- "• rem_query - Execute REM queries (LOOKUP, FUZZY, SEARCH, SQL, TRAVERSE)\n"
123
- "• ask_rem - Natural language to REM query conversion\n"
130
+ "• search_rem - Execute REM queries (LOOKUP, FUZZY, SEARCH, SQL, TRAVERSE)\n"
131
+ "• ask_rem_agent - Natural language to REM query conversion\n"
124
132
  " - plan_mode=True: Hints agent to use TRAVERSE with depth=0 for edge analysis\n"
125
- "• parse_and_ingest_file - Ingest files from local paths (local server only), s3://, or https://\n"
133
+ "• ingest_into_rem - Ingest files from local paths (local server only), s3://, or https://\n"
134
+ "• list_schema - List all database schemas (tables) with row counts\n"
135
+ "• get_schema - Get detailed schema for a specific table (columns, types, indexes)\n"
126
136
  "\n"
127
137
  "═══════════════════════════════════════════════════════════════════════════\n"
128
138
  "AVAILABLE RESOURCES (Read-Only)\n"
@@ -165,11 +175,22 @@ def create_mcp_server(is_local: bool = False) -> FastMCP:
165
175
  )
166
176
 
167
177
  # Register REM tools
168
- from .tools import ask_rem_agent, ingest_into_rem, read_resource, search_rem
178
+ from .tools import (
179
+ ask_rem_agent,
180
+ get_schema,
181
+ ingest_into_rem,
182
+ list_schema,
183
+ read_resource,
184
+ register_metadata,
185
+ search_rem,
186
+ )
169
187
 
170
188
  mcp.tool()(search_rem)
171
189
  mcp.tool()(ask_rem_agent)
172
190
  mcp.tool()(read_resource)
191
+ mcp.tool()(register_metadata)
192
+ mcp.tool()(list_schema)
193
+ mcp.tool()(get_schema)
173
194
 
174
195
  # File ingestion tool (with local path support for local servers)
175
196
  # Wrap to inject is_local parameter