remdb 0.3.180__py3-none-any.whl → 0.3.230__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 +36 -2
- rem/agentic/context.py +173 -0
- rem/agentic/context_builder.py +12 -2
- rem/agentic/mcp/tool_wrapper.py +2 -2
- rem/agentic/providers/pydantic_ai.py +1 -1
- rem/agentic/schema.py +2 -2
- rem/api/main.py +1 -1
- rem/api/mcp_router/server.py +4 -0
- rem/api/mcp_router/tools.py +542 -166
- rem/api/routers/admin.py +30 -4
- rem/api/routers/auth.py +106 -10
- rem/api/routers/chat/child_streaming.py +379 -0
- rem/api/routers/chat/completions.py +74 -37
- rem/api/routers/chat/sse_events.py +7 -3
- rem/api/routers/chat/streaming.py +352 -257
- rem/api/routers/chat/streaming_utils.py +327 -0
- rem/api/routers/common.py +18 -0
- rem/api/routers/dev.py +7 -1
- rem/api/routers/feedback.py +9 -1
- rem/api/routers/messages.py +176 -38
- rem/api/routers/models.py +9 -1
- rem/api/routers/query.py +12 -1
- rem/api/routers/shared_sessions.py +16 -0
- rem/auth/jwt.py +19 -4
- rem/auth/middleware.py +42 -28
- rem/cli/README.md +62 -0
- rem/cli/commands/ask.py +61 -81
- rem/cli/commands/db.py +55 -31
- rem/cli/commands/process.py +171 -43
- rem/models/entities/ontology.py +18 -20
- rem/schemas/agents/rem.yaml +1 -1
- rem/services/content/service.py +18 -5
- rem/services/embeddings/worker.py +26 -12
- rem/services/postgres/__init__.py +28 -3
- rem/services/postgres/diff_service.py +57 -5
- rem/services/postgres/programmable_diff_service.py +635 -0
- rem/services/postgres/pydantic_to_sqlalchemy.py +2 -2
- rem/services/postgres/register_type.py +11 -10
- rem/services/postgres/repository.py +39 -29
- rem/services/postgres/schema_generator.py +5 -5
- rem/services/postgres/sql_builder.py +6 -5
- rem/services/session/__init__.py +8 -1
- rem/services/session/compression.py +40 -2
- rem/services/session/pydantic_messages.py +292 -0
- rem/settings.py +28 -0
- rem/sql/migrations/001_install.sql +125 -7
- rem/sql/migrations/002_install_models.sql +159 -149
- rem/sql/migrations/004_cache_system.sql +7 -275
- rem/sql/migrations/migrate_session_id_to_uuid.sql +45 -0
- rem/utils/schema_loader.py +79 -51
- {remdb-0.3.180.dist-info → remdb-0.3.230.dist-info}/METADATA +2 -2
- {remdb-0.3.180.dist-info → remdb-0.3.230.dist-info}/RECORD +54 -48
- {remdb-0.3.180.dist-info → remdb-0.3.230.dist-info}/WHEEL +0 -0
- {remdb-0.3.180.dist-info → remdb-0.3.230.dist-info}/entry_points.txt +0 -0
rem/agentic/README.md
CHANGED
|
@@ -716,11 +716,45 @@ curl -X POST http://localhost:8000/api/v1/chat/completions \
|
|
|
716
716
|
|
|
717
717
|
See `rem/api/README.md` for full SSE event protocol documentation.
|
|
718
718
|
|
|
719
|
+
## Multi-Agent Orchestration
|
|
720
|
+
|
|
721
|
+
Agents can delegate work to other agents via the `ask_agent` tool. This enables orchestrator patterns where a parent agent routes to specialists.
|
|
722
|
+
|
|
723
|
+
### How It Works
|
|
724
|
+
|
|
725
|
+
1. **Parent agent** calls `ask_agent(agent_name, input_text)`
|
|
726
|
+
2. **Child agent** executes and streams its response
|
|
727
|
+
3. **Child events** bubble up to parent via an event sink (asyncio.Queue in ContextVar)
|
|
728
|
+
4. **All tool calls** are saved to the database for the session
|
|
729
|
+
|
|
730
|
+
### Key Components
|
|
731
|
+
|
|
732
|
+
| Component | Location | Purpose |
|
|
733
|
+
|-----------|----------|---------|
|
|
734
|
+
| `ask_agent` tool | `mcp_router/tools.py` | Loads child agent, runs with streaming, pushes events to sink |
|
|
735
|
+
| Event sink | `context.py` | ContextVar holding asyncio.Queue for child→parent event flow |
|
|
736
|
+
| Streaming controller | `streaming.py` | Drains event sink, emits SSE events, saves to DB |
|
|
737
|
+
|
|
738
|
+
### Event Types
|
|
739
|
+
|
|
740
|
+
Child agents emit events that the parent streams to the client:
|
|
741
|
+
|
|
742
|
+
- **`child_tool_start`**: Child is calling a tool (logged, streamed, saved to DB)
|
|
743
|
+
- **`child_content`**: Child's text response (streamed as SSE content delta)
|
|
744
|
+
- **`child_tool_result`**: Tool completed with result (metadata extraction)
|
|
745
|
+
|
|
746
|
+
### Testing
|
|
747
|
+
|
|
748
|
+
Integration tests in `tests/integration/test_ask_agent_streaming.py` verify:
|
|
749
|
+
- Child content streams correctly to client
|
|
750
|
+
- Tool calls are persisted to database
|
|
751
|
+
- Multi-turn conversations save all messages
|
|
752
|
+
|
|
719
753
|
## Future Work
|
|
720
754
|
|
|
721
755
|
- [ ] Phoenix evaluator integration
|
|
722
756
|
- [ ] Agent schema registry (load schemas by URI)
|
|
723
757
|
- [ ] Schema validation and versioning
|
|
724
|
-
- [
|
|
725
|
-
- [
|
|
758
|
+
- [x] Multi-turn conversation management
|
|
759
|
+
- [x] Agent composition (agents calling agents)
|
|
726
760
|
- [ ] Alternative provider implementations (if needed)
|
rem/agentic/context.py
CHANGED
|
@@ -22,14 +22,153 @@ Key Design Pattern:
|
|
|
22
22
|
- Enables session tracking across API, CLI, and test execution
|
|
23
23
|
- Supports header-based configuration override (model, schema URI)
|
|
24
24
|
- Clean separation: context (who/what) vs agent (how)
|
|
25
|
+
|
|
26
|
+
Multi-Agent Context Propagation:
|
|
27
|
+
- ContextVar (_current_agent_context) threads context through nested agent calls
|
|
28
|
+
- Parent context is automatically available to child agents via get_current_context()
|
|
29
|
+
- Use agent_context_scope() context manager for scoped context setting
|
|
30
|
+
- Child agents inherit user_id, tenant_id, session_id, is_eval from parent
|
|
25
31
|
"""
|
|
26
32
|
|
|
33
|
+
import asyncio
|
|
34
|
+
from contextlib import contextmanager
|
|
35
|
+
from contextvars import ContextVar
|
|
36
|
+
from typing import Any, Generator
|
|
37
|
+
|
|
27
38
|
from loguru import logger
|
|
28
39
|
from pydantic import BaseModel, Field
|
|
29
40
|
|
|
30
41
|
from ..settings import settings
|
|
31
42
|
|
|
32
43
|
|
|
44
|
+
# Thread-local context for current agent execution
|
|
45
|
+
# This enables context propagation through nested agent calls (multi-agent)
|
|
46
|
+
_current_agent_context: ContextVar["AgentContext | None"] = ContextVar(
|
|
47
|
+
"current_agent_context", default=None
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# Event sink for streaming child agent events to parent
|
|
51
|
+
# When set, child agents (via ask_agent) should push their events here
|
|
52
|
+
# for the parent's streaming loop to proxy to the client
|
|
53
|
+
_parent_event_sink: ContextVar["asyncio.Queue | None"] = ContextVar(
|
|
54
|
+
"parent_event_sink", default=None
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def get_current_context() -> "AgentContext | None":
|
|
59
|
+
"""
|
|
60
|
+
Get the current agent context from context var.
|
|
61
|
+
|
|
62
|
+
Used by MCP tools (like ask_agent) to inherit context from parent agent.
|
|
63
|
+
Returns None if no context is set (e.g., direct CLI invocation without context).
|
|
64
|
+
|
|
65
|
+
Example:
|
|
66
|
+
# In an MCP tool
|
|
67
|
+
parent_context = get_current_context()
|
|
68
|
+
if parent_context:
|
|
69
|
+
# Inherit user_id, session_id, etc. from parent
|
|
70
|
+
child_context = parent_context.child_context(agent_schema_uri="child-agent")
|
|
71
|
+
"""
|
|
72
|
+
return _current_agent_context.get()
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def set_current_context(ctx: "AgentContext | None") -> None:
|
|
76
|
+
"""
|
|
77
|
+
Set the current agent context.
|
|
78
|
+
|
|
79
|
+
Called by streaming layer before agent execution.
|
|
80
|
+
Should be cleared (set to None) after execution completes.
|
|
81
|
+
"""
|
|
82
|
+
_current_agent_context.set(ctx)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@contextmanager
|
|
86
|
+
def agent_context_scope(ctx: "AgentContext") -> Generator["AgentContext", None, None]:
|
|
87
|
+
"""
|
|
88
|
+
Context manager for scoped context setting.
|
|
89
|
+
|
|
90
|
+
Automatically restores previous context when exiting scope.
|
|
91
|
+
Safe for nested agent calls - each level preserves its parent's context.
|
|
92
|
+
|
|
93
|
+
Example:
|
|
94
|
+
context = AgentContext(user_id="user-123")
|
|
95
|
+
with agent_context_scope(context):
|
|
96
|
+
# Context is available via get_current_context()
|
|
97
|
+
result = await agent.run(...)
|
|
98
|
+
# Previous context (or None) is restored
|
|
99
|
+
"""
|
|
100
|
+
previous = _current_agent_context.get()
|
|
101
|
+
_current_agent_context.set(ctx)
|
|
102
|
+
try:
|
|
103
|
+
yield ctx
|
|
104
|
+
finally:
|
|
105
|
+
_current_agent_context.set(previous)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# =============================================================================
|
|
109
|
+
# Event Sink for Streaming Multi-Agent Delegation
|
|
110
|
+
# =============================================================================
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def get_event_sink() -> "asyncio.Queue | None":
|
|
114
|
+
"""
|
|
115
|
+
Get the parent's event sink for streaming child events.
|
|
116
|
+
|
|
117
|
+
Used by ask_agent to push child agent events to the parent's stream.
|
|
118
|
+
Returns None if not in a streaming context.
|
|
119
|
+
"""
|
|
120
|
+
return _parent_event_sink.get()
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def set_event_sink(sink: "asyncio.Queue | None") -> None:
|
|
124
|
+
"""Set the event sink for child agents to push events to."""
|
|
125
|
+
_parent_event_sink.set(sink)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
@contextmanager
|
|
129
|
+
def event_sink_scope(sink: "asyncio.Queue") -> Generator["asyncio.Queue", None, None]:
|
|
130
|
+
"""
|
|
131
|
+
Context manager for scoped event sink setting.
|
|
132
|
+
|
|
133
|
+
Used by streaming layer to set up event proxying before tool execution.
|
|
134
|
+
Child agents (via ask_agent) will push their events to this sink.
|
|
135
|
+
|
|
136
|
+
Example:
|
|
137
|
+
event_queue = asyncio.Queue()
|
|
138
|
+
with event_sink_scope(event_queue):
|
|
139
|
+
# ask_agent will push child events to event_queue
|
|
140
|
+
async for event in tools_stream:
|
|
141
|
+
...
|
|
142
|
+
# Also consume from event_queue
|
|
143
|
+
"""
|
|
144
|
+
previous = _parent_event_sink.get()
|
|
145
|
+
_parent_event_sink.set(sink)
|
|
146
|
+
try:
|
|
147
|
+
yield sink
|
|
148
|
+
finally:
|
|
149
|
+
_parent_event_sink.set(previous)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
async def push_event(event: Any) -> bool:
|
|
153
|
+
"""
|
|
154
|
+
Push an event to the parent's event sink (if available).
|
|
155
|
+
|
|
156
|
+
Used by ask_agent to proxy child agent events to the parent's stream.
|
|
157
|
+
Returns True if event was pushed, False if no sink available.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
event: Any streaming event (ToolCallEvent, content chunk, etc.)
|
|
161
|
+
|
|
162
|
+
Returns:
|
|
163
|
+
True if event was pushed to sink, False otherwise
|
|
164
|
+
"""
|
|
165
|
+
sink = _parent_event_sink.get()
|
|
166
|
+
if sink is not None:
|
|
167
|
+
await sink.put(event)
|
|
168
|
+
return True
|
|
169
|
+
return False
|
|
170
|
+
|
|
171
|
+
|
|
33
172
|
class AgentContext(BaseModel):
|
|
34
173
|
"""
|
|
35
174
|
Session and configuration context for agent execution.
|
|
@@ -85,6 +224,40 @@ class AgentContext(BaseModel):
|
|
|
85
224
|
|
|
86
225
|
model_config = {"populate_by_name": True}
|
|
87
226
|
|
|
227
|
+
def child_context(
|
|
228
|
+
self,
|
|
229
|
+
agent_schema_uri: str | None = None,
|
|
230
|
+
model_override: str | None = None,
|
|
231
|
+
) -> "AgentContext":
|
|
232
|
+
"""
|
|
233
|
+
Create a child context for nested agent calls.
|
|
234
|
+
|
|
235
|
+
Inherits user_id, tenant_id, session_id, is_eval from parent.
|
|
236
|
+
Allows overriding agent_schema_uri and default_model for the child.
|
|
237
|
+
|
|
238
|
+
Args:
|
|
239
|
+
agent_schema_uri: Agent schema for the child agent (required for lineage)
|
|
240
|
+
model_override: Optional model override for child agent
|
|
241
|
+
|
|
242
|
+
Returns:
|
|
243
|
+
New AgentContext for the child agent
|
|
244
|
+
|
|
245
|
+
Example:
|
|
246
|
+
parent_context = get_current_context()
|
|
247
|
+
child_context = parent_context.child_context(
|
|
248
|
+
agent_schema_uri="sentiment-analyzer"
|
|
249
|
+
)
|
|
250
|
+
agent = await create_agent(context=child_context)
|
|
251
|
+
"""
|
|
252
|
+
return AgentContext(
|
|
253
|
+
user_id=self.user_id,
|
|
254
|
+
tenant_id=self.tenant_id,
|
|
255
|
+
session_id=self.session_id,
|
|
256
|
+
default_model=model_override or self.default_model,
|
|
257
|
+
agent_schema_uri=agent_schema_uri or self.agent_schema_uri,
|
|
258
|
+
is_eval=self.is_eval,
|
|
259
|
+
)
|
|
260
|
+
|
|
88
261
|
@staticmethod
|
|
89
262
|
def get_user_id_or_default(
|
|
90
263
|
user_id: str | None,
|
rem/agentic/context_builder.py
CHANGED
|
@@ -217,11 +217,21 @@ class ContextBuilder:
|
|
|
217
217
|
)
|
|
218
218
|
|
|
219
219
|
# Convert to ContextMessage format
|
|
220
|
+
# For tool messages, wrap content with clear markers so the agent
|
|
221
|
+
# can see previous tool results when the prompt is concatenated
|
|
220
222
|
for msg_dict in session_history:
|
|
223
|
+
role = msg_dict["role"]
|
|
224
|
+
content = msg_dict["content"]
|
|
225
|
+
|
|
226
|
+
if role == "tool":
|
|
227
|
+
# Wrap tool results with clear markers for visibility
|
|
228
|
+
tool_name = msg_dict.get("tool_name", "unknown")
|
|
229
|
+
content = f"[TOOL RESULT: {tool_name}]\n{content}\n[/TOOL RESULT]"
|
|
230
|
+
|
|
221
231
|
messages.append(
|
|
222
232
|
ContextMessage(
|
|
223
|
-
role=
|
|
224
|
-
content=
|
|
233
|
+
role=role,
|
|
234
|
+
content=content,
|
|
225
235
|
)
|
|
226
236
|
)
|
|
227
237
|
|
rem/agentic/mcp/tool_wrapper.py
CHANGED
|
@@ -116,7 +116,7 @@ def create_resource_tool(uri: str, usage: str = "", mcp_server: Any = None) -> T
|
|
|
116
116
|
the artificial MCP distinction between tools and resources.
|
|
117
117
|
|
|
118
118
|
Supports both:
|
|
119
|
-
- Concrete URIs: "rem://
|
|
119
|
+
- Concrete URIs: "rem://agents" -> tool with no parameters
|
|
120
120
|
- Template URIs: "patient-profile://field/{field_key}" -> tool with field_key parameter
|
|
121
121
|
|
|
122
122
|
Args:
|
|
@@ -131,7 +131,7 @@ def create_resource_tool(uri: str, usage: str = "", mcp_server: Any = None) -> T
|
|
|
131
131
|
|
|
132
132
|
Example:
|
|
133
133
|
# Concrete URI -> no-param tool
|
|
134
|
-
tool = create_resource_tool("rem://
|
|
134
|
+
tool = create_resource_tool("rem://agents", "List all agent schemas")
|
|
135
135
|
|
|
136
136
|
# Template URI -> parameterized tool
|
|
137
137
|
tool = create_resource_tool("patient-profile://field/{field_key}", "Get field definition", mcp_server=mcp)
|
|
@@ -732,7 +732,7 @@ async def create_agent(
|
|
|
732
732
|
# the artificial MCP distinction between tools and resources
|
|
733
733
|
#
|
|
734
734
|
# Supports both concrete and template URIs:
|
|
735
|
-
# - Concrete: "rem://
|
|
735
|
+
# - Concrete: "rem://agents" -> no-param tool
|
|
736
736
|
# - Template: "patient-profile://field/{field_key}" -> tool with field_key param
|
|
737
737
|
from ..mcp.tool_wrapper import create_resource_tool
|
|
738
738
|
|
rem/agentic/schema.py
CHANGED
|
@@ -79,7 +79,7 @@ class MCPResourceReference(BaseModel):
|
|
|
79
79
|
|
|
80
80
|
Example (exact URI):
|
|
81
81
|
{
|
|
82
|
-
"uri": "rem://
|
|
82
|
+
"uri": "rem://agents",
|
|
83
83
|
"name": "Agent Schemas",
|
|
84
84
|
"description": "List all available agent schemas"
|
|
85
85
|
}
|
|
@@ -96,7 +96,7 @@ class MCPResourceReference(BaseModel):
|
|
|
96
96
|
default=None,
|
|
97
97
|
description=(
|
|
98
98
|
"Exact resource URI or URI with query parameters. "
|
|
99
|
-
"Examples: 'rem://
|
|
99
|
+
"Examples: 'rem://agents', 'rem://resources?category=drug.*'"
|
|
100
100
|
)
|
|
101
101
|
)
|
|
102
102
|
|
rem/api/main.py
CHANGED
|
@@ -322,7 +322,7 @@ def create_app() -> FastAPI:
|
|
|
322
322
|
|
|
323
323
|
app.add_middleware(
|
|
324
324
|
AuthMiddleware,
|
|
325
|
-
protected_paths=["/api/v1"],
|
|
325
|
+
protected_paths=["/api/v1", "/api/admin"],
|
|
326
326
|
excluded_paths=["/api/auth", "/api/dev", "/api/v1/mcp/auth", "/api/v1/slack"],
|
|
327
327
|
# Allow anonymous when auth is disabled, otherwise use setting
|
|
328
328
|
allow_anonymous=(not settings.auth.enabled) or settings.auth.allow_anonymous,
|
rem/api/mcp_router/server.py
CHANGED
|
@@ -34,6 +34,7 @@ from .resources import (
|
|
|
34
34
|
register_status_resources,
|
|
35
35
|
)
|
|
36
36
|
from .tools import (
|
|
37
|
+
ask_agent,
|
|
37
38
|
ask_rem_agent,
|
|
38
39
|
get_schema,
|
|
39
40
|
ingest_into_rem,
|
|
@@ -203,6 +204,9 @@ def create_mcp_server(is_local: bool = False) -> FastMCP:
|
|
|
203
204
|
mcp.tool()(get_schema)
|
|
204
205
|
mcp.tool()(save_agent)
|
|
205
206
|
|
|
207
|
+
# Register multi-agent tools
|
|
208
|
+
mcp.tool()(ask_agent)
|
|
209
|
+
|
|
206
210
|
# Register test tool only in development environment (not staging/production)
|
|
207
211
|
if settings.environment not in ("staging", "production"):
|
|
208
212
|
mcp.tool()(test_error_handling)
|