From eeb671872a4f10748acf0a42f6097c89448e3eaf Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Sun, 5 Apr 2026 19:33:31 -0400 Subject: [PATCH] fix(chat): replay tool_calls in history so tool context survives follow-ups The history builder was silently dropping tool_calls from prior turns, causing the LLM to lose article/search context on every follow-up. Now reconstructs the assistant tool_call dict + per-call tool result entries. Messages without tool_calls are unaffected. Co-Authored-By: Claude Sonnet 4.6 --- src/fabledassistant/routes/chat.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/fabledassistant/routes/chat.py b/src/fabledassistant/routes/chat.py index 34e7f96..9e83e5a 100644 --- a/src/fabledassistant/routes/chat.py +++ b/src/fabledassistant/routes/chat.py @@ -162,8 +162,19 @@ async def send_message_route(conv_id: int): # Build history from existing messages (excluding system and the placeholder) history = [] for msg in conv.messages: - if msg.role != "system": - history.append({"role": msg.role, "content": msg.content}) + if msg.role == "system": + continue + msg_dict = {"role": msg.role, "content": msg.content or ""} + if msg.tool_calls: + msg_dict["tool_calls"] = [ + {"function": {"name": tc["function"], "arguments": tc["arguments"]}} + for tc in msg.tool_calls + ] + history.append(msg_dict) + for tc in msg.tool_calls: + history.append({"role": "tool", "content": json.dumps(tc.get("result", {}))}) + else: + history.append(msg_dict) model = await get_setting(uid, "default_model", Config.OLLAMA_MODEL) or Config.OLLAMA_MODEL