Add note context visibility in chat and standardize UI design tokens
Improve chat context: build_context() now returns metadata about auto-found notes, emitted as an SSE event so the frontend can display context pills showing which notes influenced the response. Users can promote notes for deeper context (+) or exclude irrelevant ones (x). A note picker lets users manually attach notes. Multi-word search uses per-term AND matching, and auto-search iterates keywords individually for broader OR-style coverage. Standardize styling: introduce CSS design tokens (--radius-sm/md/lg/pill, --color-success/warning/overlay, --focus-ring) and migrate all components to use them. Fix header alignment to full-width, add active nav link state, replace hardcoded colors with CSS variables, and normalize button padding. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -14,10 +14,13 @@ async def create_conversation(title: str = "", model: str = "") -> Conversation:
|
||||
conv = Conversation(title=title, model=model)
|
||||
session.add(conv)
|
||||
await session.commit()
|
||||
await session.refresh(conv)
|
||||
# Initialize empty messages list for to_dict()
|
||||
conv.messages = []
|
||||
return conv
|
||||
# Re-fetch with messages eagerly loaded to avoid lazy-load in async context
|
||||
result = await session.execute(
|
||||
select(Conversation)
|
||||
.options(selectinload(Conversation.messages))
|
||||
.where(Conversation.id == conv.id)
|
||||
)
|
||||
return result.scalars().first()
|
||||
|
||||
|
||||
async def get_conversation(conversation_id: int) -> Conversation | None:
|
||||
|
||||
@@ -138,8 +138,14 @@ async def build_context(
|
||||
history: list[dict],
|
||||
current_note_id: int | None,
|
||||
user_message: str,
|
||||
) -> list[dict]:
|
||||
"""Build messages array for Ollama with system prompt and context."""
|
||||
exclude_note_ids: list[int] | None = None,
|
||||
) -> tuple[list[dict], dict]:
|
||||
"""Build messages array for Ollama with system prompt and context.
|
||||
|
||||
Returns (messages, context_meta) where context_meta contains info about
|
||||
which notes were included as context.
|
||||
"""
|
||||
exclude_set = set(exclude_note_ids or [])
|
||||
assistant_name = await get_setting("assistant_name", "Fable")
|
||||
system_parts = [
|
||||
f"You are a helpful assistant named {assistant_name}, integrated into a note-taking and task-tracking app called Fabled Assistant. "
|
||||
@@ -147,10 +153,18 @@ async def build_context(
|
||||
"When note context is provided, use it to give relevant answers."
|
||||
]
|
||||
|
||||
context_meta: dict = {
|
||||
"context_note_id": None,
|
||||
"context_note_title": None,
|
||||
"auto_notes": [],
|
||||
}
|
||||
|
||||
# Include current note context if provided
|
||||
if current_note_id:
|
||||
note = await get_note(current_note_id)
|
||||
if note:
|
||||
context_meta["context_note_id"] = note.id
|
||||
context_meta["context_note_title"] = note.title
|
||||
system_parts.append(
|
||||
f"\n\n--- Current Note ---\n"
|
||||
f"Title: {note.title}\n"
|
||||
@@ -158,28 +172,39 @@ async def build_context(
|
||||
f"--- End Note ---"
|
||||
)
|
||||
|
||||
# Search notes by keywords from user message
|
||||
# Search notes by keywords from user message — search per keyword
|
||||
# individually and deduplicate to get OR-style matching (any keyword hit
|
||||
# is relevant context). Collect up to 3 unique notes.
|
||||
keywords = _extract_keywords(user_message)
|
||||
if keywords:
|
||||
search_q = " ".join(keywords[:3])
|
||||
try:
|
||||
notes, _ = await list_notes(q=search_q, limit=3)
|
||||
if notes:
|
||||
snippets = []
|
||||
seen_ids: set[int] = set()
|
||||
snippets: list[str] = []
|
||||
for kw in keywords[:5]:
|
||||
if len(snippets) >= 3:
|
||||
break
|
||||
try:
|
||||
notes, _ = await list_notes(q=kw, limit=3)
|
||||
for n in notes:
|
||||
# Skip the current note (already included)
|
||||
if n.id in seen_ids:
|
||||
continue
|
||||
if current_note_id and n.id == current_note_id:
|
||||
continue
|
||||
body_preview = n.body[:300] if n.body else ""
|
||||
if n.id in exclude_set:
|
||||
continue
|
||||
seen_ids.add(n.id)
|
||||
body_preview = n.body[:2000] if n.body else ""
|
||||
snippets.append(f"- {n.title}: {body_preview}")
|
||||
if snippets:
|
||||
system_parts.append(
|
||||
"\n\n--- Related Notes ---\n"
|
||||
+ "\n".join(snippets)
|
||||
+ "\n--- End Related Notes ---"
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
context_meta["auto_notes"].append({"id": n.id, "title": n.title})
|
||||
if len(snippets) >= 3:
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
if snippets:
|
||||
system_parts.append(
|
||||
"\n\n--- Related Notes ---\n"
|
||||
+ "\n".join(snippets)
|
||||
+ "\n--- End Related Notes ---"
|
||||
)
|
||||
|
||||
# Fetch URL content from user message
|
||||
urls = _find_urls(user_message)
|
||||
@@ -193,4 +218,4 @@ async def build_context(
|
||||
messages = [{"role": "system", "content": "".join(system_parts)}]
|
||||
messages.extend(history)
|
||||
messages.append({"role": "user", "content": user_message})
|
||||
return messages
|
||||
return messages, context_meta
|
||||
|
||||
@@ -60,11 +60,13 @@ async def list_notes(
|
||||
count_query = count_query.where(Note.status.is_(None))
|
||||
|
||||
if q:
|
||||
escaped_q = q.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
|
||||
pattern = f"%{escaped_q}%"
|
||||
filter_ = or_(Note.title.ilike(pattern), Note.body.ilike(pattern))
|
||||
query = query.where(filter_)
|
||||
count_query = count_query.where(filter_)
|
||||
terms = q.split()
|
||||
for term in terms:
|
||||
escaped_term = term.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
|
||||
pattern = f"%{escaped_term}%"
|
||||
term_filter = or_(Note.title.ilike(pattern), Note.body.ilike(pattern))
|
||||
query = query.where(term_filter)
|
||||
count_query = count_query.where(term_filter)
|
||||
|
||||
if tags:
|
||||
for i, tag in enumerate(tags):
|
||||
|
||||
Reference in New Issue
Block a user