llm_council/backend/docs_context.py
Irina Levit 3546c04348
Some checks failed
CI / backend-test (push) Successful in 4m9s
CI / frontend-test (push) Failing after 3m48s
CI / lint-python (push) Successful in 1m41s
CI / secret-scanning (push) Successful in 1m20s
CI / dependency-scan (push) Successful in 10m50s
CI / workflow-summary (push) Successful in 1m11s
feat: Major UI/UX improvements and production readiness
## Features Added

### Document Reference System
- Implemented numbered document references (@1, @2, etc.) with autocomplete dropdown
- Added fuzzy filename matching for @filename references
- Document filtering now prioritizes numeric refs > filename refs > all documents
- Autocomplete dropdown appears when typing @ with keyboard navigation (Up/Down, Enter/Tab, Escape)
- Document numbers displayed in UI for easy reference

### Conversation Management
- Added conversation rename functionality with inline editing
- Implemented conversation search (by title and content)
- Search box always visible, even when no conversations exist
- Export reports now replace @N references with actual filenames

### UI/UX Improvements
- Removed debug toggle button
- Improved text contrast in dark mode (better visibility)
- Made input textarea expand to full available width
- Fixed file text color for better readability
- Enhanced document display with numbered badges

### Configuration & Timeouts
- Made HTTP client timeouts configurable (connect, write, pool)
- Added .env.example with all configuration options
- Updated timeout documentation

### Developer Experience
- Added `make test-setup` target for automated test conversation creation
- Test setup script supports TEST_MESSAGE and TEST_DOCS env vars
- Improved Makefile with dev and test-setup targets

### Documentation
- Updated ARCHITECTURE.md with all new features
- Created comprehensive deployment documentation
- Added GPU VM setup guides
- Removed unnecessary markdown files (CLAUDE.md, CONTRIBUTING.md, header.jpg)
- Organized documentation in docs/ directory

### GPU VM / Ollama (Stability + GPU Offload)
- Updated GPU VM docs to reflect the working systemd environment for remote Ollama
- Standardized remote Ollama port to 11434 (and added /v1/models verification)
- Documented required env for GPU offload on this VM:
  - `OLLAMA_MODELS=/mnt/data/ollama`, `HOME=/mnt/data/ollama/home`
  - `OLLAMA_LLM_LIBRARY=cuda_v12` (not `cuda`)
  - `LD_LIBRARY_PATH=/usr/local/lib/ollama:/usr/local/lib/ollama/cuda_v12`

## Technical Changes

### Backend
- Enhanced `docs_context.py` with reference parsing (numeric and filename)
- Added `update_conversation_title` to storage.py
- New endpoints: PATCH /api/conversations/{id}/title, GET /api/conversations/search
- Improved report generation with filename substitution

### Frontend
- Removed debugMode state and related code
- Added autocomplete dropdown component
- Implemented search functionality in Sidebar
- Enhanced ChatInterface with autocomplete and improved textarea sizing
- Updated CSS for better contrast and responsive design

## Files Changed
- Backend: config.py, council.py, docs_context.py, main.py, storage.py
- Frontend: App.jsx, ChatInterface.jsx, Sidebar.jsx, and related CSS files
- Documentation: README.md, ARCHITECTURE.md, new docs/ directory
- Configuration: .env.example, Makefile
- Scripts: scripts/test_setup.py

## Breaking Changes
None - all changes are backward compatible

## Testing
- All existing tests pass
- New test-setup script validates conversation creation workflow
- Manual testing of autocomplete, search, and rename features
2025-12-28 18:15:02 -05:00

107 lines
3.7 KiB
Python

"""Helpers to load and format uploaded markdown docs as prompt context."""
from __future__ import annotations
import re
from typing import Optional, List
from . import documents
def _normalize_filename_for_matching(filename: str) -> str:
"""Normalize filename for matching @filename references."""
# Convert to lowercase, replace spaces/underscores/hyphens with single underscore
normalized = filename.lower()
normalized = re.sub(r'[_\s\-]+', '_', normalized)
# Remove .md extension for matching
normalized = normalized.replace('.md', '')
return normalized
def _extract_filename_references(text: str) -> List[str]:
"""Extract @filename references from text."""
# Match @filename patterns (with or without .md extension)
pattern = r'@([a-zA-Z0-9_\s\-\+\.]+)'
matches = re.findall(pattern, text)
# Normalize each match
return [_normalize_filename_for_matching(m) for m in matches]
def _extract_numeric_references(text: str) -> List[int]:
"""Extract numeric document references like @1, @2, @3 from text."""
# Match @ followed by digits
pattern = r'@(\d+)'
matches = re.findall(pattern, text)
# Convert to integers (1-indexed, will be converted to 0-indexed when used)
return [int(m) for m in matches]
def build_docs_context(
conversation_id: str,
user_query: Optional[str] = None,
*,
max_chars: int = 8000,
max_docs: int = 5
) -> Optional[str]:
"""
Return a single markdown string containing (truncated) docs for a conversation.
If user_query is provided and contains references:
- @1, @2, @3 etc. (numeric): Include documents by their numbered position (1-indexed)
- @filename (text): Include documents whose filenames match (fuzzy matching)
- If both are present, numeric references take precedence
Otherwise, include all documents up to max_docs.
"""
all_metas = documents.list_documents(conversation_id)
if not all_metas:
return None
# Check for numeric references first (e.g., @1, @2, @3)
if user_query:
numeric_refs = _extract_numeric_references(user_query)
if numeric_refs:
# Convert 1-indexed to 0-indexed and filter
filtered_metas = []
for num in numeric_refs:
idx = num - 1 # Convert to 0-indexed
if 0 <= idx < len(all_metas):
filtered_metas.append(all_metas[idx])
if filtered_metas:
all_metas = filtered_metas
else:
# If no numeric refs, check for filename references
refs = _extract_filename_references(user_query)
if refs:
filtered_metas = []
for meta in all_metas:
normalized = _normalize_filename_for_matching(meta.filename)
# Check if any reference matches this filename
if any(ref in normalized or normalized in ref for ref in refs):
filtered_metas.append(meta)
if filtered_metas:
all_metas = filtered_metas
# Limit to max_docs
metas = all_metas[:max_docs]
if not metas:
return None
chunks = []
remaining = max_chars
for meta in metas:
if remaining <= 0:
break
text = documents.read_document_text(conversation_id, meta.id)
header = f"\n\n---\nDOC: {meta.filename} ({meta.bytes} bytes)\n---\n"
body = text
if len(header) >= remaining:
break
remaining -= len(header)
if len(body) > remaining:
body = body[: max(0, remaining - 3)] + "..."
remaining -= len(body)
chunks.append(header + body)
return "".join(chunks).strip() if chunks else None