Add LLM tool calling for creating tasks, notes, and searching from chat

Ollama tool/function calling integration allows the LLM to create tasks,
create notes, and search existing notes on behalf of the user during chat.
Multi-round tool loop (max 5 rounds) lets the model execute tools then
produce a natural language response. Tool results are persisted in a new
JSONB column on messages and rendered as compact cards with linked titles.

- Migration 0013: add tool_calls JSONB column to messages
- New services/tools.py: tool definitions + execute_tool dispatcher
- llm.py: ChatChunk dataclass, stream_chat_with_tools(), date in system prompt
- generation_task.py: multi-round tool call loop with SSE tool_call events
- Frontend: ToolCallRecord type, streamingToolCalls in store, ToolCallCard
  component, rendering in ChatMessage and ChatView streaming bubble

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-14 23:34:36 -05:00
parent f089b16080
commit 8996b45e50
11 changed files with 522 additions and 29 deletions
+89 -18
View File
@@ -4,6 +4,7 @@ Streams from Ollama into a GenerationBuffer, periodically flushing to DB.
Runs independently of any HTTP connection.
"""
import json
import logging
import time
@@ -12,8 +13,9 @@ from sqlalchemy import update
from fabledassistant.models import async_session
from fabledassistant.models.conversation import Message
from fabledassistant.services.generation_buffer import GenerationBuffer, GenerationState
from fabledassistant.services.llm import generate_completion, stream_chat
from fabledassistant.services.llm import ChatChunk, generate_completion, stream_chat, stream_chat_with_tools
from fabledassistant.services.chat import update_conversation_title
from fabledassistant.services.tools import TOOL_DEFINITIONS, execute_tool
logger = logging.getLogger(__name__)
@@ -47,12 +49,20 @@ async def _generate_title(messages: list[dict], model: str) -> str:
return title[:100] if title else ""
async def _update_message(message_id: int, content: str, status: str) -> None:
async def _update_message(
message_id: int,
content: str,
status: str,
tool_calls: list[dict] | None = None,
) -> None:
values: dict = {"content": content, "status": status}
if tool_calls is not None:
values["tool_calls"] = tool_calls
async with async_session() as session:
await session.execute(
update(Message)
.where(Message.id == message_id)
.values(content=content, status=status)
.values(**values)
)
await session.commit()
@@ -68,33 +78,94 @@ async def run_generation(
user_content: str,
) -> None:
"""Stream LLM response into buffer with periodic DB flushes."""
MAX_TOOL_ROUNDS = 5
msg_id = buf.assistant_message_id
# Emit context event
buf.append_event("context", {"context": context_meta})
last_flush = time.monotonic()
all_tool_calls: list[dict] = []
try:
cancelled = False
async for chunk in stream_chat(messages, model):
if buf.cancel_event.is_set():
cancelled = True
for _round in range(MAX_TOOL_ROUNDS + 1):
round_tool_calls: list[dict] = []
async for chunk in stream_chat_with_tools(messages, model, tools=TOOL_DEFINITIONS):
if buf.cancel_event.is_set():
cancelled = True
break
if chunk.type == "content":
buf.content_so_far += chunk.content
buf.append_event("chunk", {"chunk": chunk.content})
# Periodic DB flush
now = time.monotonic()
if now - last_flush >= DB_FLUSH_INTERVAL:
try:
await _update_message(msg_id, buf.content_so_far, "generating")
except Exception:
logger.warning("Failed periodic flush for message %d", msg_id, exc_info=True)
last_flush = now
elif chunk.type == "tool_calls" and chunk.tool_calls:
# Process each tool call
for tc in chunk.tool_calls:
fn = tc.get("function", {})
tool_name = fn.get("name", "")
arguments = fn.get("arguments", {})
result = await execute_tool(user_id, tool_name, arguments)
tool_record = {
"function": tool_name,
"arguments": arguments,
"result": result,
"status": "success" if result.get("success") else "error",
}
round_tool_calls.append(tool_record)
all_tool_calls.append(tool_record)
# Emit tool_call SSE event
buf.append_event("tool_call", {"tool_call": tool_record})
if cancelled:
break
buf.content_so_far += chunk
buf.append_event("chunk", {"chunk": chunk})
# Periodic DB flush
now = time.monotonic()
if now - last_flush >= DB_FLUSH_INTERVAL:
try:
await _update_message(msg_id, buf.content_so_far, "generating")
except Exception:
logger.warning("Failed periodic flush for message %d", msg_id, exc_info=True)
last_flush = now
# If no tool calls this round, the LLM gave its final text response
if not round_tool_calls:
break
# Final save (partial content on cancel is still valid)
await _update_message(msg_id, buf.content_so_far, "complete")
# Append assistant tool_call message and tool results to conversation
# for the next round
messages.append({
"role": "assistant",
"content": buf.content_so_far,
"tool_calls": [
{"function": {"name": tc["function"], "arguments": tc["arguments"]}}
for tc in round_tool_calls
],
})
for tc in round_tool_calls:
messages.append({
"role": "tool",
"content": json.dumps(tc["result"]),
})
# Reset content for the next round (LLM will produce a new response)
buf.content_so_far = ""
# Final save
await _update_message(
msg_id,
buf.content_so_far,
"complete",
tool_calls=all_tool_calls if all_tool_calls else None,
)
# Count non-system messages to decide on title generation
non_system = [m for m in messages if m["role"] != "system"]