Refactor chat handler to drop superseded trailing user messages from history

This commit is contained in:
2026-06-15 21:37:00 +02:00
parent e2d86ff96a
commit bb08eb4a93

View File

@@ -59,6 +59,17 @@ def _collect_sources(blocks: List[Any]) -> List[Any]:
return sources return sources
def _drop_superseded_trailing_user_rows(rows: List[Any]) -> List[Any]:
if not rows or getattr(rows[-1], "role", None) != "user":
return rows
first_trailing_user = len(rows) - 1
while first_trailing_user > 0 and getattr(rows[first_trailing_user - 1], "role", None) == "user":
first_trailing_user -= 1
if first_trailing_user == len(rows) - 1:
return rows
return [*rows[:first_trailing_user], rows[-1]]
async def chat_handler(arguments: Dict[str, Any], context: ToolExecutionContext) -> Dict[str, Any]: async def chat_handler(arguments: Dict[str, Any], context: ToolExecutionContext) -> Dict[str, Any]:
messages = [dict(item) for item in arguments.get("messages") or []] messages = [dict(item) for item in arguments.get("messages") or []]
if context.session_id: if context.session_id:
@@ -74,6 +85,7 @@ async def chat_handler(arguments: Dict[str, Any], context: ToolExecutionContext)
finally: finally:
db.close() db.close()
if rows: if rows:
rows = _drop_superseded_trailing_user_rows(rows)
from ... import main as main_module from ... import main as main_module
supports_vision = await main_module._model_supports_vision(arguments["model"]) supports_vision = await main_module._model_supports_vision(arguments["model"])
override_index = len(rows) - 1 if rows[-1].role == "user" else None override_index = len(rows) - 1 if rows[-1].role == "user" else None