Add CalDAV calendar integration, LLM-suggested tags, and settings refinements
- CalDAV integration: per-user calendar config, create/list/search events via caldav library, LLM tools for calendar operations from chat - LLM-suggested tags: new tag_suggestions service prompts LLM with existing tags and note content to suggest 3-5 relevant tags; exposed via API endpoints (suggest-tags, append-tag); integrated into editor views (suggest button + clickable pills) and chat tool calls (pills in ToolCallCard with one-click apply) - Settings/model UI refinements, generation task improvements Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ Runs independently of any HTTP connection.
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import time
|
||||
|
||||
from sqlalchemy import update
|
||||
@@ -15,10 +16,13 @@ from fabledassistant.models.conversation import Message
|
||||
from fabledassistant.services.generation_buffer import GenerationBuffer, GenerationState
|
||||
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
|
||||
from fabledassistant.services.tools import get_tools_for_user, execute_tool
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Mistral prefixes tool-call responses with "[TOOL_CALLS]" as visible text
|
||||
_TOOL_CALL_MARKER = re.compile(r"^\s*\[TOOL_CALLS\]\s*", re.IGNORECASE)
|
||||
|
||||
DB_FLUSH_INTERVAL = 5.0 # seconds between partial DB flushes
|
||||
|
||||
|
||||
@@ -87,20 +91,28 @@ async def run_generation(
|
||||
last_flush = time.monotonic()
|
||||
all_tool_calls: list[dict] = []
|
||||
|
||||
# Resolve tools based on user's configured integrations
|
||||
tools = await get_tools_for_user(user_id)
|
||||
logger.info("Starting generation for conv %d: model=%s, tools=%d", conv_id, model, len(tools))
|
||||
|
||||
try:
|
||||
cancelled = False
|
||||
|
||||
for _round in range(MAX_TOOL_ROUNDS + 1):
|
||||
round_tool_calls: list[dict] = []
|
||||
logger.info("Generation round %d started for conv %d (model=%s)", _round, conv_id, model)
|
||||
|
||||
async for chunk in stream_chat_with_tools(messages, model, tools=TOOL_DEFINITIONS):
|
||||
async for chunk in stream_chat_with_tools(messages, model, tools=tools):
|
||||
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})
|
||||
# Filter out "[TOOL_CALLS]" marker from streaming output
|
||||
clean = _TOOL_CALL_MARKER.sub("", chunk.content)
|
||||
if clean:
|
||||
buf.append_event("chunk", {"chunk": clean})
|
||||
|
||||
# Periodic DB flush
|
||||
now = time.monotonic()
|
||||
@@ -112,13 +124,16 @@ async def run_generation(
|
||||
last_flush = now
|
||||
|
||||
elif chunk.type == "tool_calls" and chunk.tool_calls:
|
||||
logger.info("Round %d: model returned %d tool call(s)", _round, len(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", {})
|
||||
logger.info("Executing tool: %s(%s)", tool_name, json.dumps(arguments)[:200])
|
||||
|
||||
result = await execute_tool(user_id, tool_name, arguments)
|
||||
logger.info("Tool %s result: success=%s", tool_name, result.get("success"))
|
||||
|
||||
tool_record = {
|
||||
"function": tool_name,
|
||||
@@ -133,12 +148,19 @@ async def run_generation(
|
||||
buf.append_event("tool_call", {"tool_call": tool_record})
|
||||
|
||||
if cancelled:
|
||||
logger.info("Generation cancelled for conv %d", conv_id)
|
||||
break
|
||||
|
||||
# If no tool calls this round, the LLM gave its final text response
|
||||
if not round_tool_calls:
|
||||
logger.info("Round %d: no tool calls, final content length=%d", _round, len(buf.content_so_far))
|
||||
break
|
||||
|
||||
logger.info("Round %d: %d tool call(s) executed, starting next round", _round, len(round_tool_calls))
|
||||
|
||||
# Strip model artifacts like "[TOOL_CALLS]" from content
|
||||
buf.content_so_far = _TOOL_CALL_MARKER.sub("", buf.content_so_far)
|
||||
|
||||
# Append assistant tool_call message and tool results to conversation
|
||||
# for the next round
|
||||
messages.append({
|
||||
@@ -159,7 +181,12 @@ async def run_generation(
|
||||
# Reset content for the next round (LLM will produce a new response)
|
||||
buf.content_so_far = ""
|
||||
|
||||
# Strip model artifacts from final content
|
||||
buf.content_so_far = _TOOL_CALL_MARKER.sub("", buf.content_so_far)
|
||||
|
||||
# Final save
|
||||
logger.info("Generation complete for conv %d: content_length=%d, tool_calls=%d",
|
||||
conv_id, len(buf.content_so_far), len(all_tool_calls))
|
||||
await _update_message(
|
||||
msg_id,
|
||||
buf.content_so_far,
|
||||
|
||||
Reference in New Issue
Block a user