refactor: replace orchestrator with LangGraph deep-agent supervisors
- Add app/core/deep_agent.py with Home and Floating supervisor graphs using LangGraph create_react_agent (hierarchical pattern) - Strip ChatAgent classes from all 4 agent files, keep @tool functions - Rewrite output_formatter.py for event-based (token/tool_end/mutations) stream - Update device_ws.py to use run_home_stream/run_floating_stream - Rewrite chat.py REST route to use run_home - Add update_core_memory tool to both supervisors - Add langgraph>=0.3.0 to requirements.txt - Remove orchestrator.py, execution_plan.py, agent_registry.py, plans.py - Remove PlanAction, PlanStep, ExecutionPlan, execution_mode from schemas - Update all affected tests to match new API - Remove 6 deprecated test files for deleted modules - Clean up stale docstrings referencing removed orchestrator
This commit is contained in:
@@ -16,15 +16,15 @@ from app.schemas import (
|
||||
|
||||
# ── helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
async def _stream(*pairs: tuple[str, str]):
|
||||
"""Async generator that yields (agent_name, token) pairs."""
|
||||
for pair in pairs:
|
||||
yield pair
|
||||
async def _stream(*events: tuple[str, object]):
|
||||
"""Async generator that yields (event_type, data) tuples."""
|
||||
for event in events:
|
||||
yield event
|
||||
|
||||
|
||||
async def collect(formatter, token_stream):
|
||||
async def collect(formatter, event_stream):
|
||||
frames = []
|
||||
async for frame in formatter.format(token_stream):
|
||||
async for frame in formatter.format(event_stream):
|
||||
frames.append(frame)
|
||||
return frames
|
||||
|
||||
@@ -32,13 +32,14 @@ async def collect(formatter, token_stream):
|
||||
# ── HomeFormatter ─────────────────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_home_formatter_text_block():
|
||||
async def test_home_formatter_text_token():
|
||||
req_id = "req-1"
|
||||
tokens = [
|
||||
("task_agent", '{"type": "text", "content": "Hello world"}'),
|
||||
events = [
|
||||
("token", "Hello world"),
|
||||
("mutations", []),
|
||||
]
|
||||
formatter = HomeFormatter(request_id=req_id, tool_results=[])
|
||||
frames = await collect(formatter, _stream(*tokens))
|
||||
formatter = HomeFormatter(request_id=req_id)
|
||||
frames = await collect(formatter, _stream(*events))
|
||||
|
||||
assert isinstance(frames[0], WsStreamStart)
|
||||
assert frames[0].request_id == req_id
|
||||
@@ -48,104 +49,94 @@ async def test_home_formatter_text_block():
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_home_formatter_chart_block():
|
||||
async def test_home_formatter_entity_ref_from_tool_end():
|
||||
req_id = "req-2"
|
||||
chart_json = (
|
||||
'{"type": "chart", "chartType": "bar", '
|
||||
'"title": "Tasks", "data": [{"x": 1}], '
|
||||
'"config": {"x": {"label": "X", "color": "#fff"}}}'
|
||||
)
|
||||
formatter = HomeFormatter(request_id=req_id, tool_results=[])
|
||||
frames = await collect(formatter, _stream(("task_agent", chart_json)))
|
||||
events = [
|
||||
("tool_end", {"name": "task_agent", "result": "Found 3 tasks."}),
|
||||
("token", "Here are your tasks."),
|
||||
("mutations", []),
|
||||
]
|
||||
formatter = HomeFormatter(request_id=req_id)
|
||||
frames = await collect(formatter, _stream(*events))
|
||||
|
||||
block_frames = [f for f in frames if isinstance(f, WsStreamBlock)]
|
||||
assert len(block_frames) == 1
|
||||
assert block_frames[0].block_type == "chart"
|
||||
assert block_frames[0].data["chartType"] == "bar"
|
||||
assert block_frames[0].block_type == "entity_ref"
|
||||
assert block_frames[0].data["entity"] == "tasks"
|
||||
assert block_frames[0].data["result"] == "Found 3 tasks."
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_home_formatter_invalid_chart_skipped():
|
||||
async def test_home_formatter_unknown_agent_no_block():
|
||||
req_id = "req-3"
|
||||
bad_chart = '{"type": "chart", "chartType": "unknown", "data": []}'
|
||||
formatter = HomeFormatter(request_id=req_id, tool_results=[])
|
||||
frames = await collect(formatter, _stream(("task_agent", bad_chart)))
|
||||
events = [
|
||||
("tool_end", {"name": "unknown_agent", "result": "stuff"}),
|
||||
("mutations", []),
|
||||
]
|
||||
formatter = HomeFormatter(request_id=req_id)
|
||||
frames = await collect(formatter, _stream(*events))
|
||||
|
||||
block_frames = [f for f in frames if isinstance(f, WsStreamBlock)]
|
||||
assert len(block_frames) == 0 # invalid chart skipped
|
||||
assert len(block_frames) == 0 # unknown agent → no entity mapping
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_home_formatter_entity_ref_resolved():
|
||||
async def test_home_formatter_mutations_in_stream_end():
|
||||
req_id = "req-4"
|
||||
tool_results = [{"entity": "task", "id": "t1", "title": "My Task"}]
|
||||
entity_json = '{"type": "entity_ref", "entity": "task"}'
|
||||
formatter = HomeFormatter(request_id=req_id, tool_results=tool_results)
|
||||
frames = await collect(formatter, _stream(("task_agent", entity_json)))
|
||||
muts = [{"action": "insert", "table": "tasks", "data": {"id": "t1"}}]
|
||||
events = [
|
||||
("token", "Done"),
|
||||
("mutations", muts),
|
||||
]
|
||||
formatter = HomeFormatter(request_id=req_id)
|
||||
frames = await collect(formatter, _stream(*events))
|
||||
|
||||
block_frames = [f for f in frames if isinstance(f, WsStreamBlock)]
|
||||
assert len(block_frames) == 1
|
||||
assert block_frames[0].data["entity"] == "task"
|
||||
assert block_frames[0].data["items"][0]["id"] == "t1"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_home_formatter_entity_ref_missing_skipped():
|
||||
req_id = "req-5"
|
||||
entity_json = '{"type": "entity_ref", "entity": "task"}'
|
||||
formatter = HomeFormatter(request_id=req_id, tool_results=[])
|
||||
frames = await collect(formatter, _stream(("task_agent", entity_json)))
|
||||
|
||||
block_frames = [f for f in frames if isinstance(f, WsStreamBlock)]
|
||||
assert len(block_frames) == 0 # no tool results → skipped
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_home_formatter_table_block():
|
||||
req_id = "req-6"
|
||||
table_json = '{"type": "table", "headers": ["A", "B"], "rows": [["1", "2"]]}'
|
||||
formatter = HomeFormatter(request_id=req_id, tool_results=[])
|
||||
frames = await collect(formatter, _stream(("task_agent", table_json)))
|
||||
|
||||
block_frames = [f for f in frames if isinstance(f, WsStreamBlock)]
|
||||
assert len(block_frames) == 1
|
||||
assert block_frames[0].block_type == "table"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_home_formatter_timeline_block():
|
||||
req_id = "req-7"
|
||||
timeline_json = '{"type": "timeline", "timelines": [{"id": "c1", "title": "M1", "date": 123}]}'
|
||||
formatter = HomeFormatter(request_id=req_id, tool_results=[])
|
||||
frames = await collect(formatter, _stream(("task_agent", timeline_json)))
|
||||
|
||||
block_frames = [f for f in frames if isinstance(f, WsStreamBlock)]
|
||||
assert len(block_frames) == 1
|
||||
assert block_frames[0].block_type == "timeline"
|
||||
end_frame = frames[-1]
|
||||
assert isinstance(end_frame, WsStreamEnd)
|
||||
assert len(end_frame.mutations) == 1
|
||||
assert end_frame.mutations[0]["action"] == "insert"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_home_formatter_frame_order():
|
||||
"""stream_start is first, stream_end is last."""
|
||||
req_id = "req-8"
|
||||
formatter = HomeFormatter(request_id=req_id, tool_results=[])
|
||||
frames = await collect(formatter, _stream(("task_agent", '{"type": "text", "content": "Hi"}')))
|
||||
req_id = "req-5"
|
||||
formatter = HomeFormatter(request_id=req_id)
|
||||
frames = await collect(formatter, _stream(("token", "Hi"), ("mutations", [])))
|
||||
assert isinstance(frames[0], WsStreamStart)
|
||||
assert isinstance(frames[-1], WsStreamEnd)
|
||||
|
||||
|
||||
# ── FloatingFormatter ────────────────────────────────────────────────────────────
|
||||
@pytest.mark.asyncio
|
||||
async def test_home_formatter_multiple_tool_ends():
|
||||
req_id = "req-6"
|
||||
events = [
|
||||
("tool_end", {"name": "task_agent", "result": "3 tasks"}),
|
||||
("tool_end", {"name": "project_agent", "result": "2 projects"}),
|
||||
("token", "Overview done."),
|
||||
("mutations", []),
|
||||
]
|
||||
formatter = HomeFormatter(request_id=req_id)
|
||||
frames = await collect(formatter, _stream(*events))
|
||||
|
||||
block_frames = [f for f in frames if isinstance(f, WsStreamBlock)]
|
||||
assert len(block_frames) == 2
|
||||
entities = {b.data["entity"] for b in block_frames}
|
||||
assert entities == {"tasks", "projects"}
|
||||
|
||||
|
||||
# ── FloatingFormatter ─────────────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_floating_formatter_domain_emitted_first():
|
||||
async def test_floating_formatter_domain_from_tool_end():
|
||||
req_id = "pop-1"
|
||||
formatter = FloatingFormatter(request_id=req_id)
|
||||
tokens = [
|
||||
("task_agent", ""), # domain signal
|
||||
("task_agent", "Hello"),
|
||||
("task_agent", " there"),
|
||||
events = [
|
||||
("tool_end", {"name": "task_agent", "result": "ok"}),
|
||||
("token", "Hello"),
|
||||
("mutations", []),
|
||||
]
|
||||
frames = await collect(formatter, _stream(*tokens))
|
||||
frames = await collect(formatter, _stream(*events))
|
||||
|
||||
assert isinstance(frames[0], WsFloatingDomain)
|
||||
assert frames[0].domain == "tasks"
|
||||
@@ -156,8 +147,12 @@ async def test_floating_formatter_domain_emitted_first():
|
||||
async def test_floating_formatter_text_only():
|
||||
req_id = "pop-2"
|
||||
formatter = FloatingFormatter(request_id=req_id)
|
||||
tokens = [("timeline_agent", ""), ("timeline_agent", "Summary")]
|
||||
frames = await collect(formatter, _stream(*tokens))
|
||||
events = [
|
||||
("tool_end", {"name": "timeline_agent", "result": "done"}),
|
||||
("token", "Summary"),
|
||||
("mutations", []),
|
||||
]
|
||||
frames = await collect(formatter, _stream(*events))
|
||||
|
||||
assert isinstance(frames[0], WsFloatingDomain)
|
||||
assert frames[0].domain == "timelines"
|
||||
@@ -171,11 +166,12 @@ async def test_floating_formatter_no_block_frames():
|
||||
"""FloatingFormatter must never emit WsStreamBlock."""
|
||||
req_id = "pop-3"
|
||||
formatter = FloatingFormatter(request_id=req_id)
|
||||
tokens = [
|
||||
("note_agent", ""),
|
||||
("note_agent", '{"type": "chart", "chartType": "bar", "data": []}'),
|
||||
events = [
|
||||
("tool_end", {"name": "note_agent", "result": "data"}),
|
||||
("token", "some text"),
|
||||
("mutations", []),
|
||||
]
|
||||
frames = await collect(formatter, _stream(*tokens))
|
||||
frames = await collect(formatter, _stream(*events))
|
||||
assert not any(isinstance(f, WsStreamBlock) for f in frames)
|
||||
|
||||
|
||||
@@ -183,13 +179,37 @@ async def test_floating_formatter_no_block_frames():
|
||||
async def test_floating_formatter_end_frame():
|
||||
req_id = "pop-4"
|
||||
formatter = FloatingFormatter(request_id=req_id)
|
||||
frames = await collect(formatter, _stream(("project_agent", ""), ("project_agent", "Done")))
|
||||
events = [
|
||||
("tool_end", {"name": "project_agent", "result": "ok"}),
|
||||
("token", "Done"),
|
||||
("mutations", []),
|
||||
]
|
||||
frames = await collect(formatter, _stream(*events))
|
||||
assert isinstance(frames[-1], WsStreamEnd)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_floating_formatter_unknown_agent_defaults_to_tasks():
|
||||
async def test_floating_formatter_default_domain_on_early_token():
|
||||
"""When the first event is a token (no tool_end yet), default to 'tasks'."""
|
||||
req_id = "pop-5"
|
||||
formatter = FloatingFormatter(request_id=req_id)
|
||||
frames = await collect(formatter, _stream(("unknown_agent", ""), ("unknown_agent", "hi")))
|
||||
events = [("token", "hi"), ("mutations", [])]
|
||||
frames = await collect(formatter, _stream(*events))
|
||||
assert isinstance(frames[0], WsFloatingDomain)
|
||||
assert frames[0].domain == "tasks"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_floating_formatter_mutations_in_stream_end():
|
||||
req_id = "pop-6"
|
||||
muts = [{"action": "update", "table": "tasks", "data": {"id": "t2"}}]
|
||||
events = [
|
||||
("token", "Updated"),
|
||||
("mutations", muts),
|
||||
]
|
||||
formatter = FloatingFormatter(request_id=req_id)
|
||||
frames = await collect(formatter, _stream(*events))
|
||||
|
||||
end_frame = frames[-1]
|
||||
assert isinstance(end_frame, WsStreamEnd)
|
||||
assert len(end_frame.mutations) == 1
|
||||
|
||||
Reference in New Issue
Block a user