remdb 0.3.14__py3-none-any.whl → 0.3.157__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.
- rem/agentic/README.md +76 -0
- rem/agentic/__init__.py +15 -0
- rem/agentic/agents/__init__.py +32 -2
- rem/agentic/agents/agent_manager.py +310 -0
- rem/agentic/agents/sse_simulator.py +502 -0
- rem/agentic/context.py +51 -27
- rem/agentic/context_builder.py +5 -3
- rem/agentic/llm_provider_models.py +301 -0
- rem/agentic/mcp/tool_wrapper.py +155 -18
- rem/agentic/otel/setup.py +93 -4
- rem/agentic/providers/phoenix.py +371 -108
- rem/agentic/providers/pydantic_ai.py +280 -57
- rem/agentic/schema.py +361 -21
- rem/agentic/tools/rem_tools.py +3 -3
- rem/api/README.md +215 -1
- rem/api/deps.py +255 -0
- rem/api/main.py +132 -40
- rem/api/mcp_router/resources.py +1 -1
- rem/api/mcp_router/server.py +28 -5
- rem/api/mcp_router/tools.py +555 -7
- rem/api/routers/admin.py +494 -0
- rem/api/routers/auth.py +278 -4
- rem/api/routers/chat/completions.py +402 -20
- rem/api/routers/chat/models.py +88 -10
- rem/api/routers/chat/otel_utils.py +33 -0
- rem/api/routers/chat/sse_events.py +542 -0
- rem/api/routers/chat/streaming.py +697 -45
- rem/api/routers/dev.py +81 -0
- rem/api/routers/feedback.py +268 -0
- rem/api/routers/messages.py +473 -0
- rem/api/routers/models.py +78 -0
- rem/api/routers/query.py +360 -0
- rem/api/routers/shared_sessions.py +406 -0
- rem/auth/__init__.py +13 -3
- rem/auth/middleware.py +186 -22
- rem/auth/providers/__init__.py +4 -1
- rem/auth/providers/email.py +215 -0
- rem/cli/commands/README.md +237 -64
- rem/cli/commands/cluster.py +1808 -0
- rem/cli/commands/configure.py +4 -7
- rem/cli/commands/db.py +386 -143
- rem/cli/commands/experiments.py +468 -76
- rem/cli/commands/process.py +14 -8
- rem/cli/commands/schema.py +97 -50
- rem/cli/commands/session.py +336 -0
- rem/cli/dreaming.py +2 -2
- rem/cli/main.py +29 -6
- rem/config.py +10 -3
- rem/models/core/core_model.py +7 -1
- rem/models/core/experiment.py +58 -14
- rem/models/core/rem_query.py +5 -2
- rem/models/entities/__init__.py +25 -0
- rem/models/entities/domain_resource.py +38 -0
- rem/models/entities/feedback.py +123 -0
- rem/models/entities/message.py +30 -1
- rem/models/entities/ontology.py +1 -1
- rem/models/entities/ontology_config.py +1 -1
- rem/models/entities/session.py +83 -0
- rem/models/entities/shared_session.py +180 -0
- rem/models/entities/subscriber.py +175 -0
- rem/models/entities/user.py +1 -0
- rem/registry.py +10 -4
- rem/schemas/agents/core/agent-builder.yaml +134 -0
- rem/schemas/agents/examples/contract-analyzer.yaml +1 -1
- rem/schemas/agents/examples/contract-extractor.yaml +1 -1
- rem/schemas/agents/examples/cv-parser.yaml +1 -1
- rem/schemas/agents/rem.yaml +7 -3
- rem/services/__init__.py +3 -1
- rem/services/content/service.py +92 -19
- rem/services/email/__init__.py +10 -0
- rem/services/email/service.py +459 -0
- rem/services/email/templates.py +360 -0
- rem/services/embeddings/api.py +4 -4
- rem/services/embeddings/worker.py +16 -16
- rem/services/phoenix/client.py +154 -14
- rem/services/postgres/README.md +197 -15
- rem/services/postgres/__init__.py +2 -1
- rem/services/postgres/diff_service.py +547 -0
- rem/services/postgres/pydantic_to_sqlalchemy.py +470 -140
- rem/services/postgres/repository.py +132 -0
- rem/services/postgres/schema_generator.py +205 -4
- rem/services/postgres/service.py +6 -6
- rem/services/rem/parser.py +44 -9
- rem/services/rem/service.py +36 -2
- rem/services/session/compression.py +137 -51
- rem/services/session/reload.py +15 -8
- rem/settings.py +515 -27
- rem/sql/background_indexes.sql +21 -16
- rem/sql/migrations/001_install.sql +387 -54
- rem/sql/migrations/002_install_models.sql +2304 -377
- rem/sql/migrations/003_optional_extensions.sql +326 -0
- rem/sql/migrations/004_cache_system.sql +548 -0
- rem/sql/migrations/005_schema_update.sql +145 -0
- rem/utils/README.md +45 -0
- rem/utils/__init__.py +18 -0
- rem/utils/date_utils.py +2 -2
- rem/utils/files.py +157 -1
- rem/utils/model_helpers.py +156 -1
- rem/utils/schema_loader.py +220 -22
- rem/utils/sql_paths.py +146 -0
- rem/utils/sql_types.py +3 -1
- rem/utils/vision.py +1 -1
- rem/workers/__init__.py +3 -1
- rem/workers/db_listener.py +579 -0
- rem/workers/unlogged_maintainer.py +463 -0
- {remdb-0.3.14.dist-info → remdb-0.3.157.dist-info}/METADATA +340 -229
- {remdb-0.3.14.dist-info → remdb-0.3.157.dist-info}/RECORD +109 -80
- {remdb-0.3.14.dist-info → remdb-0.3.157.dist-info}/WHEEL +1 -1
- rem/sql/002_install_models.sql +0 -1068
- rem/sql/install_models.sql +0 -1051
- rem/sql/migrations/003_seed_default_user.sql +0 -48
- {remdb-0.3.14.dist-info → remdb-0.3.157.dist-info}/entry_points.txt +0 -0
|
@@ -1,19 +1,70 @@
|
|
|
1
1
|
"""Session message compression and rehydration for efficient context loading.
|
|
2
2
|
|
|
3
|
-
This module implements message compression to keep conversation history
|
|
4
|
-
context windows while preserving full content via REM LOOKUP.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
This module implements message storage and compression to keep conversation history
|
|
4
|
+
within context windows while preserving full content via REM LOOKUP.
|
|
5
|
+
|
|
6
|
+
Message Types and Storage Strategy
|
|
7
|
+
===================================
|
|
8
|
+
|
|
9
|
+
All messages are stored UNCOMPRESSED in the database for full audit/analysis.
|
|
10
|
+
Compression happens only on RELOAD when reconstructing context for the LLM.
|
|
11
|
+
|
|
12
|
+
Message Types:
|
|
13
|
+
- `user`: User messages - stored and reloaded as-is
|
|
14
|
+
- `tool`: Tool call messages (e.g., register_metadata) - stored and reloaded as-is
|
|
15
|
+
NEVER compressed - contains important structured metadata
|
|
16
|
+
- `assistant`: Assistant text responses - stored uncompressed, but MAY BE
|
|
17
|
+
compressed on reload if long (>400 chars) with REM LOOKUP hints
|
|
18
|
+
|
|
19
|
+
Example Session Flow:
|
|
20
|
+
```
|
|
21
|
+
Turn 1 (stored uncompressed):
|
|
22
|
+
- user: "I have a headache"
|
|
23
|
+
- tool: register_metadata({confidence: 0.3, collected_fields: {...}})
|
|
24
|
+
- assistant: "I'm sorry to hear that. How long has this been going on?"
|
|
25
|
+
|
|
26
|
+
Turn 2 (stored uncompressed):
|
|
27
|
+
- user: "About 3 days, really bad"
|
|
28
|
+
- tool: register_metadata({confidence: 0.6, collected_fields: {...}})
|
|
29
|
+
- assistant: "Got it - 3 days. On a scale of 1-10..."
|
|
30
|
+
|
|
31
|
+
On reload (for LLM context):
|
|
32
|
+
- user messages: returned as-is
|
|
33
|
+
- tool messages: returned as-is (never compressed)
|
|
34
|
+
- assistant messages: compressed if long, with REM LOOKUP hint for full retrieval
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
REM LOOKUP Pattern:
|
|
38
|
+
- Long assistant messages get truncated with hint: "... [REM LOOKUP session-{id}-msg-{idx}] ..."
|
|
39
|
+
- Agent can retrieve full content on-demand using the LOOKUP key
|
|
40
|
+
- Keeps context window efficient while preserving data integrity
|
|
41
|
+
|
|
42
|
+
Key Design Decisions:
|
|
43
|
+
1. Store everything uncompressed - full audit trail in database
|
|
44
|
+
2. Compress only on reload - optimize for LLM context window
|
|
45
|
+
3. Never compress tool messages - structured metadata must stay intact
|
|
46
|
+
4. REM LOOKUP enables on-demand retrieval of full assistant responses
|
|
11
47
|
"""
|
|
12
48
|
|
|
13
49
|
from typing import Any
|
|
14
50
|
|
|
15
51
|
from loguru import logger
|
|
16
52
|
|
|
53
|
+
# Max length for entity keys (kv_store.entity_key is varchar(255))
|
|
54
|
+
MAX_ENTITY_KEY_LENGTH = 255
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def truncate_key(key: str, max_length: int = MAX_ENTITY_KEY_LENGTH) -> str:
|
|
58
|
+
"""Truncate a key to max length, preserving useful suffix if possible."""
|
|
59
|
+
if len(key) <= max_length:
|
|
60
|
+
return key
|
|
61
|
+
# Keep first part and add hash suffix for uniqueness
|
|
62
|
+
import hashlib
|
|
63
|
+
hash_suffix = hashlib.md5(key.encode()).hexdigest()[:8]
|
|
64
|
+
truncated = key[:max_length - 9] + "-" + hash_suffix
|
|
65
|
+
logger.warning(f"Truncated key from {len(key)} to {len(truncated)} chars: {key[:50]}...")
|
|
66
|
+
return truncated
|
|
67
|
+
|
|
17
68
|
from rem.models.entities import Message
|
|
18
69
|
from rem.services.postgres import PostgresService, Repository
|
|
19
70
|
from rem.settings import settings
|
|
@@ -151,15 +202,20 @@ class SessionMessageStore:
|
|
|
151
202
|
return f"msg-{message_index}"
|
|
152
203
|
|
|
153
204
|
# Create entity key for REM LOOKUP: session-{session_id}-msg-{index}
|
|
154
|
-
entity_key
|
|
205
|
+
# Truncate to avoid exceeding kv_store.entity_key varchar(255) limit
|
|
206
|
+
entity_key = truncate_key(f"session-{session_id}-msg-{message_index}")
|
|
155
207
|
|
|
156
208
|
# Create Message entity for assistant response
|
|
209
|
+
# Use pre-generated id from message dict if available (for frontend feedback)
|
|
157
210
|
msg = Message(
|
|
211
|
+
id=message.get("id"), # Use pre-generated ID if provided
|
|
158
212
|
content=message.get("content", ""),
|
|
159
213
|
message_type=message.get("role", "assistant"),
|
|
160
214
|
session_id=session_id,
|
|
161
215
|
tenant_id=self.user_id, # Set tenant_id to user_id (application scoped to user)
|
|
162
216
|
user_id=user_id or self.user_id,
|
|
217
|
+
trace_id=message.get("trace_id"),
|
|
218
|
+
span_id=message.get("span_id"),
|
|
163
219
|
metadata={
|
|
164
220
|
"message_index": message_index,
|
|
165
221
|
"entity_key": entity_key, # Store entity key for LOOKUP
|
|
@@ -265,18 +321,33 @@ class SessionMessageStore:
|
|
|
265
321
|
msg_copy["_entity_key"] = entity_key
|
|
266
322
|
compressed_messages.append(msg_copy)
|
|
267
323
|
else:
|
|
268
|
-
# Short assistant messages, user messages, and system messages stored as-is
|
|
324
|
+
# Short assistant messages, user messages, tool messages, and system messages stored as-is
|
|
269
325
|
# Store ALL messages in database for full audit trail
|
|
326
|
+
# Build metadata dict with standard fields
|
|
327
|
+
msg_metadata = {
|
|
328
|
+
"message_index": idx,
|
|
329
|
+
"timestamp": message.get("timestamp"),
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
# For tool messages, include tool call details in metadata
|
|
333
|
+
if message.get("role") == "tool":
|
|
334
|
+
if message.get("tool_call_id"):
|
|
335
|
+
msg_metadata["tool_call_id"] = message.get("tool_call_id")
|
|
336
|
+
if message.get("tool_name"):
|
|
337
|
+
msg_metadata["tool_name"] = message.get("tool_name")
|
|
338
|
+
if message.get("tool_arguments"):
|
|
339
|
+
msg_metadata["tool_arguments"] = message.get("tool_arguments")
|
|
340
|
+
|
|
270
341
|
msg = Message(
|
|
342
|
+
id=message.get("id"), # Use pre-generated ID if provided
|
|
271
343
|
content=content,
|
|
272
344
|
message_type=message.get("role", "user"),
|
|
273
345
|
session_id=session_id,
|
|
274
346
|
tenant_id=self.user_id, # Set tenant_id to user_id (application scoped to user)
|
|
275
347
|
user_id=user_id or self.user_id,
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
},
|
|
348
|
+
trace_id=message.get("trace_id"),
|
|
349
|
+
span_id=message.get("span_id"),
|
|
350
|
+
metadata=msg_metadata,
|
|
280
351
|
)
|
|
281
352
|
await self.repo.upsert(msg)
|
|
282
353
|
compressed_messages.append(message.copy())
|
|
@@ -284,18 +355,24 @@ class SessionMessageStore:
|
|
|
284
355
|
return compressed_messages
|
|
285
356
|
|
|
286
357
|
async def load_session_messages(
|
|
287
|
-
self, session_id: str, user_id: str | None = None,
|
|
358
|
+
self, session_id: str, user_id: str | None = None, compress_on_load: bool = True
|
|
288
359
|
) -> list[dict[str, Any]]:
|
|
289
360
|
"""
|
|
290
|
-
Load session messages from database.
|
|
361
|
+
Load session messages from database, optionally compressing long assistant messages.
|
|
362
|
+
|
|
363
|
+
Compression on Load:
|
|
364
|
+
- Tool messages (role: "tool") are NEVER compressed - they contain structured metadata
|
|
365
|
+
- User messages are returned as-is
|
|
366
|
+
- Assistant messages MAY be compressed if long (>400 chars) with REM LOOKUP hints
|
|
291
367
|
|
|
292
368
|
Args:
|
|
293
369
|
session_id: Session identifier
|
|
294
370
|
user_id: Optional user identifier for filtering
|
|
295
|
-
|
|
371
|
+
compress_on_load: Whether to compress long assistant messages (default: True)
|
|
296
372
|
|
|
297
373
|
Returns:
|
|
298
|
-
List of session messages in chronological order
|
|
374
|
+
List of session messages in chronological order, with long assistant
|
|
375
|
+
messages optionally compressed with REM LOOKUP hints
|
|
299
376
|
"""
|
|
300
377
|
if not settings.postgres.enabled:
|
|
301
378
|
logger.debug("Postgres disabled, returning empty message list")
|
|
@@ -312,49 +389,58 @@ class SessionMessageStore:
|
|
|
312
389
|
|
|
313
390
|
# Convert Message entities to dict format
|
|
314
391
|
message_dicts = []
|
|
315
|
-
for msg in messages:
|
|
392
|
+
for idx, msg in enumerate(messages):
|
|
393
|
+
role = msg.message_type or "assistant"
|
|
316
394
|
msg_dict = {
|
|
317
|
-
"role":
|
|
395
|
+
"role": role,
|
|
318
396
|
"content": msg.content,
|
|
319
397
|
"timestamp": msg.created_at.isoformat() if msg.created_at else None,
|
|
320
398
|
}
|
|
321
399
|
|
|
322
|
-
#
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
400
|
+
# For tool messages, reconstruct tool call metadata
|
|
401
|
+
if role == "tool" and msg.metadata:
|
|
402
|
+
if msg.metadata.get("tool_call_id"):
|
|
403
|
+
msg_dict["tool_call_id"] = msg.metadata["tool_call_id"]
|
|
404
|
+
if msg.metadata.get("tool_name"):
|
|
405
|
+
msg_dict["tool_name"] = msg.metadata["tool_name"]
|
|
406
|
+
if msg.metadata.get("tool_arguments"):
|
|
407
|
+
msg_dict["tool_arguments"] = msg.metadata["tool_arguments"]
|
|
408
|
+
|
|
409
|
+
# Compress long ASSISTANT messages on load (never tool messages)
|
|
410
|
+
if (
|
|
411
|
+
compress_on_load
|
|
412
|
+
and role == "assistant"
|
|
413
|
+
and len(msg.content) > self.compressor.min_length_for_compression
|
|
414
|
+
):
|
|
415
|
+
# Generate entity key for REM LOOKUP
|
|
416
|
+
entity_key = truncate_key(f"session-{session_id}-msg-{idx}")
|
|
417
|
+
msg_dict = self.compressor.compress_message(msg_dict, entity_key)
|
|
329
418
|
|
|
330
419
|
message_dicts.append(msg_dict)
|
|
331
420
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
if self.compressor.is_compressed(message):
|
|
337
|
-
entity_key = self.compressor.get_entity_key(message)
|
|
338
|
-
if entity_key:
|
|
339
|
-
full_content = await self.retrieve_message(entity_key)
|
|
340
|
-
if full_content:
|
|
341
|
-
decompressed_messages.append(
|
|
342
|
-
self.compressor.decompress_message(
|
|
343
|
-
message, full_content
|
|
344
|
-
)
|
|
345
|
-
)
|
|
346
|
-
else:
|
|
347
|
-
# Fallback to compressed version if retrieval fails
|
|
348
|
-
decompressed_messages.append(message)
|
|
349
|
-
else:
|
|
350
|
-
decompressed_messages.append(message)
|
|
351
|
-
else:
|
|
352
|
-
decompressed_messages.append(message)
|
|
353
|
-
|
|
354
|
-
return decompressed_messages
|
|
355
|
-
|
|
421
|
+
logger.debug(
|
|
422
|
+
f"Loaded {len(message_dicts)} messages for session {session_id} "
|
|
423
|
+
f"(compress_on_load={compress_on_load})"
|
|
424
|
+
)
|
|
356
425
|
return message_dicts
|
|
357
426
|
|
|
358
427
|
except Exception as e:
|
|
359
428
|
logger.error(f"Failed to load session messages: {e}")
|
|
360
429
|
return []
|
|
430
|
+
|
|
431
|
+
async def retrieve_full_message(self, session_id: str, message_index: int) -> str | None:
|
|
432
|
+
"""
|
|
433
|
+
Retrieve full message content by session and message index (for REM LOOKUP).
|
|
434
|
+
|
|
435
|
+
This is used when an agent needs to recover full content from a compressed
|
|
436
|
+
message that has a REM LOOKUP hint.
|
|
437
|
+
|
|
438
|
+
Args:
|
|
439
|
+
session_id: Session identifier
|
|
440
|
+
message_index: Index of message in session (from REM LOOKUP key)
|
|
441
|
+
|
|
442
|
+
Returns:
|
|
443
|
+
Full message content or None if not found
|
|
444
|
+
"""
|
|
445
|
+
entity_key = truncate_key(f"session-{session_id}-msg-{message_index}")
|
|
446
|
+
return await self.retrieve_message(entity_key)
|
rem/services/session/reload.py
CHANGED
|
@@ -6,8 +6,14 @@ allowing conversations to be resumed across multiple API calls.
|
|
|
6
6
|
Design Pattern:
|
|
7
7
|
- Session identified by session_id from X-Session-Id header
|
|
8
8
|
- All messages for session loaded in chronological order
|
|
9
|
-
-
|
|
9
|
+
- Long assistant messages compressed on load with REM LOOKUP hints
|
|
10
|
+
- Tool messages (register_metadata, etc.) are NEVER compressed
|
|
10
11
|
- Gracefully handles missing database (returns empty history)
|
|
12
|
+
|
|
13
|
+
Message Types on Reload:
|
|
14
|
+
- user: Returned as-is
|
|
15
|
+
- tool: Returned as-is with metadata (tool_call_id, tool_name, tool_arguments)
|
|
16
|
+
- assistant: Compressed on load if long (>400 chars), with REM LOOKUP for recovery
|
|
11
17
|
"""
|
|
12
18
|
|
|
13
19
|
from loguru import logger
|
|
@@ -19,7 +25,7 @@ from rem.settings import settings
|
|
|
19
25
|
async def reload_session(
|
|
20
26
|
session_id: str,
|
|
21
27
|
user_id: str,
|
|
22
|
-
|
|
28
|
+
compress_on_load: bool = True,
|
|
23
29
|
) -> list[dict]:
|
|
24
30
|
"""
|
|
25
31
|
Reload all messages for a session from the database.
|
|
@@ -27,7 +33,8 @@ async def reload_session(
|
|
|
27
33
|
Args:
|
|
28
34
|
session_id: Session/conversation identifier
|
|
29
35
|
user_id: User identifier for data isolation
|
|
30
|
-
|
|
36
|
+
compress_on_load: Whether to compress long assistant messages (default: True)
|
|
37
|
+
Tool messages are NEVER compressed.
|
|
31
38
|
|
|
32
39
|
Returns:
|
|
33
40
|
List of message dicts in chronological order (oldest first)
|
|
@@ -41,7 +48,7 @@ async def reload_session(
|
|
|
41
48
|
history = await reload_session(
|
|
42
49
|
session_id=context.session_id,
|
|
43
50
|
user_id=context.user_id,
|
|
44
|
-
|
|
51
|
+
compress_on_load=True, # Compress long assistant messages
|
|
45
52
|
)
|
|
46
53
|
|
|
47
54
|
# Combine with new user message
|
|
@@ -60,14 +67,14 @@ async def reload_session(
|
|
|
60
67
|
# Create message store for this session
|
|
61
68
|
store = SessionMessageStore(user_id=user_id)
|
|
62
69
|
|
|
63
|
-
# Load messages (
|
|
70
|
+
# Load messages (assistant messages compressed on load, tool messages never compressed)
|
|
64
71
|
messages = await store.load_session_messages(
|
|
65
|
-
session_id=session_id, user_id=user_id,
|
|
72
|
+
session_id=session_id, user_id=user_id, compress_on_load=compress_on_load
|
|
66
73
|
)
|
|
67
74
|
|
|
68
|
-
logger.
|
|
75
|
+
logger.debug(
|
|
69
76
|
f"Reloaded {len(messages)} messages for session {session_id} "
|
|
70
|
-
f"(
|
|
77
|
+
f"(compress_on_load={compress_on_load})"
|
|
71
78
|
)
|
|
72
79
|
|
|
73
80
|
return messages
|