Compare commits

...
16 Commits
Author SHA1 Message Date
jpmschweitzerandClaude Opus 4.5 31486018a5 fix: update endpoint TTL defaults to match namespace TTLs
Build and Push / release (push) Successful in 3s
Build and Push / build (push) Successful in 1m16s
All fetch endpoints now use 2x refresh interval as default TTL.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-08 17:35:28 +01:00
jpmschweitzerandClaude Opus 4.5 4cfad2e8bc fix: double volatile TTLs to survive missed scheduler runs
Build and Push / release (push) Successful in 3s
Build and Push / build (push) Successful in 2m13s
TTL = 2x refresh interval ensures data remains valid even if a
scheduled refresh is delayed or fails.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-08 16:54:18 +01:00
jpmschweitzerandClaude Opus 4.5 1e31e74ad6 fix: update CI workflow to trigger on tag push
Build and Push / release (push) Successful in 3s
Build and Push / build (push) Successful in 1m14s
Matches core-api workflow pattern for auto release/build on version tags.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-07 12:05:31 +01:00
jpmschweitzerandClaude Opus 4.5 72f515bf61 feat: add combined environment endpoint for concurrent weather + air quality fetch
Build and Push / release (release) Failing after 3s
Build and Push / build (release) Successful in 1m19s
- POST /volatile/fetch/environment/{city} fetches both in parallel
- Single geocode lookup shared between API calls
- Uses asyncio.gather() for concurrent external requests
- Fix scheduler executor name (rest_api → rest_api_executor)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-07 11:53:46 +01:00
jpmschweitzer 0c085d603e auto release/build on version tag 2026-01-03 20:39:00 +01:00
jpmschweitzerandClaude Opus 4.5 152b2f28c4 release: v1.6.2 - Stats endpoint, weather/forecast separation
Build and Push / build (release) Successful in 30s
- GET /stats endpoint with Neo4j, Qdrant, Wiki.js, Paperless stats
- Split weather into current (1hr TTL) and forecast (12hr TTL)
- New FORECAST namespace for multi-day outlook

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-30 12:50:44 +01:00
jpmschweitzerandClaude Opus 4.5 46b9bcd7a0 feat: separate current weather from forecast into distinct namespaces
- Add FORECAST namespace for multi-day outlook (12hr TTL)
- WEATHER namespace now stores only current conditions (1hr TTL)
- Split fetch_weather into fetch_current_weather + fetch_forecast
- Add POST /volatile/fetch/forecast/{city} endpoint
- Different update frequencies for efficient caching

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-30 12:44:16 +01:00
jpmschweitzerandClaude Opus 4.5 68eb1add3d feat: add GET /stats endpoint with system statistics
Returns counts for:
- Neo4j: nodes by type (Document, Entity, Collection, Search)
- Qdrant: vectors per collection
- Wiki.js: total page count
- Paperless: documents, tags, correspondents, document types

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-30 12:07:02 +01:00
jpmschweitzerandClaude Opus 4.5 d6b30570a0 release: v1.6.1 - Weather forecasts, sun times, air quality
Build and Push / build (release) Successful in 52s
- Weather fetch now returns 7-day forecasts with UV index
- New /volatile/fetch/sun/{city} endpoint for sunrise/sunset
- New /volatile/fetch/air_quality/{city} endpoint for AQI and pollutants
- OpenMeteoProvider now implements AirQualityProvider interface

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-30 10:13:06 +01:00
jpmschweitzerandClaude Opus 4.5 6b0530ed79 fix: remove dead automated user filtering code
The _is_automated_user method was never called - loop prevention is
handled by debouncing instead. User email filtering was intentionally
removed because the notification email is the page CREATOR, not editor.

- Remove unused _is_automated_user method
- Update test to verify notifications are processed regardless of user
- Remove obsolete test_automated_user_filtering test

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 21:47:55 +01:00
jpmschweitzerandClaude Opus 4.5 0a8c2639a0 docs: update MEMORY_REMEMBER_PLAN with implementation status
Mark all phases as complete (v1.5.0-v1.6.0):
- Settings DB, Phase A, B, C all implemented
- Updated files summary with actual implementations
- Added remaining work section for file upload placeholder

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 21:33:10 +01:00
jpmschweitzerandClaude Opus 4.5 943fcd9bf9 release: v1.6.0 - Memory system with scheduler integration
Build and Push / build (release) Successful in 1m20s
Complete three-tier memory architecture:
- Volatile fetch endpoints for scheduler-driven prefetch
- Unified memory routing in consolidation service
- Paperless document recall in HybridRAG
- External scheduler integration for prefetch tasks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 21:00:35 +01:00
jpmschweitzerandClaude Opus 4.5 910b289c9e feat: integrate external scheduler for prefetch task registration
Add SchedulerClient to communicate with external scheduler service for
registering volatile prefetch tasks discovered during HybridRAG searches.

- Add scheduler_client.py with full REST API for task CRUD operations
- Add scheduler_url config setting (default: http://scheduler:8090)
- Update consolidation service to use scheduler for prefetch registration
- Add scheduler health checks to startup/shutdown lifecycle

When HybridRAG classifies web content as prefetch-worthy, it now creates
scheduled tasks that periodically refresh the volatile cache via the
external scheduler service.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 20:42:37 +01:00
jpmschweitzerandClaude Opus 4.5 c01033505b feat: add Paperless document recall to HybridRAG
Phase C of memory system: Documents are now a retrieval source alongside
wiki, volatile, and web search.

Changes:
- Add enable_documents, document_limit, document_threshold to HybridRAGConfig
- Add paperless_id field to HybridRAGResult
- Add document_ms timing to TimingBreakdown
- Add document search to parallel retrieval (filters doc_type=document)
- Update RRF fusion to include documents as fourth source
- Add document metadata (correspondent, document_type, tags) to results

HybridRAG now searches 4 sources in parallel:
- Wiki (vector + graph merged)
- Volatile cache (priority boost)
- Paperless documents (new)
- Web search

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 15:11:11 +01:00
jpmschweitzerandClaude Opus 4.5 1f47b052d8 feat: add unified memory routing to consolidation service
- Add MemoryRouteClassification and MemoryRoutingResult models
- Implement unified classifier (_classify_web_results_unified) that routes
  web results to: wiki, volatile, file (Paperless), prefetch, or skip
- Add routing methods: _route_to_volatile, _route_to_files, _register_prefetch
- Update _process_search to use unified classifier instead of separate analysis
- Add get_volatile_cache_service factory to dependencies
- Wire volatile_service and settings_client into ConsolidationService
- Update ConsolidationResult/Response with new routing counters

Test fixes:
- Fix WikiJSClient fixtures to use api_token instead of username/password
- Fix entity linking test assertions to expect full user-namespaced paths
- Add sample_unified_classification fixture for new classifier format

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 17:19:20 +01:00
jpmschweitzerandClaude Opus 4.5 ab892745fa feat: add volatile fetch endpoints for scheduler-driven prefetch
- Add VolatileFetchService to orchestrate API fetch and cache storage
- Add POST /volatile/fetch/weather/{city} endpoint
- Add POST /volatile/fetch/news/{category} endpoint
- Add POST /volatile/fetch/stock/{symbol} endpoint
- Add POST /volatile/fetch/crypto/{symbol} endpoint

Endpoints integrate with external API providers (OpenMeteo, NOS/BBC,
AlphaVantage) and store results in volatile cache with configurable TTL.
Designed for scheduler cron jobs to prefetch user-relevant data.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 13:40:40 +01:00
29 changed files with 2816 additions and 228 deletions
+15 -2
View File
@@ -1,12 +1,25 @@
name: Build and Push
on:
release:
types: [published]
push:
tags:
- 'v*'
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Create Gitea Release
run: |
curl -sf -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Content-Type: application/json" \
-d '{"tag_name": "${{ github.ref_name }}", "name": "Release ${{ github.ref_name }}", "body": "Automated release for ${{ github.ref_name }}"}' \
"${{ github.server_url }}/api/v1/repos/${{ github.repository }}/releases"
build:
runs-on: ubuntu-latest
needs: release
steps:
- uses: actions/checkout@v4
+111
View File
@@ -5,6 +5,117 @@ All notable changes to Library Desk will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.7.3] - 2026-01-07
### Fixed
- **Endpoint TTL defaults** - Updated all fetch endpoint defaults to match namespace TTLs (2x refresh interval)
## [1.7.2] - 2026-01-07
### Fixed
- **Volatile TTL doubled** - TTL now 2x refresh interval to survive missed/delayed scheduler runs
## [1.7.1] - 2026-01-07
### Fixed
- **CI workflow** - Updated Gitea Actions to trigger on tag push (matching core-api)
## [1.7.0] - 2026-01-07
### Added
- **Combined Environment Endpoint** - `POST /volatile/fetch/environment/{city}`
- Fetches weather and air quality concurrently with `asyncio.gather()`
- Single geocode lookup shared between both API calls
- More efficient than calling weather and air_quality separately
- Reduces wall-clock time and eliminates redundant geocoding
### Fixed
- **Scheduler executor name** - Fixed `rest_api``rest_api_executor` in SchedulerTask model and register_volatile_fetch() to prevent "Executor module not found" errors
## [1.6.2] - 2025-12-30
### Added
- **System Statistics Endpoint** - `GET /stats`
- Neo4j: node counts by type (Document, Entity, Collection, Search)
- Qdrant: collection counts, total vectors, per-collection breakdown
- Wiki.js: total page count
- Paperless: documents, tags, correspondents, document types
- **Weather/Forecast Separation** - Split weather into two distinct namespaces
- `POST /volatile/fetch/weather/{city}` - Current conditions only (1hr TTL)
- `POST /volatile/fetch/forecast/{city}` - 7-day outlook (12hr TTL)
- Different update frequencies for efficient caching
- `FORECAST` namespace added to volatile namespaces
### Changed
- Weather namespace TTL changed from 30 minutes to 1 hour (current conditions)
- Forecast data now stored separately with 12 hour TTL
## [1.6.1] - 2025-12-30
### Added
- **Weather Forecast Support** - Enhanced weather fetch with 7-day daily forecasts
- Current conditions now include UV index
- Daily forecasts with high/low temps, conditions, precipitation chance, UV max
- Natural language text summary with multi-day outlook
- **Sun Times Endpoint** - `POST /volatile/fetch/sun/{city}`
- Sunrise and sunset times (HH:MM and ISO formats)
- Daylight duration in seconds and hours
- Separate volatile namespace with 24hr TTL
- Useful for home automation light triggers
- **Air Quality Endpoint** - `POST /volatile/fetch/air_quality/{city}`
- European and US AQI indices
- Pollutants: PM2.5, PM10, ozone, nitrogen dioxide, sulphur dioxide, carbon monoxide
- Pollen data (grass, birch, alder) for European locations (seasonal)
- Hourly refresh (1hr TTL)
- **New Base Models**
- `SunTimes` dataclass for sunrise/sunset data
- `AirQuality` dataclass with AQI and pollutants
- `AirQualityProvider` abstract interface
- **New Volatile Namespace** - `SUN` for sunrise/sunset times (86400s default TTL)
### Changed
- Weather fetch now uses `get_forecast()` instead of `get_current()` for richer data
- `OpenMeteoProvider` now implements both `WeatherProvider` and `AirQualityProvider`
## [1.6.0] - 2025-12-29
### Added
- **Memory System Implementation** - Complete three-tier memory architecture
- **Volatile Fetch Endpoints** - Scheduler-driven prefetch for ephemeral data
- `POST /volatile/fetch/{namespace}/{key}` - Fetch and cache external data
- Weather, news, and financial data providers integrated
- Auto-caching with namespace-specific TTLs
- **Unified Memory Routing** - LLM-based classification of web results
- Routes content to wiki (stable), volatile (ephemeral), file (documents), or prefetch (scheduled)
- Integrated into consolidation service post-processor
- **Document Recall in HybridRAG** - Paperless documents as fourth retrieval source
- Documents searched alongside wiki, volatile, and web in parallel
- New config: `enable_documents`, `document_limit`, `document_threshold`
- `paperless_id` field in results for document attribution
- `document_ms` timing in performance breakdown
- **Scheduler Integration** - External scheduler service for prefetch task management
- `SchedulerClient` - Full REST API client for task CRUD operations
- `register_volatile_fetch()` convenience method for prefetch registration
- Consolidation service now creates scheduled tasks for prefetch-worthy content
- Health checks integrated into startup/shutdown lifecycle
### Changed
- HybridRAG now searches 4 sources in parallel (wiki, volatile, documents, web)
- Consolidation service uses external scheduler instead of settings storage for prefetch
## [1.5.0] - 2025-12-26
### Added
-9
View File
@@ -41,12 +41,3 @@ Check for duplicate or highly similar documents using vector similarity and grap
3. Check graph relationships
4. Return candidates with similarity scores
## System Statistics
#### `GET /stats`
Get system statistics (wiki pages, neo4j nodes, qdrant vectors).
**Implementation needed:**
- Query Neo4j for node count
- Query Qdrant for vector count
- Query Wiki.js for page count
+102
View File
@@ -0,0 +1,102 @@
#!/usr/bin/env python3
"""Quick script to check environmental data in Qdrant volatile cache."""
import asyncio
import os
import time
from dotenv import load_dotenv
load_dotenv()
async def main():
from qdrant_client import QdrantClient
from qdrant_client.models import Filter, FieldCondition, MatchValue
from src.config import get_settings
settings = get_settings()
qdrant = QdrantClient(url=settings.qdrant_url)
user = "jpmschweitzer"
collection = f"volatile_{user}"
print(f"\n{'='*60}")
print(f"Volatile Data in Qdrant ({collection})")
print(f"{'='*60}\n")
namespaces = ["weather", "air_quality", "forecast", "sun", "news"]
for ns in namespaces:
try:
results = qdrant.scroll(
collection_name=collection,
scroll_filter=Filter(
must=[FieldCondition(key="namespace", match=MatchValue(value=ns))]
),
limit=10,
with_payload=True,
with_vectors=False,
)
points = results[0]
print(f"=== {ns.upper()} ({len(points)} records) ===")
if not points:
print(" (no data)")
print()
continue
for point in points:
payload = point.payload
raw_data = payload.get("raw_data", {})
if ns == "weather":
print(f" Temperature: {raw_data.get('temperature')}°C (feels like {raw_data.get('feels_like')}°C)")
print(f" Conditions: {raw_data.get('conditions')}")
print(f" Humidity: {raw_data.get('humidity')}%")
print(f" Wind: {raw_data.get('wind_speed')} km/h")
print(f" UV Index: {raw_data.get('uv_index')}")
elif ns == "air_quality":
print(f" European AQI: {raw_data.get('aqi_european')}")
print(f" US AQI: {raw_data.get('aqi_us')}")
print(f" PM2.5: {raw_data.get('pm2_5')} µg/m³")
print(f" PM10: {raw_data.get('pm10')} µg/m³")
print(f" Ozone: {raw_data.get('ozone')} µg/m³")
print(f" NO₂: {raw_data.get('nitrogen_dioxide')} µg/m³")
elif ns == "forecast":
daily = raw_data.get("daily", [])
for day in daily[:5]:
print(f" {day.get('day_name', 'N/A')[:3]}: {day.get('temp_low'):.0f}-{day.get('temp_high'):.0f}°C, {day.get('conditions')}")
elif ns == "sun":
print(f" Sunrise: {raw_data.get('sunrise')}")
print(f" Sunset: {raw_data.get('sunset')}")
print(f" Daylight: {raw_data.get('daylight_hours', 0):.1f} hours")
elif ns == "news":
headlines = raw_data.get("headlines", [])
print(f" Category: {raw_data.get('category', 'general')}")
print(f" Headlines ({len(headlines)}):")
for item in headlines[:5]:
title = item.get("title", "")[:60]
source = item.get("source", "")
print(f" - [{source}] {title}...")
# Show TTL info
ttl_expiry = payload.get("ttl_expiry")
if ttl_expiry:
remaining = (ttl_expiry / 1000) - time.time()
if remaining > 0:
print(f" TTL remaining: {int(remaining)}s ({int(remaining/60)} min)")
else:
print(f" TTL: EXPIRED")
print()
except Exception as e:
print(f" Error fetching {ns}: {e}")
print()
if __name__ == "__main__":
asyncio.run(main())
+53 -31
View File
@@ -16,8 +16,16 @@ This document outlines the implementation of "remember" triggers for the memory
| Memory Tier | Remember Trigger | Recall | Status |
|-------------|------------------|--------|--------|
| Wiki | Wiki.js webhook, Consolidation | HybridRAG vector+graph | ✅ Complete |
| Documents | Paperless webhook | Manual `/documents/search` | ⚠️ Partial (no HybridRAG recall) |
| Volatile | Manual `/volatile/store` only | HybridRAG volatile search | ⚠️ Partial (no auto-triggers) |
| Documents | Paperless webhook | HybridRAG document search | ✅ Complete (v1.6.0) |
| Volatile | Scheduler prefetch, HybridRAG post-processor | HybridRAG volatile search | ✅ Complete (v1.6.0) |
### Implementation Summary (v1.6.0)
- **Settings DB**: Central `system_settings` PostgreSQL database with `SettingsClient`
- **Phase A**: Volatile fetch endpoints (`/volatile/fetch/{namespace}/{key}`) with weather, news, financial providers
- **Phase B**: Unified memory routing in consolidation service (wiki/volatile/file/prefetch/skip classification)
- **Phase C**: Document recall in HybridRAG (4-source parallel retrieval)
- **Scheduler Integration**: `SchedulerClient` for external scheduler task registration
---
@@ -662,46 +670,60 @@ document_threshold: float = Field(default=0.6)
## Implementation Order
| Phase | Priority | Effort | Description |
|-------|----------|--------|-------------|
| **Settings DB** | High | Low | PostgreSQL schema + settings client |
| **B.4** | High | Low | Scheduler client |
| **B.1-B.3** | High | Medium | HybridRAG post-processor |
| **B.5** | High | Low | Config options |
| **C.1-C.2** | High | Low | Document recall in HybridRAG |
| **A.1** | Medium | Low | `/volatile/fetch` endpoint |
| **A.2** | Medium | Medium | Weather + News API clients |
| **A.3** | Medium | Low | Fetch service |
| Phase | Priority | Effort | Description | Status |
|-------|----------|--------|-------------|--------|
| **Settings DB** | High | Low | PostgreSQL schema + settings client | ✅ v1.5.0 |
| **B.4** | High | Low | Scheduler client | ✅ v1.6.0 |
| **B.1-B.3** | High | Medium | HybridRAG post-processor | ✅ v1.6.0 |
| **B.5** | High | Low | Config options | ✅ v1.6.0 |
| **C.1-C.2** | High | Low | Document recall in HybridRAG | ✅ v1.6.0 |
| **A.1** | Medium | Low | `/volatile/fetch` endpoint | ✅ v1.6.0 |
| **A.2** | Medium | Medium | Weather + News API clients | ✅ v1.5.0 |
| **A.3** | Medium | Low | Fetch service | ✅ v1.6.0 |
### Remaining Work
| Item | Description | Status |
|------|-------------|--------|
| File upload | Download PDFs and upload to Paperless | ⚠️ Placeholder (logs only) |
| Prefetch patterns | More sophisticated pattern detection | Optional enhancement |
---
## Files Summary
### New Files
### New Files (Implemented)
| Path | Purpose |
|------|---------|
| `src/clients/settings_client.py` | Read-only central settings access |
| `src/clients/scheduler_client.py` | Register/manage scheduler tasks |
| `src/clients/weather_client.py` | Open-Meteo API (with geocoding) |
| `src/clients/news_client.py` | NOS.nl RSS feeds |
| `src/services/volatile_fetch_service.py` | Orchestrates fetch + store |
| Path | Purpose | Version |
|------|---------|---------|
| `src/clients/settings_client.py` | Central settings database access | v1.5.0 |
| `src/clients/scheduler_client.py` | External scheduler task management | v1.6.0 |
| `src/apis/__init__.py` | External API providers package | v1.5.0 |
| `src/apis/base.py` | Abstract base classes for providers | v1.5.0 |
| `src/apis/weather.py` | OpenMeteoProvider (geocoding + forecast) | v1.5.0 |
| `src/apis/news.py` | AggregatedNewsProvider | v1.5.0 |
| `src/apis/nos.py` | NOSProvider (Dutch news RSS) | v1.5.0 |
| `src/apis/bbc.py` | BBCProvider (English news RSS) | v1.5.0 |
| `src/apis/financial.py` | AlphaVantageProvider (stocks/crypto) | v1.5.0 |
| `src/services/volatile_fetch_service.py` | Orchestrates fetch + store | v1.6.0 |
### Modified Files
| Path | Changes |
|------|---------|
| `src/services/hybrid_rag_service.py` | Post-processor, prefetch detection, document search |
| `src/models/hybrid_rag.py` | Memory config options |
| `src/routers/volatile.py` | `/volatile/fetch/{namespace}/{key}` |
| `src/core/dependencies.py` | Settings client, scheduler client DI |
| `src/config.py` | `SYSTEM_SETTINGS_*` connection vars |
| Path | Changes | Version |
|------|---------|---------|
| `src/services/hybrid_rag_service.py` | Document search (4-source parallel retrieval) | v1.6.0 |
| `src/services/consolidation_service.py` | Unified memory routing, scheduler integration | v1.6.0 |
| `src/models/hybrid_rag.py` | Document config options (`enable_documents`, `document_limit`) | v1.6.0 |
| `src/models/consolidation.py` | Memory routing models | v1.6.0 |
| `src/routers/volatile.py` | `/volatile/fetch/{namespace}/{key}` endpoints | v1.6.0 |
| `src/core/dependencies.py` | Settings, scheduler, provider DI | v1.5.0-v1.6.0 |
| `src/config.py` | `SYSTEM_SETTINGS_*`, `SCHEDULER_URL` vars | v1.5.0-v1.6.0 |
### Database
| Item | Details |
|------|---------|
| Database | `system_settings` (PostgreSQL) |
| Table | `settings (key, value JSONB, category, ...)` |
| Library-desk user | `library_desk_ro` (read-only) |
| Management | Direct psql commands |
| Database | `system_settings` (PostgreSQL on postgres-shared) |
| Table | `settings (key, user_scope, value JSONB, schema JSONB, ...)` |
| Library-desk access | Read-only via `SettingsClient` |
| Management | Direct psql commands (future: CRUD manager UI) |
+1 -1
View File
@@ -1,6 +1,6 @@
[project]
name = "library-desk"
version = "1.5.0"
version = "1.7.3"
description = "Coordination service for The Library system - HybridRAG queries, document ingestion, entity extraction, and knowledge consolidation"
readme = "README.md"
requires-python = ">=3.12"
+8
View File
@@ -27,6 +27,9 @@ from .base import (
DayForecast,
WeatherForecast,
GeoLocation,
SunTimes,
# Air quality models
AirQuality,
# News models
NewsItem,
NewsFeed,
@@ -34,6 +37,7 @@ from .base import (
StockQuote,
# Abstract providers
WeatherProvider,
AirQualityProvider,
NewsProvider,
FinancialProvider,
)
@@ -53,8 +57,12 @@ __all__ = [
"DayForecast",
"WeatherForecast",
"GeoLocation",
"SunTimes",
"WeatherProvider",
"OpenMeteoProvider",
# Air quality
"AirQuality",
"AirQualityProvider",
# News
"NewsItem",
"NewsFeed",
+102 -4
View File
@@ -44,14 +44,18 @@ class CurrentWeather:
condition_text: str # Human-readable description
timestamp: datetime
location: str # City/location name
uv_index: Optional[float] = None # UV index 0-11+
def to_text(self) -> str:
"""Generate natural language description."""
return (
f"Currently {self.temperature:.1f}°C "
f"({self.condition_text}) in {self.location}. "
parts = [
f"Currently {self.temperature:.1f}°C",
f"({self.condition_text}) in {self.location}.",
f"Humidity {self.humidity}%, wind {self.wind_speed:.0f} km/h."
)
]
if self.uv_index is not None:
parts.append(f"UV index: {self.uv_index:.0f}.")
return " ".join(parts)
@dataclass
@@ -64,6 +68,14 @@ class DayForecast:
condition_text: str
precipitation_chance: Optional[int] # Percentage 0-100
precipitation_mm: Optional[float]
uv_index_max: Optional[float] = None # Max UV index for the day
def to_text(self) -> str:
"""Generate natural language description."""
date_str = self.date.strftime("%A") # Day name
precip = f", {self.precipitation_chance}% rain" if self.precipitation_chance else ""
uv = f", UV {self.uv_index_max:.0f}" if self.uv_index_max else ""
return f"{date_str}: {self.temp_high:.0f}°/{self.temp_low:.0f}°C, {self.condition_text}{precip}{uv}"
@dataclass
@@ -84,6 +96,78 @@ class GeoLocation:
admin_area: Optional[str] = None # State/province
@dataclass
class SunTimes:
"""Sunrise/sunset times for a location."""
location: str
date: datetime
sunrise: datetime
sunset: datetime
daylight_duration: int # seconds
solar_noon: Optional[datetime] = None
def to_text(self) -> str:
"""Generate natural language description."""
sunrise_str = self.sunrise.strftime("%H:%M")
sunset_str = self.sunset.strftime("%H:%M")
hours = self.daylight_duration // 3600
minutes = (self.daylight_duration % 3600) // 60
return (
f"Sun times for {self.location} on {self.date.strftime('%A %d %B')}: "
f"Sunrise at {sunrise_str}, sunset at {sunset_str}. "
f"Daylight duration: {hours}h {minutes}m."
)
@dataclass
class AirQuality:
"""Air quality measurements for a location."""
location: str
timestamp: datetime
aqi_european: Optional[int] # European AQI 0-500+
aqi_us: Optional[int] # US AQI 0-500+
pm2_5: Optional[float] # µg/m³
pm10: Optional[float] # µg/m³
ozone: Optional[float] # µg/m³
nitrogen_dioxide: Optional[float] # µg/m³
sulphur_dioxide: Optional[float] # µg/m³
carbon_monoxide: Optional[float] # µg/m³
# Pollen (European data only, seasonal)
pollen_grass: Optional[float] = None
pollen_birch: Optional[float] = None
pollen_alder: Optional[float] = None
def to_text(self) -> str:
"""Generate natural language description."""
parts = [f"Air quality in {self.location}:"]
if self.aqi_european is not None:
level = self._aqi_level(self.aqi_european)
parts.append(f"European AQI {self.aqi_european} ({level}).")
if self.pm2_5 is not None:
parts.append(f"PM2.5: {self.pm2_5:.1f} µg/m³.")
if self.pm10 is not None:
parts.append(f"PM10: {self.pm10:.1f} µg/m³.")
if self.ozone is not None:
parts.append(f"Ozone: {self.ozone:.1f} µg/m³.")
return " ".join(parts)
@staticmethod
def _aqi_level(aqi: int) -> str:
"""Convert AQI to human-readable level."""
if aqi <= 20:
return "good"
elif aqi <= 40:
return "fair"
elif aqi <= 60:
return "moderate"
elif aqi <= 80:
return "poor"
elif aqi <= 100:
return "very poor"
else:
return "hazardous"
# =============================================================================
# News Models
# =============================================================================
@@ -163,6 +247,11 @@ class WeatherProvider(ABC):
"""Get weather forecast for a location."""
pass
@abstractmethod
async def get_sun_times(self, location: GeoLocation) -> SunTimes:
"""Get sunrise/sunset times for today."""
pass
async def get_weather_for_city(self, city: str) -> CurrentWeather:
"""Convenience method: geocode and get current weather."""
location = await self.geocode(city)
@@ -171,6 +260,15 @@ class WeatherProvider(ABC):
return await self.get_current(location)
class AirQualityProvider(ABC):
"""Abstract base class for air quality API providers."""
@abstractmethod
async def get_air_quality(self, location: GeoLocation) -> AirQuality:
"""Get current air quality for a location."""
pass
class NewsProvider(ABC):
"""Abstract base class for news API providers."""
+138 -8
View File
@@ -14,11 +14,14 @@ from typing import Optional
from .base import (
WeatherProvider,
AirQualityProvider,
WeatherCondition,
CurrentWeather,
DayForecast,
WeatherForecast,
GeoLocation,
SunTimes,
AirQuality,
)
logger = logging.getLogger(__name__)
@@ -89,11 +92,12 @@ WMO_DESCRIPTIONS: dict[int, str] = {
}
class OpenMeteoProvider(WeatherProvider):
"""Open-Meteo weather API implementation."""
class OpenMeteoProvider(WeatherProvider, AirQualityProvider):
"""Open-Meteo weather and air quality API implementation."""
GEOCODING_URL = "https://geocoding-api.open-meteo.com/v1/search"
WEATHER_URL = "https://api.open-meteo.com/v1/forecast"
AIR_QUALITY_URL = "https://air-quality-api.open-meteo.com/v1/air-quality"
def __init__(
self,
@@ -194,9 +198,11 @@ class OpenMeteoProvider(WeatherProvider):
"wind_speed_10m",
"wind_direction_10m"
],
"daily": ["uv_index_max"],
"timezone": self.timezone,
"temperature_unit": "celsius",
"wind_speed_unit": "kmh"
"wind_speed_unit": "kmh",
"forecast_days": 1
}
)
response.raise_for_status()
@@ -205,6 +211,12 @@ class OpenMeteoProvider(WeatherProvider):
current = data.get("current", {})
weather_code = current.get("weather_code", 0)
# Get today's UV index from daily data
daily = data.get("daily", {})
uv_index = None
if daily.get("uv_index_max"):
uv_index = daily["uv_index_max"][0]
return CurrentWeather(
temperature=current.get("temperature_2m", 0.0),
feels_like=current.get("apparent_temperature"),
@@ -214,7 +226,8 @@ class OpenMeteoProvider(WeatherProvider):
condition=WMO_CODE_MAP.get(weather_code, WeatherCondition.UNKNOWN),
condition_text=WMO_DESCRIPTIONS.get(weather_code, "Unknown"),
timestamp=datetime.now(),
location=location.name
location=location.name,
uv_index=uv_index
)
except httpx.HTTPError as e:
logger.error(f"Weather request failed for {location.name}: {e}")
@@ -259,7 +272,8 @@ class OpenMeteoProvider(WeatherProvider):
"temperature_2m_max",
"temperature_2m_min",
"precipitation_sum",
"precipitation_probability_max"
"precipitation_probability_max",
"uv_index_max"
],
"timezone": self.timezone,
"temperature_unit": "celsius",
@@ -272,7 +286,14 @@ class OpenMeteoProvider(WeatherProvider):
# Parse current weather
current_data = data.get("current", {})
daily_data = data.get("daily", {})
weather_code = current_data.get("weather_code", 0)
# Get today's UV from daily data
uv_index = None
if daily_data.get("uv_index_max"):
uv_index = daily_data["uv_index_max"][0]
current = CurrentWeather(
temperature=current_data.get("temperature_2m", 0.0),
feels_like=current_data.get("apparent_temperature"),
@@ -282,15 +303,16 @@ class OpenMeteoProvider(WeatherProvider):
condition=WMO_CODE_MAP.get(weather_code, WeatherCondition.UNKNOWN),
condition_text=WMO_DESCRIPTIONS.get(weather_code, "Unknown"),
timestamp=datetime.now(),
location=location.name
location=location.name,
uv_index=uv_index
)
# Parse daily forecast
daily_data = data.get("daily", {})
daily = []
dates = daily_data.get("time", [])
for i, date_str in enumerate(dates):
code = daily_data.get("weather_code", [])[i] if i < len(daily_data.get("weather_code", [])) else 0
uv_max = daily_data.get("uv_index_max", [])[i] if i < len(daily_data.get("uv_index_max", [])) else None
daily.append(DayForecast(
date=datetime.fromisoformat(date_str),
temp_high=daily_data.get("temperature_2m_max", [])[i] if i < len(daily_data.get("temperature_2m_max", [])) else 0.0,
@@ -298,7 +320,8 @@ class OpenMeteoProvider(WeatherProvider):
condition=WMO_CODE_MAP.get(code, WeatherCondition.UNKNOWN),
condition_text=WMO_DESCRIPTIONS.get(code, "Unknown"),
precipitation_chance=daily_data.get("precipitation_probability_max", [])[i] if i < len(daily_data.get("precipitation_probability_max", [])) else None,
precipitation_mm=daily_data.get("precipitation_sum", [])[i] if i < len(daily_data.get("precipitation_sum", [])) else None
precipitation_mm=daily_data.get("precipitation_sum", [])[i] if i < len(daily_data.get("precipitation_sum", [])) else None,
uv_index_max=uv_max
))
return WeatherForecast(
@@ -309,3 +332,110 @@ class OpenMeteoProvider(WeatherProvider):
except httpx.HTTPError as e:
logger.error(f"Forecast request failed for {location.name}: {e}")
raise ValueError(f"Failed to get forecast: {e}")
async def get_sun_times(self, location: GeoLocation) -> SunTimes:
"""
Get sunrise/sunset times for today.
Args:
location: GeoLocation with lat/long
Returns:
SunTimes with sunrise, sunset, and daylight duration
Raises:
ValueError: If API request fails
"""
try:
response = await self.client.get(
self.WEATHER_URL,
params={
"latitude": location.latitude,
"longitude": location.longitude,
"daily": [
"sunrise",
"sunset",
"daylight_duration"
],
"timezone": self.timezone,
"forecast_days": 1
}
)
response.raise_for_status()
data = response.json()
daily = data.get("daily", {})
date_str = daily.get("time", [""])[0]
sunrise_str = daily.get("sunrise", [""])[0]
sunset_str = daily.get("sunset", [""])[0]
daylight = daily.get("daylight_duration", [0])[0]
return SunTimes(
location=location.name,
date=datetime.fromisoformat(date_str) if date_str else datetime.now(),
sunrise=datetime.fromisoformat(sunrise_str) if sunrise_str else datetime.now(),
sunset=datetime.fromisoformat(sunset_str) if sunset_str else datetime.now(),
daylight_duration=int(daylight) if daylight else 0
)
except httpx.HTTPError as e:
logger.error(f"Sun times request failed for {location.name}: {e}")
raise ValueError(f"Failed to get sun times: {e}")
async def get_air_quality(self, location: GeoLocation) -> AirQuality:
"""
Get current air quality for a location.
Args:
location: GeoLocation with lat/long
Returns:
AirQuality with pollutant measurements and AQI
Raises:
ValueError: If API request fails
"""
try:
response = await self.client.get(
self.AIR_QUALITY_URL,
params={
"latitude": location.latitude,
"longitude": location.longitude,
"current": [
"european_aqi",
"us_aqi",
"pm2_5",
"pm10",
"ozone",
"nitrogen_dioxide",
"sulphur_dioxide",
"carbon_monoxide",
"grass_pollen",
"birch_pollen",
"alder_pollen"
],
"timezone": self.timezone
}
)
response.raise_for_status()
data = response.json()
current = data.get("current", {})
return AirQuality(
location=location.name,
timestamp=datetime.now(),
aqi_european=current.get("european_aqi"),
aqi_us=current.get("us_aqi"),
pm2_5=current.get("pm2_5"),
pm10=current.get("pm10"),
ozone=current.get("ozone"),
nitrogen_dioxide=current.get("nitrogen_dioxide"),
sulphur_dioxide=current.get("sulphur_dioxide"),
carbon_monoxide=current.get("carbon_monoxide"),
pollen_grass=current.get("grass_pollen"),
pollen_birch=current.get("birch_pollen"),
pollen_alder=current.get("alder_pollen")
)
except httpx.HTTPError as e:
logger.error(f"Air quality request failed for {location.name}: {e}")
raise ValueError(f"Failed to get air quality: {e}")
+315
View File
@@ -0,0 +1,315 @@
"""
Client for external Scheduler service.
Registers and manages scheduled tasks for prefetch operations
(weather, news, etc.) discovered through HybridRAG searches.
"""
import httpx
import logging
from typing import Optional, Any
from pydantic import BaseModel, Field
logger = logging.getLogger(__name__)
class SchedulerTask(BaseModel):
"""Task definition for scheduler registration."""
task_name: str = Field(..., description="Unique task identifier")
service: str = Field(default="library-desk", description="Service that owns this task")
executor: str = Field(default="rest_api_executor", description="Executor type")
priority: int = Field(default=50, ge=1, le=100, description="Priority (lower = higher)")
description: Optional[str] = Field(None, description="Human-readable description")
enabled: bool = Field(default=True, description="Whether task is enabled")
max_retries: int = Field(default=3, ge=0, le=10, description="Max retry attempts")
timeout_seconds: int = Field(default=3600, ge=1, description="Execution timeout")
# Schedule (-1 = every, or specific value)
minute: int = Field(default=-1, ge=-1, le=59, description="Minute (-1=every)")
hour: int = Field(default=-1, ge=-1, le=23, description="Hour (-1=every)")
day_of_month: int = Field(default=-1, ge=-1, le=31, description="Day of month (-1=every)")
month: int = Field(default=-1, ge=-1, le=12, description="Month (-1=every)")
day_of_week: int = Field(default=-1, ge=-1, le=6, description="Day of week (-1=every, 0=Mon)")
# Executor config (for rest_api executor)
config: Optional[dict[str, Any]] = Field(None, description="Executor-specific config")
class SchedulerClient:
"""Client for external scheduler service."""
def __init__(self, base_url: str, timeout: float = 30.0):
"""
Initialize scheduler client.
Args:
base_url: Scheduler API base URL (e.g., "http://scheduler:8090")
timeout: HTTP request timeout in seconds
"""
self.base_url = base_url.rstrip("/")
self.timeout = timeout
self._client: Optional[httpx.AsyncClient] = None
async def _get_client(self) -> httpx.AsyncClient:
"""Get or create HTTP client."""
if self._client is None or self._client.is_closed:
self._client = httpx.AsyncClient(
base_url=self.base_url,
timeout=self.timeout,
)
return self._client
async def close(self):
"""Close HTTP client."""
if self._client and not self._client.is_closed:
await self._client.aclose()
self._client = None
logger.info("Scheduler client closed")
async def health_check(self) -> bool:
"""Check scheduler connectivity."""
try:
client = await self._get_client()
response = await client.get("/health")
return response.status_code == 200
except Exception as e:
logger.error(f"Scheduler health check failed: {e}")
return False
async def task_exists(self, task_name: str) -> bool:
"""
Check if a task already exists.
Args:
task_name: Task identifier to check
Returns:
True if task exists, False otherwise.
"""
try:
client = await self._get_client()
response = await client.get(f"/tasks/{task_name}")
return response.status_code == 200
except Exception as e:
logger.error(f"Failed to check task existence: {e}")
return False
async def get_task(self, task_name: str) -> Optional[dict[str, Any]]:
"""
Get task details.
Args:
task_name: Task identifier
Returns:
Task dict or None if not found.
"""
try:
client = await self._get_client()
response = await client.get(f"/tasks/{task_name}")
if response.status_code == 200:
return response.json()
return None
except Exception as e:
logger.error(f"Failed to get task {task_name}: {e}")
return None
async def list_tasks(
self,
service: Optional[str] = None,
enabled: Optional[bool] = None
) -> list[dict[str, Any]]:
"""
List scheduled tasks.
Args:
service: Filter by service name
enabled: Filter by enabled status
Returns:
List of task dicts.
"""
try:
client = await self._get_client()
params = {}
if service:
params["service"] = service
if enabled is not None:
params["enabled"] = enabled
response = await client.get("/tasks", params=params)
if response.status_code == 200:
return response.json()
return []
except Exception as e:
logger.error(f"Failed to list tasks: {e}")
return []
async def create_task(self, task: SchedulerTask) -> Optional[dict[str, Any]]:
"""
Create a new scheduled task.
Args:
task: Task definition
Returns:
Created task dict or None on failure.
"""
try:
client = await self._get_client()
response = await client.post(
"/tasks",
json=task.model_dump(exclude_none=True)
)
if response.status_code == 200:
logger.info(f"Created scheduler task: {task.task_name}")
return response.json()
else:
logger.error(
f"Failed to create task {task.task_name}: "
f"{response.status_code} - {response.text}"
)
return None
except Exception as e:
logger.error(f"Failed to create task {task.task_name}: {e}")
return None
async def update_task(
self,
task_name: str,
updates: dict[str, Any]
) -> Optional[dict[str, Any]]:
"""
Update an existing task.
Args:
task_name: Task identifier
updates: Fields to update
Returns:
Updated task dict or None on failure.
"""
try:
client = await self._get_client()
response = await client.put(f"/tasks/{task_name}", json=updates)
if response.status_code == 200:
logger.info(f"Updated scheduler task: {task_name}")
return response.json()
else:
logger.error(
f"Failed to update task {task_name}: "
f"{response.status_code} - {response.text}"
)
return None
except Exception as e:
logger.error(f"Failed to update task {task_name}: {e}")
return None
async def delete_task(self, task_name: str) -> bool:
"""
Delete a scheduled task.
Args:
task_name: Task identifier
Returns:
True if deleted, False otherwise.
"""
try:
client = await self._get_client()
response = await client.delete(f"/tasks/{task_name}")
if response.status_code == 200:
logger.info(f"Deleted scheduler task: {task_name}")
return True
else:
logger.error(
f"Failed to delete task {task_name}: "
f"{response.status_code} - {response.text}"
)
return False
except Exception as e:
logger.error(f"Failed to delete task {task_name}: {e}")
return False
async def trigger_task(self, task_name: str) -> bool:
"""
Manually trigger a task to run immediately.
Args:
task_name: Task identifier
Returns:
True if triggered, False otherwise.
"""
try:
client = await self._get_client()
response = await client.post(f"/tasks/{task_name}/trigger")
if response.status_code == 200:
logger.info(f"Triggered task: {task_name}")
return True
else:
logger.error(
f"Failed to trigger task {task_name}: "
f"{response.status_code} - {response.text}"
)
return False
except Exception as e:
logger.error(f"Failed to trigger task {task_name}: {e}")
return False
async def register_volatile_fetch(
self,
namespace: str,
key: str,
user: str,
schedule: dict[str, int],
description: Optional[str] = None,
) -> bool:
"""
Register a volatile fetch task for prefetch.
Convenience method to create tasks that call /volatile/fetch endpoints.
Args:
namespace: Volatile namespace (e.g., "weather", "news")
key: Volatile key (e.g., "rotterdam", "nos")
user: User for the fetch
schedule: Cron-like schedule dict (minute, hour, etc.)
description: Human-readable description
Returns:
True if registered (or already exists), False on failure.
"""
task_name = f"volatile_{namespace}_{key}_{user}".replace("-", "_")
# Check if already exists
if await self.task_exists(task_name):
logger.info(f"Prefetch task already exists: {task_name}")
return True
task = SchedulerTask(
task_name=task_name,
service="library-desk",
executor="rest_api_executor",
priority=60, # Background maintenance priority
description=description or f"Prefetch {namespace}/{key} for {user}",
minute=schedule.get("minute", -1),
hour=schedule.get("hour", -1),
day_of_month=schedule.get("day_of_month", -1),
month=schedule.get("month", -1),
day_of_week=schedule.get("day_of_week", -1),
config={
"method": "POST",
"url": f"http://library-desk:8089/volatile/fetch/{namespace}/{key}",
"headers": {
"Content-Type": "application/json"
},
"body": {
"user": user
}
}
)
result = await self.create_task(task)
return result is not None
+3
View File
@@ -128,6 +128,9 @@ class Settings(BaseSettings):
system_settings_user: str = Field(default="settings", description="System settings database user")
system_settings_password: str = Field(default="", description="System settings database password")
# Scheduler Service
scheduler_url: str = Field(default="http://scheduler:8090", description="Scheduler service URL")
@property
def qdrant_url(self) -> str:
"""Computed Qdrant URL."""
+60 -1
View File
@@ -24,6 +24,7 @@ from src.clients.ollama_client import OllamaClient
from src.clients.content_extractor import ContentExtractor
from src.clients.paperless_client import PaperlessClient
from src.clients.settings_client import SettingsClient
from src.clients.scheduler_client import SchedulerClient
from src.apis import (
OpenMeteoProvider,
AggregatedNewsProvider,
@@ -202,6 +203,22 @@ def get_settings_client() -> SettingsClient:
return client
@lru_cache
def get_scheduler_client() -> SchedulerClient:
"""
Get scheduler service client singleton.
Returns:
Initialized SchedulerClient for task management
Note: Used for registering prefetch tasks discovered during HybridRAG searches
"""
settings = get_settings()
client = SchedulerClient(base_url=settings.scheduler_url)
logger.debug(f"Created Scheduler client: {settings.scheduler_url}")
return client
# =============================================================================
# External API Providers
# =============================================================================
@@ -320,6 +337,7 @@ RedisDep = Annotated[aioredis.Redis, Depends(get_redis_client)]
ContentExtractorDep = Annotated[ContentExtractor, Depends(get_content_extractor)]
PaperlessDep = Annotated[PaperlessClient, Depends(get_paperless_client)]
SettingsClientDep = Annotated[SettingsClient, Depends(get_settings_client)]
SchedulerDep = Annotated[SchedulerClient, Depends(get_scheduler_client)]
# External API provider dependencies
WeatherProviderDep = Annotated[OpenMeteoProvider, Depends(get_weather_provider)]
@@ -392,6 +410,17 @@ async def startup_clients():
else:
logger.info("○ System settings not configured")
# Check Scheduler availability
try:
scheduler = get_scheduler_client()
is_healthy = await scheduler.health_check()
if is_healthy:
logger.info(f"✓ Scheduler ready: {settings.scheduler_url}")
else:
logger.warning("✗ Scheduler not responding")
except Exception as e:
logger.error(f"✗ Scheduler health check failed: {e}")
# Qdrant, Wiki.js, SearXNG are lazy-initialized
logger.info("Service clients startup complete")
@@ -461,6 +490,14 @@ async def shutdown_clients():
except Exception as e:
logger.error(f"Error closing settings client: {e}")
# Close scheduler client
try:
scheduler = get_scheduler_client()
await scheduler.close()
logger.info("✓ Scheduler client closed")
except Exception as e:
logger.error(f"Error closing scheduler client: {e}")
logger.info("Service clients shutdown complete")
@@ -556,6 +593,14 @@ async def check_service_health() -> dict:
else:
health["system_settings"] = None # Not configured
# Scheduler
try:
scheduler = get_scheduler_client()
health["scheduler"] = await scheduler.health_check()
except Exception as e:
logger.error(f"Scheduler health check failed: {e}")
health["scheduler"] = False
return health
@@ -597,7 +642,10 @@ def get_consolidation_service() -> "ConsolidationService":
ollama=get_ollama_client(),
wiki=get_wikijs_client(),
settings=get_settings(),
ingestion_service=get_ingestion_service()
ingestion_service=get_ingestion_service(),
volatile_service=get_volatile_cache_service(),
settings_client=get_settings_client(),
scheduler_client=get_scheduler_client(),
)
@@ -638,6 +686,17 @@ def get_rag_search_service() -> "RAGSearchService":
)
@lru_cache
def get_volatile_cache_service() -> "VolatileCacheService":
"""Get VolatileCacheService singleton."""
from src.services.volatile_service import VolatileCacheService
return VolatileCacheService(
qdrant_client=get_qdrant_client(),
ollama_client=get_ollama_client(),
settings=get_settings()
)
# Authentication
from fastapi import Security, HTTPException
from fastapi.security import HTTPBearer
+88 -1
View File
@@ -18,7 +18,7 @@ from pathlib import Path
from src.config import Settings, get_settings, __version__
from src.core.dependencies import (
verify_api_key, QdrantDep, WikiJSDep, OllamaDep, Neo4jDep
verify_api_key, QdrantDep, WikiJSDep, OllamaDep, Neo4jDep, PaperlessDep
)
from src.core.multi_tenancy import DEFAULT_USER
@@ -85,6 +85,14 @@ class HealthResponse(BaseModel):
services: Dict[str, Any]
class StatsResponse(BaseModel):
"""System statistics response model."""
neo4j: Dict[str, int]
qdrant: Dict[str, Any]
wiki_pages: int
paperless: Dict[str, Any]
# Routes
@app.get("/", tags=["Root"])
async def root() -> Dict[str, str]:
@@ -141,6 +149,85 @@ async def health(settings: Settings = Depends(get_settings)) -> HealthResponse:
)
@app.get("/stats", response_model=StatsResponse, tags=["System"])
async def stats(
user: str = Query(default=DEFAULT_USER, description="User identifier"),
neo4j: Neo4jDep = None,
qdrant: QdrantDep = None,
wikijs: WikiJSDep = None,
paperless: PaperlessDep = None,
api_key: str = Depends(verify_api_key)
) -> StatsResponse:
"""
Get system statistics.
Returns counts for:
- Neo4j: nodes by type (Document, Entity, Collection, Search)
- Qdrant: vectors per collection
- Wiki.js: total page count
- Paperless: documents, tags, correspondents, document types
"""
# Neo4j node counts by label
neo4j_stats = {}
try:
for label in ["Document", "Entity", "Collection", "Search"]:
result = await neo4j.execute_query(
f"MATCH (n:{label}) RETURN count(n) as count"
)
neo4j_stats[label.lower() + "_nodes"] = result[0]["count"] if result else 0
except Exception as e:
logger.error(f"Failed to get Neo4j stats: {e}")
neo4j_stats = {"error": str(e)}
# Qdrant collection stats
qdrant_stats = {}
try:
collections = await qdrant.list_collections()
qdrant_stats["collections"] = len(collections)
qdrant_stats["total_vectors"] = sum(c.get("vectors_count", 0) for c in collections)
qdrant_stats["by_collection"] = {
c["name"]: c["vectors_count"] for c in collections
}
except Exception as e:
logger.error(f"Failed to get Qdrant stats: {e}")
qdrant_stats = {"error": str(e)}
# Wiki.js page count
wiki_pages = 0
try:
pages = await wikijs.list_all_pages(user)
wiki_pages = len(pages)
except Exception as e:
logger.warning(f"Failed to get Wiki.js stats: {e}")
# Paperless-ngx document stats
paperless_stats = {}
try:
# Get document count (page_size=1 for efficiency, we just need the count)
docs_result = await paperless.list_documents(page_size=1)
paperless_stats["documents"] = docs_result.get("count", 0)
# Get metadata counts
tags = await paperless.list_tags()
paperless_stats["tags"] = len(tags)
correspondents = await paperless.list_correspondents()
paperless_stats["correspondents"] = len(correspondents)
doc_types = await paperless.list_document_types()
paperless_stats["document_types"] = len(doc_types)
except Exception as e:
logger.warning(f"Failed to get Paperless stats: {e}")
paperless_stats = {"error": str(e)}
return StatsResponse(
neo4j=neo4j_stats,
qdrant=qdrant_stats,
wiki_pages=wiki_pages,
paperless=paperless_stats
)
@app.post("/ingest/check-updates", tags=["Ingestion"])
async def check_updates(
documents: Dict[str, Any],
+50
View File
@@ -34,6 +34,9 @@ class ConsolidationResult(BaseModel):
pages_created: int = 0
pages_updated: int = 0
entities_added: int = 0
volatile_cached: int = 0
files_queued: int = 0
prefetch_registered: int = 0
error: Optional[str] = None
@@ -44,6 +47,53 @@ class ConsolidationResponse(BaseModel):
pages_created: int = Field(description="New wiki pages created")
pages_updated: int = Field(description="Existing pages updated")
entities_added: int = Field(description="New entities added to graph")
volatile_cached: int = Field(default=0, description="Items cached to volatile storage")
files_queued: int = Field(default=0, description="Files queued for Paperless")
prefetch_registered: int = Field(default=0, description="Prefetch patterns registered")
errors: List[str] = Field(default=[], description="Error messages")
results: List[ConsolidationResult] = Field(description="Per-search results")
dry_run: bool = Field(description="Whether this was a dry run")
class MemoryRouteClassification(BaseModel):
"""
Unified classification of a web result for memory routing.
Route types:
- wiki: Stable reference content → wiki page creation/update
- volatile: Ephemeral data (weather, news, prices) → volatile cache
- file: Downloadable file (PDF, doc, xls, images) → Paperless ingestion
- prefetch: Regularly updated source → scheduler registration
- skip: Low value, ads, errors → discard
"""
url: str
title: str
route_type: str = Field(description="One of: wiki, volatile, file, prefetch, skip")
# Wiki routing fields
wiki_action: Optional[str] = Field(default=None, description="create or update")
wiki_path: Optional[str] = Field(default=None, description="Wiki path for page")
wiki_summary: Optional[str] = Field(default=None, description="Summary for wiki page")
# Volatile routing fields
volatile_namespace: Optional[str] = Field(default=None, description="weather, news, financial, etc.")
volatile_key: Optional[str] = Field(default=None, description="Cache key")
volatile_ttl_hours: Optional[int] = Field(default=None, description="TTL in hours")
# Prefetch routing fields
prefetch_cron: Optional[str] = Field(default=None, description="Cron expression for refresh")
prefetch_endpoint: Optional[str] = Field(default=None, description="API endpoint to call")
# Classification metadata
confidence: float = Field(default=0.0, ge=0.0, le=1.0)
reason: str = Field(default="")
class MemoryRoutingResult(BaseModel):
"""Aggregated result of memory routing for a search."""
wiki_routed: int = 0
volatile_cached: int = 0
files_queued: int = 0
prefetch_registered: int = 0
skipped: int = 0
classifications: List[MemoryRouteClassification] = []
+6 -1
View File
@@ -15,15 +15,18 @@ class HybridRAGConfig(BaseModel):
graph_limit: int = Field(default=10, ge=1, le=50, description="Max graph results")
web_limit: int = Field(default=5, ge=1, le=20, description="Max web results")
volatile_limit: int = Field(default=1, ge=1, le=5, description="Max volatile results (typically 1)")
document_limit: int = Field(default=5, ge=1, le=20, description="Max Paperless document results")
enable_vector: bool = Field(default=True, description="Enable vector search")
enable_graph: bool = Field(default=True, description="Enable graph search")
enable_web: bool = Field(default=True, description="Enable web search")
enable_volatile: bool = Field(default=True, description="Enable volatile cache search")
enable_documents: bool = Field(default=True, description="Enable Paperless document search")
enable_reranking: bool = Field(default=True, description="Enable LLM re-ranking")
enable_enrichment: bool = Field(default=True, description="Enable graph enrichment")
final_result_count: int = Field(default=10, ge=1, le=50, description="Final results to return")
rrf_k: int = Field(default=60, ge=1, le=100, description="RRF constant")
volatile_threshold: float = Field(default=0.8, ge=0.5, le=1.0, description="Volatile similarity threshold")
document_threshold: float = Field(default=0.6, ge=0.3, le=1.0, description="Document similarity threshold")
class RelatedDossier(BaseModel):
@@ -37,12 +40,13 @@ class RelatedDossier(BaseModel):
class HybridRAGResult(BaseModel):
"""Single result from HybridRAG query."""
source_type: str = Field(..., description="Source: 'wiki', 'web', 'volatile'")
source_type: str = Field(..., description="Source: 'wiki', 'web', 'volatile', 'document'")
title: str
content: str
url: Optional[str] = Field(None, description="URL for web results")
page_id: Optional[int] = Field(None, description="Page ID for wiki results")
page_path: Optional[str] = Field(None, description="Wiki page path")
paperless_id: Optional[int] = Field(None, description="Paperless document ID")
rrf_score: float = Field(..., description="Reciprocal Rank Fusion score")
final_rank: int = Field(..., description="Final rank after re-ranking")
sources: List[str] = Field(..., description="Which sources included this result")
@@ -57,6 +61,7 @@ class TimingBreakdown(BaseModel):
graph_ms: float = Field(..., description="Phase 1: Graph search")
web_ms: float = Field(..., description="Phase 1: Web search")
volatile_ms: float = Field(default=0, description="Phase 1: Volatile cache search")
document_ms: float = Field(default=0, description="Phase 1: Paperless document search")
fusion_ms: float = Field(..., description="Phase 2: RRF fusion")
enrichment_ms: float = Field(..., description="Phase 3: Graph enrichment")
reranking_ms: float = Field(..., description="Phase 4: LLM re-ranking")
+17 -12
View File
@@ -18,7 +18,9 @@ class VolatileNamespace(str, Enum):
Each namespace can have different default TTLs and refresh schedules.
"""
# Real-time external data
WEATHER = "weather" # Current conditions, forecasts
WEATHER = "weather" # Current conditions (temperature, humidity, wind)
FORECAST = "forecast" # Multi-day weather outlook
SUN = "sun" # Sunrise, sunset, daylight duration
NEWS = "news" # Headlines, breaking news
FINANCIAL = "financial" # Stock prices, exchange rates, crypto
TRANSIT = "transit" # Train/bus schedules, delays, disruptions
@@ -36,18 +38,21 @@ class VolatileNamespace(str, Enum):
# Default TTLs per namespace (in seconds)
# TTL = 2x refresh interval to ensure data survives missed/delayed refreshes
NAMESPACE_DEFAULT_TTL: Dict[str, int] = {
VolatileNamespace.WEATHER: 1800, # 30 min - weather changes slowly
VolatileNamespace.NEWS: 3600, # 1 hour - news cycles
VolatileNamespace.FINANCIAL: 300, # 5 min - markets move fast
VolatileNamespace.TRANSIT: 300, # 5 min - schedules update frequently
VolatileNamespace.TRAFFIC: 600, # 10 min - traffic patterns
VolatileNamespace.AIR_QUALITY: 3600, # 1 hour - air quality stable
VolatileNamespace.SPORTS: 60, # 1 min - live scores
VolatileNamespace.SOCIAL: 600, # 10 min - social notifications
VolatileNamespace.SYSTEM: 60, # 1 min - system health
VolatileNamespace.CONTEXT: 3600, # 1 hour - session context
VolatileNamespace.CUSTOM: 3600, # 1 hour - default for custom
VolatileNamespace.WEATHER: 7200, # 2 hours (hourly refresh)
VolatileNamespace.FORECAST: 86400, # 24 hours (12hr refresh)
VolatileNamespace.SUN: 172800, # 48 hours (daily refresh)
VolatileNamespace.NEWS: 7200, # 2 hours (hourly refresh)
VolatileNamespace.FINANCIAL: 600, # 10 min (5 min refresh)
VolatileNamespace.TRANSIT: 600, # 10 min (5 min refresh)
VolatileNamespace.TRAFFIC: 1200, # 20 min (10 min refresh)
VolatileNamespace.AIR_QUALITY: 7200, # 2 hours (hourly refresh)
VolatileNamespace.SPORTS: 120, # 2 min (1 min refresh)
VolatileNamespace.SOCIAL: 1200, # 20 min (10 min refresh)
VolatileNamespace.SYSTEM: 120, # 2 min (1 min refresh)
VolatileNamespace.CONTEXT: 7200, # 2 hours - session context
VolatileNamespace.CUSTOM: 7200, # 2 hours - default for custom
}
+5 -2
View File
@@ -12,7 +12,8 @@ from src.models.consolidation import ConsolidationRequest, ConsolidationResponse
from src.services.consolidation_service import ConsolidationService
from src.core.dependencies import (
Neo4jDep, OllamaDep, WikiJSDep,
verify_api_key, get_settings, get_ingestion_service
verify_api_key, get_settings, get_ingestion_service,
get_volatile_cache_service, get_settings_client,
)
from src.config import Settings
@@ -34,7 +35,9 @@ def get_consolidation_service(
ollama=ollama_client,
wiki=wiki_client,
settings=settings,
ingestion_service=get_ingestion_service()
ingestion_service=get_ingestion_service(),
volatile_service=get_volatile_cache_service(),
settings_client=get_settings_client(),
)
+383 -1
View File
@@ -19,7 +19,15 @@ from src.models.volatile import (
NAMESPACE_DEFAULT_TTL,
)
from src.services.volatile_service import VolatileCacheService
from src.core.dependencies import verify_api_key, QdrantDep, OllamaDep
from src.services.volatile_fetch_service import VolatileFetchService
from src.core.dependencies import (
verify_api_key,
QdrantDep,
OllamaDep,
get_weather_provider,
get_news_provider,
get_alphavantage_provider,
)
from src.core.multi_tenancy import DEFAULT_USER
from src.config import get_settings
@@ -221,6 +229,380 @@ async def store_volatile(
raise HTTPException(status_code=500, detail=f"Failed to store record: {str(e)}")
@router.post("/fetch/weather/{city}")
async def fetch_weather(
city: str,
user: str = Query(default=DEFAULT_USER, description="User identifier"),
ttl: int = Query(default=7200, ge=60, le=86400, description="TTL in seconds (default 2 hours)"),
qdrant: QdrantDep = None,
ollama: OllamaDep = None,
api_key: str = Depends(verify_api_key)
):
"""
Fetch current weather conditions for a city and store in volatile cache.
Stores temperature, humidity, wind, UV index. For forecasts use /fetch/forecast.
Called by scheduler for hourly prefetch or on-demand.
**Example:**
```
POST /volatile/fetch/weather/amsterdam?user=jpmschweitzer
```
"""
volatile_service = get_volatile_service(qdrant, ollama)
weather_provider = get_weather_provider()
fetch_service = VolatileFetchService(
volatile_service=volatile_service,
weather_provider=weather_provider,
)
result = await fetch_service.fetch_current_weather(user, city, ttl=ttl)
if not result.success:
raise HTTPException(status_code=500, detail=result.error)
return {
"success": True,
"namespace": result.namespace,
"key": result.key,
"record": result.record,
}
@router.post("/fetch/forecast/{city}")
async def fetch_forecast(
city: str,
user: str = Query(default=DEFAULT_USER, description="User identifier"),
days: int = Query(default=7, ge=1, le=16, description="Forecast days (1-16)"),
ttl: int = Query(default=86400, ge=60, le=604800, description="TTL in seconds (default 24 hours)"),
qdrant: QdrantDep = None,
ollama: OllamaDep = None,
api_key: str = Depends(verify_api_key)
):
"""
Fetch weather forecast for a city and store in volatile cache.
Stores multi-day outlook with highs/lows, precipitation, UV.
For current conditions use /fetch/weather.
**Example:**
```
POST /volatile/fetch/forecast/amsterdam?user=jpmschweitzer&days=7
```
"""
volatile_service = get_volatile_service(qdrant, ollama)
weather_provider = get_weather_provider()
fetch_service = VolatileFetchService(
volatile_service=volatile_service,
weather_provider=weather_provider,
)
result = await fetch_service.fetch_forecast(user, city, days=days, ttl=ttl)
if not result.success:
raise HTTPException(status_code=500, detail=result.error)
return {
"success": True,
"namespace": result.namespace,
"key": result.key,
"record": result.record,
}
@router.post("/fetch/news/{category}")
async def fetch_news(
category: str = "general",
user: str = Query(default=DEFAULT_USER, description="User identifier"),
limit: int = Query(default=10, ge=1, le=50, description="Max headlines"),
ttl: int = Query(default=7200, ge=60, le=86400, description="TTL in seconds"),
qdrant: QdrantDep = None,
ollama: OllamaDep = None,
api_key: str = Depends(verify_api_key)
):
"""
Fetch news headlines and store in volatile cache.
Fetches from configured news sources (NOS, BBC) based on user settings.
Categories: general, world, tech, business, politics, etc.
**Example:**
```
POST /volatile/fetch/news/tech?user=jpmschweitzer&limit=15
```
"""
volatile_service = get_volatile_service(qdrant, ollama)
weather_provider = get_weather_provider()
news_provider = await get_news_provider()
fetch_service = VolatileFetchService(
volatile_service=volatile_service,
weather_provider=weather_provider,
news_provider=news_provider,
)
result = await fetch_service.fetch_news(user, category, limit=limit, ttl=ttl)
if not result.success:
raise HTTPException(status_code=500, detail=result.error)
return {
"success": True,
"namespace": result.namespace,
"key": result.key,
"record": result.record,
}
@router.post("/fetch/stock/{symbol}")
async def fetch_stock(
symbol: str,
user: str = Query(default=DEFAULT_USER, description="User identifier"),
ttl: int = Query(default=600, ge=60, le=3600, description="TTL in seconds (default 10 min)"),
qdrant: QdrantDep = None,
ollama: OllamaDep = None,
api_key: str = Depends(verify_api_key)
):
"""
Fetch stock quote and store in volatile cache.
Fetches from Alpha Vantage API. Requires API key configured in settings.
**Example:**
```
POST /volatile/fetch/stock/AAPL?user=jpmschweitzer
```
"""
volatile_service = get_volatile_service(qdrant, ollama)
weather_provider = get_weather_provider()
financial_provider = await get_alphavantage_provider()
if not financial_provider:
raise HTTPException(
status_code=503,
detail="Financial provider not configured (Alpha Vantage API key missing)"
)
fetch_service = VolatileFetchService(
volatile_service=volatile_service,
weather_provider=weather_provider,
financial_provider=financial_provider,
)
result = await fetch_service.fetch_stock(user, symbol, ttl=ttl)
if not result.success:
raise HTTPException(status_code=500, detail=result.error)
return {
"success": True,
"namespace": result.namespace,
"key": result.key,
"record": result.record,
}
@router.post("/fetch/crypto/{symbol}")
async def fetch_crypto(
symbol: str,
market: str = Query(default="USD", description="Market currency"),
user: str = Query(default=DEFAULT_USER, description="User identifier"),
ttl: int = Query(default=600, ge=60, le=3600, description="TTL in seconds (default 10 min)"),
qdrant: QdrantDep = None,
ollama: OllamaDep = None,
api_key: str = Depends(verify_api_key)
):
"""
Fetch cryptocurrency quote and store in volatile cache.
Fetches from Alpha Vantage API. Requires API key configured in settings.
**Example:**
```
POST /volatile/fetch/crypto/BTC?market=EUR&user=jpmschweitzer
```
"""
volatile_service = get_volatile_service(qdrant, ollama)
weather_provider = get_weather_provider()
financial_provider = await get_alphavantage_provider()
if not financial_provider:
raise HTTPException(
status_code=503,
detail="Financial provider not configured (Alpha Vantage API key missing)"
)
fetch_service = VolatileFetchService(
volatile_service=volatile_service,
weather_provider=weather_provider,
financial_provider=financial_provider,
)
result = await fetch_service.fetch_crypto(user, symbol, market=market, ttl=ttl)
if not result.success:
raise HTTPException(status_code=500, detail=result.error)
return {
"success": True,
"namespace": result.namespace,
"key": result.key,
"record": result.record,
}
@router.post("/fetch/sun/{city}")
async def fetch_sun_times(
city: str,
user: str = Query(default=DEFAULT_USER, description="User identifier"),
ttl: int = Query(default=172800, ge=60, le=604800, description="TTL in seconds (default 48 hours)"),
qdrant: QdrantDep = None,
ollama: OllamaDep = None,
api_key: str = Depends(verify_api_key)
):
"""
Fetch sunrise/sunset times for a city and store in volatile cache.
Fetches from Open-Meteo API. Useful for home automation triggers.
**Example:**
```
POST /volatile/fetch/sun/rotterdam?user=jpmschweitzer
```
**Response data includes:**
- sunrise/sunset times (both HH:MM and ISO formats)
- daylight_duration_seconds
- daylight_hours
- Natural language text summary
"""
volatile_service = get_volatile_service(qdrant, ollama)
weather_provider = get_weather_provider()
fetch_service = VolatileFetchService(
volatile_service=volatile_service,
weather_provider=weather_provider,
)
result = await fetch_service.fetch_sun_times(user, city, ttl=ttl)
if not result.success:
raise HTTPException(status_code=500, detail=result.error)
return {
"success": True,
"namespace": result.namespace,
"key": result.key,
"record": result.record,
}
@router.post("/fetch/air_quality/{city}")
async def fetch_air_quality(
city: str,
user: str = Query(default=DEFAULT_USER, description="User identifier"),
ttl: int = Query(default=7200, ge=60, le=86400, description="TTL in seconds (default 2 hours)"),
qdrant: QdrantDep = None,
ollama: OllamaDep = None,
api_key: str = Depends(verify_api_key)
):
"""
Fetch air quality data for a city and store in volatile cache.
Fetches from Open-Meteo Air Quality API.
**Example:**
```
POST /volatile/fetch/air_quality/rotterdam?user=jpmschweitzer
```
**Response data includes:**
- European and US AQI indices
- Pollutants: PM2.5, PM10, ozone, nitrogen dioxide, etc.
- Pollen data (European locations, seasonal)
- Natural language text summary
"""
volatile_service = get_volatile_service(qdrant, ollama)
weather_provider = get_weather_provider()
fetch_service = VolatileFetchService(
volatile_service=volatile_service,
weather_provider=weather_provider,
)
result = await fetch_service.fetch_air_quality(user, city, ttl=ttl)
if not result.success:
raise HTTPException(status_code=500, detail=result.error)
return {
"success": True,
"namespace": result.namespace,
"key": result.key,
"record": result.record,
}
@router.post("/fetch/environment/{city}")
async def fetch_environment(
city: str,
user: str = Query(default=DEFAULT_USER, description="User identifier"),
weather_ttl: int = Query(default=7200, ge=60, le=86400, description="Weather TTL in seconds (default 2 hours)"),
air_quality_ttl: int = Query(default=7200, ge=60, le=86400, description="Air quality TTL in seconds (default 2 hours)"),
qdrant: QdrantDep = None,
ollama: OllamaDep = None,
api_key: str = Depends(verify_api_key)
):
"""
Fetch weather and air quality concurrently for a city.
Performs a single geocode lookup and fetches both weather and air quality
data in parallel, storing both in volatile cache. More efficient than
calling /fetch/weather and /fetch/air_quality separately.
**Example:**
```
POST /volatile/fetch/environment/rotterdam?user=jpmschweitzer
```
**Response includes:**
- weather: Current conditions (temperature, humidity, wind, UV)
- air_quality: AQI indices, pollutants, pollen data
"""
volatile_service = get_volatile_service(qdrant, ollama)
weather_provider = get_weather_provider()
fetch_service = VolatileFetchService(
volatile_service=volatile_service,
weather_provider=weather_provider,
)
result = await fetch_service.fetch_environment(
user, city, weather_ttl=weather_ttl, air_quality_ttl=air_quality_ttl
)
if not result.success:
raise HTTPException(status_code=500, detail="; ".join(result.errors))
return {
"success": True,
"key": result.key,
"weather": {
"success": result.weather.success if result.weather else False,
"record": result.weather.record if result.weather else None,
"error": result.weather.error if result.weather else None,
},
"air_quality": {
"success": result.air_quality.success if result.air_quality else False,
"record": result.air_quality.record if result.air_quality else None,
"error": result.air_quality.error if result.air_quality else None,
},
"errors": result.errors,
}
@router.get("/{namespace}/{key}", response_model=VolatileRecordResponse)
async def get_record(
namespace: str,
+437 -62
View File
@@ -23,7 +23,9 @@ from src.services.wiki_page_writer import WikiPageWriter
from src.models.consolidation import (
SearchQueryInfo,
ConsolidationResult,
ConsolidationResponse
ConsolidationResponse,
MemoryRouteClassification,
MemoryRoutingResult,
)
from src.config import Settings
@@ -41,7 +43,10 @@ class ConsolidationService:
ollama: OllamaClient,
wiki: WikiJSClient,
settings: Settings,
ingestion_service: Optional["IngestionService"] = None
ingestion_service: Optional["IngestionService"] = None,
volatile_service: Optional["VolatileCacheService"] = None,
settings_client: Optional["SettingsClient"] = None,
scheduler_client: Optional["SchedulerClient"] = None,
):
self.neo4j = neo4j
self.ollama = ollama
@@ -49,6 +54,9 @@ class ConsolidationService:
self.settings = settings
self.wiki_page_writer = WikiPageWriter(ollama_client=ollama, settings=settings)
self.ingestion_service = ingestion_service # Optional to avoid circular dependency
self.volatile_service = volatile_service # For ephemeral data caching
self.settings_client = settings_client # For prefetch registration (fallback)
self.scheduler_client = scheduler_client # For scheduler-driven prefetch
async def consolidate_knowledge(
self,
@@ -97,6 +105,9 @@ class ConsolidationService:
total_pages_created = 0
total_pages_updated = 0
total_entities_added = 0
total_volatile_cached = 0
total_files_queued = 0
total_prefetch_registered = 0
errors: List[str] = []
for search in unprocessed:
@@ -112,6 +123,9 @@ class ConsolidationService:
total_pages_created += result.pages_created
total_pages_updated += result.pages_updated
total_entities_added += result.entities_added
total_volatile_cached += result.volatile_cached
total_files_queued += result.files_queued
total_prefetch_registered += result.prefetch_registered
# Mark as processed if not dry run (even if skipped)
# This prevents searches from accumulating when they don't meet criteria
@@ -141,6 +155,9 @@ class ConsolidationService:
pages_created=total_pages_created,
pages_updated=total_pages_updated,
entities_added=total_entities_added,
volatile_cached=total_volatile_cached,
files_queued=total_files_queued,
prefetch_registered=total_prefetch_registered,
errors=errors,
results=results,
dry_run=dry_run
@@ -149,7 +166,8 @@ class ConsolidationService:
logger.info(
f"Consolidation complete: {processed_count}/{len(unprocessed)} searches, "
f"{total_pages_created} pages created, {total_pages_updated} updated, "
f"{total_entities_added} entities added"
f"{total_entities_added} entities, {total_volatile_cached} volatile, "
f"{total_files_queued} files, {total_prefetch_registered} prefetch"
)
return response
@@ -213,6 +231,13 @@ class ConsolidationService:
) -> Optional[ConsolidationResult]:
"""
Process a single search query for knowledge consolidation.
Uses unified memory routing to classify each web result and route to:
- wiki: Stable reference content → wiki page creation/update
- volatile: Ephemeral data → volatile cache
- file: Downloadable documents → Paperless queue
- prefetch: Regular updates → scheduler registration
- skip: Low value content → discard
"""
search_id = search['id']
query = search['query']
@@ -234,97 +259,106 @@ class ConsolidationService:
logger.info(f"Retrieved {len(web_results)} web results")
# Analyze web results with Ollama for novel information
analysis = await self._analyze_web_results(
# Unified classification of all web results
routing_result = await self._classify_web_results_unified(
query=query,
web_results=web_results,
keywords=search.get('keywords', []),
user=user
)
if not analysis or not analysis.get('has_novel_info'):
logger.info("No novel information found")
if not routing_result.classifications:
logger.info("No classifications returned")
return ConsolidationResult(
search_id=search_id,
query=query
)
# Extract consolidation actions
pages_to_create = analysis.get('new_pages', [])
pages_to_update = analysis.get('update_pages', [])
new_entities = analysis.get('new_entities', [])
logger.info(
f"Analysis: {len(pages_to_create)} new pages, "
f"{len(pages_to_update)} updates, {len(new_entities)} entities"
f"Routing: {routing_result.wiki_routed} wiki, "
f"{routing_result.volatile_cached} volatile, "
f"{routing_result.files_queued} files, "
f"{routing_result.prefetch_registered} prefetch, "
f"{routing_result.skipped} skipped"
)
if dry_run:
logger.info("[DRY RUN] Would create/update pages and entities")
logger.info("[DRY RUN] Would route results to destinations")
return ConsolidationResult(
search_id=search_id,
query=query,
pages_created=len(pages_to_create),
pages_updated=len(pages_to_update),
entities_added=len(new_entities)
pages_created=routing_result.wiki_routed,
volatile_cached=routing_result.volatile_cached,
files_queued=routing_result.files_queued,
prefetch_registered=routing_result.prefetch_registered,
)
# Create/update wiki pages
# Process each classification
pages_created = 0
pages_updated = 0
entities_added = 0
volatile_cached = 0
files_queued = 0
prefetch_registered = 0
# Create new pages
for page_data in pages_to_create:
try:
await self._create_or_consolidate_page(
user=user,
title=page_data.get('title'),
path=page_data.get('path'),
summary=page_data.get('summary'),
source_query=query,
web_results=web_results
)
pages_created += 1
logger.info(f"Created page: {page_data.get('title')}")
except Exception as e:
logger.error(f"Failed to create page {page_data.get('title')}: {e}")
# Create URL-to-web_result lookup
url_to_result = {r['url']: r for r in web_results}
# Update existing pages
for page_data in pages_to_update:
try:
await self._update_page_with_facts(
title=page_data.get('title'),
new_facts=page_data.get('new_facts', []),
source_url=page_data.get('source_url'),
user=user
)
pages_updated += 1
logger.info(f"Updated page: {page_data.get('title')}")
except Exception as e:
logger.error(f"Failed to update page {page_data.get('title')}: {e}")
for classification in routing_result.classifications:
web_result = url_to_result.get(classification.url, {})
# Add new entities to graph
for entity_data in new_entities:
try:
await self._add_entity_to_graph(
user=user,
entity_name=entity_data.get('name'),
entity_type=entity_data.get('type'),
description=entity_data.get('description'),
source_search_id=search_id
)
entities_added += 1
logger.info(f"Added entity: {entity_data.get('name')}")
except Exception as e:
logger.error(f"Failed to add entity {entity_data.get('name')}: {e}")
if classification.route_type == 'wiki':
# Route to wiki page creation/update
try:
if classification.wiki_action == 'create':
await self._create_or_consolidate_page(
user=user,
title=classification.title,
path=classification.wiki_path or f"reference/{classification.title.lower().replace(' ', '-')}",
summary=classification.wiki_summary or '',
source_query=query,
web_results=[web_result] if web_result else web_results[:3]
)
pages_created += 1
logger.info(f"Created wiki page: {classification.title}")
elif classification.wiki_action == 'update':
await self._update_page_with_facts(
title=classification.title,
new_facts=[classification.wiki_summary] if classification.wiki_summary else [],
source_url=classification.url,
user=user
)
pages_updated += 1
logger.info(f"Updated wiki page: {classification.title}")
except Exception as e:
logger.error(f"Failed wiki routing for {classification.title}: {e}")
elif classification.route_type == 'volatile':
# Route to volatile cache
if await self._route_to_volatile(classification, web_result, user):
volatile_cached += 1
elif classification.route_type == 'file':
# Route to Paperless queue
if await self._route_to_files(classification, web_result, user):
files_queued += 1
elif classification.route_type == 'prefetch':
# Register prefetch pattern
if await self._register_prefetch(classification, web_result, user):
prefetch_registered += 1
# 'skip' route type - do nothing
return ConsolidationResult(
search_id=search_id,
query=query,
pages_created=pages_created,
pages_updated=pages_updated,
entities_added=entities_added
entities_added=entities_added,
volatile_cached=volatile_cached,
files_queued=files_queued,
prefetch_registered=prefetch_registered,
)
async def _get_web_results(self, search_id: str) -> List[Dict[str, Any]]:
@@ -937,3 +971,344 @@ JSON:"""
logger.debug(f"Added entity to graph: {entity_name} ({entity_type})")
except Exception as e:
logger.error(f"Failed to add entity to graph: {e}")
async def _classify_web_results_unified(
self,
query: str,
web_results: List[Dict[str, Any]],
keywords: List[str],
user: str = "jpmschweitzer"
) -> MemoryRoutingResult:
"""
Unified classification of web results for memory routing.
Each web result is classified into exactly one destination:
- wiki: Stable reference content → wiki page creation/update
- volatile: Ephemeral data (weather, news, prices) → volatile cache
- file: Downloadable file (PDF, doc, xls, images) → Paperless
- prefetch: Regularly updated source → scheduler registration
- skip: Low value, ads, errors → discard
Returns:
MemoryRoutingResult with classifications for each web result
"""
# Fetch existing taxonomy structure for wiki path suggestions
try:
taxonomy_structure = await self.wiki.get_taxonomy_structure(f"users/{user}")
existing_paths_info = self._format_taxonomy_for_prompt(taxonomy_structure)
logger.info(f"Fetched taxonomy with {len(taxonomy_structure)} categories for user {user}")
except Exception as e:
logger.warning(f"Failed to fetch taxonomy structure: {e}")
existing_paths_info = ""
# Build classification prompt
web_summary = "\n\n".join([
f"[{i+1}] Title: {r['title']}\n URL: {r['url']}\n Content: {r['content'][:400]}..."
for i, r in enumerate(web_results[:10])
])
prompt = f"""You are a Memory Router for a personal knowledge system. Classify each web result into ONE destination.
Query: "{query}"
Keywords: {', '.join(keywords) if keywords else 'none'}
Web Results:
{web_summary}
CLASSIFICATION RULES:
**wiki** - Stable reference content worth documenting permanently:
- Factual information about people, places, companies, products
- How-to guides, tutorials, technical documentation
- Historical facts, biographies, definitions
- Content that won't change frequently
**volatile** - Ephemeral data that changes frequently:
- Current weather conditions or forecasts
- Latest news headlines or breaking news
- Stock prices, exchange rates, crypto prices
- Sports scores, live results
- Traffic conditions, transit delays
- Social media trends, notifications
Use namespaces: weather, news, financial, transit, traffic, sports, social, system
**file** - Downloadable documents:
- PDF files (URLs ending in .pdf or containing /pdf/)
- Office documents (.doc, .docx, .xls, .xlsx, .ppt)
- Images (.jpg, .png, .gif when they're primary content)
- CSV/data files
- Any direct download link
**prefetch** - Sources worth checking regularly:
- News feeds or RSS sources
- API endpoints with live data
- Dashboards or status pages
- Only if not already captured by volatile
**skip** - Low value content:
- Ads, paywalled content
- Error pages, 404s
- Duplicate or redundant results
- Content not answering the query
{existing_paths_info}
Return ONLY valid JSON array:
[
{{
"url": "...",
"title": "...",
"route_type": "wiki|volatile|file|prefetch|skip",
"wiki_action": "create|update",
"wiki_path": "category/subcategory/page-name",
"wiki_summary": "What to document",
"volatile_namespace": "weather|news|financial|...",
"volatile_key": "cache-key",
"volatile_ttl_hours": 1,
"prefetch_cron": "0 * * * *",
"prefetch_endpoint": "/volatile/fetch/...",
"confidence": 0.9,
"reason": "Why this classification"
}}
]
Only include fields relevant to the route_type. Set irrelevant fields to null.
JSON:"""
try:
response = await self.ollama.generate_text(
prompt=prompt,
model=self.settings.ollama_model,
stream=False,
temperature=0.0
)
if not response:
logger.warning("Empty response from Ollama for classification")
return MemoryRoutingResult()
# Extract JSON array from response
response_clean = response.strip()
if '[' in response_clean:
json_start = response_clean.find('[')
json_end = response_clean.rfind(']') + 1
response_clean = response_clean[json_start:json_end]
classifications_raw = json.loads(response_clean)
# Parse into MemoryRouteClassification objects
result = MemoryRoutingResult()
for item in classifications_raw:
try:
classification = MemoryRouteClassification(
url=item.get('url', ''),
title=item.get('title', ''),
route_type=item.get('route_type', 'skip'),
wiki_action=item.get('wiki_action'),
wiki_path=item.get('wiki_path'),
wiki_summary=item.get('wiki_summary'),
volatile_namespace=item.get('volatile_namespace'),
volatile_key=item.get('volatile_key'),
volatile_ttl_hours=item.get('volatile_ttl_hours'),
prefetch_cron=item.get('prefetch_cron'),
prefetch_endpoint=item.get('prefetch_endpoint'),
confidence=item.get('confidence', 0.5),
reason=item.get('reason', ''),
)
result.classifications.append(classification)
# Count by route type
if classification.route_type == 'wiki':
result.wiki_routed += 1
elif classification.route_type == 'volatile':
result.volatile_cached += 1
elif classification.route_type == 'file':
result.files_queued += 1
elif classification.route_type == 'prefetch':
result.prefetch_registered += 1
else:
result.skipped += 1
except Exception as e:
logger.warning(f"Failed to parse classification item: {e}")
logger.info(
f"Classification complete: {result.wiki_routed} wiki, "
f"{result.volatile_cached} volatile, {result.files_queued} files, "
f"{result.prefetch_registered} prefetch, {result.skipped} skipped"
)
return result
except json.JSONDecodeError as e:
logger.error(f"Failed to parse classification response as JSON: {e}")
return MemoryRoutingResult()
except Exception as e:
logger.error(f"Classification failed: {e}", exc_info=True)
return MemoryRoutingResult()
async def _route_to_volatile(
self,
classification: MemoryRouteClassification,
web_result: Dict[str, Any],
user: str,
) -> bool:
"""
Route a web result to volatile cache.
Args:
classification: The classification with volatile routing info
web_result: The original web result data
user: User identifier
Returns:
True if successfully cached, False otherwise
"""
if not self.volatile_service:
logger.warning("Volatile service not configured, skipping volatile routing")
return False
namespace = classification.volatile_namespace or "custom"
key = classification.volatile_key or web_result['url'].split('/')[-1]
ttl = (classification.volatile_ttl_hours or 1) * 3600 # Convert hours to seconds
try:
# Store the web result content in volatile cache
data = {
"title": web_result.get('title', ''),
"content": web_result.get('content', ''),
"url": web_result.get('url', ''),
"text": f"{web_result.get('title', '')}: {web_result.get('content', '')[:500]}",
}
await self.volatile_service.store(
user=user,
namespace=namespace,
key=key,
data=data,
source=web_result.get('url', 'web_search'),
ttl=ttl,
)
logger.info(f"Cached to volatile: {namespace}/{key} (ttl={ttl}s)")
return True
except Exception as e:
logger.error(f"Failed to cache to volatile: {e}")
return False
async def _route_to_files(
self,
classification: MemoryRouteClassification,
web_result: Dict[str, Any],
user: str,
) -> bool:
"""
Queue a file for Paperless ingestion.
Args:
classification: The classification with file info
web_result: The original web result data
user: User identifier
Returns:
True if successfully queued, False otherwise
"""
# For now, log the file for manual review or future Paperless integration
url = web_result.get('url', '')
title = web_result.get('title', '')
logger.info(f"File detected for Paperless: {title} ({url})")
# TODO: Implement actual Paperless file upload
# This would involve:
# 1. Download the file
# 2. Upload to Paperless via API
# 3. Add tags based on classification
return True # Placeholder - count as queued
async def _register_prefetch(
self,
classification: MemoryRouteClassification,
web_result: Dict[str, Any],
user: str,
) -> bool:
"""
Register a prefetch pattern with the external scheduler service.
Args:
classification: The classification with prefetch info
web_result: The original web result data
user: User identifier
Returns:
True if successfully registered, False otherwise
"""
if not self.scheduler_client:
logger.warning("Scheduler client not configured, skipping prefetch registration")
return False
# Parse cron pattern into scheduler schedule format
# Format: "minute hour day_of_month month day_of_week"
# Scheduler uses -1 for "every"
cron = classification.prefetch_cron or "0 * * * *"
schedule = self._parse_cron_to_schedule(cron)
# Determine namespace and key from classification
namespace = classification.volatile_namespace or "custom"
key = classification.volatile_key or web_result.get('url', '').split('/')[-1].split('?')[0]
if not key:
logger.warning(f"Could not determine prefetch key for {web_result.get('url')}")
return False
try:
# Use the scheduler client's convenience method to register volatile fetch
success = await self.scheduler_client.register_volatile_fetch(
namespace=namespace,
key=key,
user=user,
schedule=schedule,
description=f"Auto-prefetch: {classification.title or web_result.get('title', 'Unknown')}",
)
if success:
logger.info(f"Registered scheduler task: volatile_{namespace}_{key}_{user}")
return success
except Exception as e:
logger.error(f"Failed to register prefetch with scheduler: {e}")
return False
def _parse_cron_to_schedule(self, cron: str) -> dict:
"""
Parse cron string to scheduler schedule dict.
Args:
cron: Cron-style string (e.g., "0 6 * * *" = 6:00 AM daily)
Returns:
Dict with minute, hour, day_of_month, month, day_of_week
where -1 means "every"
"""
parts = cron.strip().split()
if len(parts) != 5:
# Default to hourly if invalid
return {"minute": 0, "hour": -1}
def parse_part(part: str) -> int:
if part == "*":
return -1
try:
return int(part)
except ValueError:
return -1
return {
"minute": parse_part(parts[0]),
"hour": parse_part(parts[1]),
"day_of_month": parse_part(parts[2]),
"month": parse_part(parts[3]),
"day_of_week": parse_part(parts[4]),
}
+99 -11
View File
@@ -109,8 +109,9 @@ class HybridRAGService:
timing["graph_ms"] = raw_results.get("timing", {}).get("graph_ms", 0)
timing["web_ms"] = raw_results.get("timing", {}).get("web_ms", 0)
timing["volatile_ms"] = raw_results.get("timing", {}).get("volatile_ms", 0)
timing["document_ms"] = raw_results.get("timing", {}).get("document_ms", 0)
# Phase 2: Three-Source RRF Fusion
# Phase 2: Four-Source RRF Fusion
phase2_start = time.time()
# Stage 1: Merge wiki sources (vector + graph) into single ranking
@@ -120,12 +121,13 @@ class HybridRAGService:
k=config.rrf_k
)
# Stage 2: Final RRF between wiki, volatile, and web
# Stage 2: Final RRF between wiki, volatile, document, and web
# Volatile gets priority boost (smaller k = higher contribution per rank)
fused_results = self._reciprocal_rank_fusion(
wiki_results=wiki_merged,
web_results=raw_results.get("web", []),
volatile_results=raw_results.get("volatile", []),
document_results=raw_results.get("document", []),
k=config.rrf_k
)
timing["fusion_ms"] = (time.time() - phase2_start) * 1000
@@ -427,6 +429,63 @@ JSON:"""
tasks["volatile"] = volatile_search()
# Paperless document search (separate from wiki vector search)
if config.enable_documents:
async def document_search():
start = time.time()
try:
# Search in same collection but filter to doc_type=document
from src.core.multi_tenancy import get_qdrant_collection_name
collection_name = get_qdrant_collection_name(user)
# Check if collection exists
exists = await self.vector.qdrant.collection_exists(collection_name)
if not exists:
return [], (time.time() - start) * 1000
# Get query embedding
query_embedding = await self.vector.ollama.embed_text(query)
# Search with filter for doc_type=document
from qdrant_client.models import Filter, FieldCondition, MatchValue
search_results = self.vector.qdrant.client.search(
collection_name=collection_name,
query_vector=query_embedding,
limit=config.document_limit,
score_threshold=config.document_threshold,
query_filter=Filter(
must=[
FieldCondition(
key="doc_type",
match=MatchValue(value="document")
)
]
)
)
# Format results
formatted = []
for r in search_results:
payload = r.payload or {}
formatted.append({
"paperless_id": payload.get("paperless_id"),
"title": payload.get("title", "Untitled Document"),
"content": payload.get("chunk_text", ""),
"score": r.score,
"correspondent": payload.get("correspondent"),
"document_type": payload.get("document_type"),
"tags": payload.get("tags", []),
"original_filename": payload.get("original_filename"),
"source": "document"
})
return formatted, (time.time() - start) * 1000
except Exception as e:
logger.error(f"Document search failed: {e}", exc_info=True)
return [], (time.time() - start) * 1000
tasks["document"] = document_search()
# Execute all searches in parallel
results_dict = await asyncio.gather(*tasks.values())
@@ -440,7 +499,7 @@ JSON:"""
logger.info(
f"Parallel retrieval: vector={len(output.get('vector', []))}, "
f"graph={len(output.get('graph', []))}, web={len(output.get('web', []))}, "
f"volatile={len(output.get('volatile', []))}"
f"volatile={len(output.get('volatile', []))}, document={len(output.get('document', []))}"
)
return output
@@ -531,10 +590,11 @@ JSON:"""
wiki_results: List[Dict],
web_results: List[Dict],
volatile_results: Optional[List[Dict]] = None,
document_results: Optional[List[Dict]] = None,
k: int = 60
) -> List[Dict[str, Any]]:
"""
Stage 2: Final RRF between wiki, volatile, and web.
Stage 2: Final RRF between wiki, volatile, document, and web.
Wiki results are pre-merged from vector+graph. Volatile results
get a priority boost (smaller effective k) since they represent
@@ -544,6 +604,7 @@ JSON:"""
wiki_results: Pre-merged wiki results from _merge_wiki_sources()
web_results: Results from web search
volatile_results: Results from volatile cache (fresh data)
document_results: Results from Paperless document search
k: RRF constant (default 60)
Returns:
@@ -551,6 +612,7 @@ JSON:"""
"""
rrf_scores = {}
volatile_results = volatile_results or []
document_results = document_results or []
# Volatile results get priority boost (k/2 = stronger score per rank)
volatile_k = k // 2
@@ -567,6 +629,19 @@ JSON:"""
"source_type": "volatile"
}
# Document results (Paperless)
for rank, result in enumerate(document_results, start=1):
paperless_id = result.get("paperless_id")
if not paperless_id:
continue
result_id = f"doc_{paperless_id}"
rrf_scores[result_id] = {
"result": result,
"rrf_score": 1 / (k + rank),
"sources": ["document"],
"source_type": "document"
}
# Wiki results (single source, already merged)
for rank, result in enumerate(wiki_results, start=1):
page_id = result.get("page_id")
@@ -601,7 +676,8 @@ JSON:"""
)
volatile_count = len([r for r in sorted_results if r["source_type"] == "volatile"])
logger.info(f"Final RRF: {len(sorted_results)} results (wiki + volatile[{volatile_count}] + web)")
document_count = len([r for r in sorted_results if r["source_type"] == "document"])
logger.info(f"Final RRF: {len(sorted_results)} results (wiki + volatile[{volatile_count}] + document[{document_count}] + web)")
return sorted_results
@@ -893,23 +969,35 @@ Ranking:"""
for result_data in results:
result = result_data.get("result", {})
related_dossiers = result_data.get("related_dossiers", [])
source_type = result_data.get("source_type", "unknown")
# Build metadata based on source type
metadata = {
"entity_matches": result.get("entity_matches"),
"matched_entities": result.get("matched_entities"),
"engine": result.get("engine")
}
# Add document-specific metadata
if source_type == "document":
metadata["correspondent"] = result.get("correspondent")
metadata["document_type"] = result.get("document_type")
metadata["tags"] = result.get("tags", [])
metadata["original_filename"] = result.get("original_filename")
models.append(HybridRAGResult(
source_type=result_data.get("source_type", "unknown"),
source_type=source_type,
title=result.get("title", "Untitled"),
content=result.get("content", ""),
url=result.get("url"),
page_id=result.get("page_id"),
page_path=result.get("path"),
paperless_id=result.get("paperless_id"),
rrf_score=result_data.get("rrf_score", 0),
final_rank=result_data.get("final_rank", 0),
sources=result_data.get("sources", []),
related_dossiers=[RelatedDossier(**d) for d in related_dossiers],
metadata={
"entity_matches": result.get("entity_matches"),
"matched_entities": result.get("matched_entities"),
"engine": result.get("engine")
}
metadata=metadata
))
return models
+736
View File
@@ -0,0 +1,736 @@
"""
Volatile Fetch service for Library Desk.
Orchestrates fetching data from external APIs and storing in volatile cache.
Called by scheduler for prefetch or by HybridRAG for reactive caching.
"""
import asyncio
import logging
from typing import Optional
from dataclasses import dataclass, field
from src.apis import (
OpenMeteoProvider,
AggregatedNewsProvider,
AlphaVantageProvider,
CurrentWeather,
WeatherForecast,
SunTimes,
AirQuality,
NewsFeed,
StockQuote,
)
from src.services.volatile_service import VolatileCacheService
from src.models.volatile import VolatileRecordResponse, VolatileNamespace
logger = logging.getLogger(__name__)
@dataclass
class FetchResult:
"""Result of a volatile fetch operation."""
success: bool
namespace: str
key: str
record: Optional[VolatileRecordResponse] = None
error: Optional[str] = None
@dataclass
class EnvironmentFetchResult:
"""Result of combined environment fetch (weather + air quality)."""
success: bool
key: str
weather: Optional[FetchResult] = None
air_quality: Optional[FetchResult] = None
errors: list[str] = field(default_factory=list)
class VolatileFetchService:
"""
Service to fetch external data and store in volatile cache.
Supports:
- Weather: Current conditions and forecast via Open-Meteo
- News: Headlines from configured sources (NOS, BBC)
- Financial: Stock/crypto quotes via Alpha Vantage
"""
def __init__(
self,
volatile_service: VolatileCacheService,
weather_provider: OpenMeteoProvider,
news_provider: Optional[AggregatedNewsProvider] = None,
financial_provider: Optional[AlphaVantageProvider] = None,
):
"""
Initialize volatile fetch service.
Args:
volatile_service: Service for volatile cache storage
weather_provider: Open-Meteo weather provider
news_provider: Aggregated news provider (optional)
financial_provider: Alpha Vantage provider (optional)
"""
self.volatile = volatile_service
self.weather = weather_provider
self.news = news_provider
self.financial = financial_provider
async def fetch_current_weather(
self,
user: str,
city: str,
ttl: int = 3600, # 1 hour
) -> FetchResult:
"""
Fetch current weather conditions for a city and store in volatile cache.
Args:
user: User identifier
city: City name (will be geocoded)
ttl: Time-to-live in seconds (default 1 hour)
Returns:
FetchResult with success status and stored record
"""
try:
# Geocode city and get current conditions
location = await self.weather.geocode(city)
if not location:
return FetchResult(
success=False,
namespace="weather",
key=city.lower(),
error=f"Could not geocode city: {city}"
)
current = await self.weather.get_current(location)
# Generate natural language summary
text = current.to_text()
# Convert to storage format
data = {
"temperature": current.temperature,
"feels_like": current.feels_like,
"humidity": current.humidity,
"wind_speed": current.wind_speed,
"wind_direction": current.wind_direction,
"conditions": current.condition_text,
"condition_code": current.condition.value,
"uv_index": current.uv_index,
"location": current.location,
"text": text,
}
# Store in volatile cache
record = await self.volatile.store(
user=user,
namespace=VolatileNamespace.WEATHER,
key=city.lower(),
data=data,
source="openmeteo",
ttl=ttl,
)
logger.info(f"Stored current weather for {city} (user={user})")
return FetchResult(
success=True,
namespace="weather",
key=city.lower(),
record=record
)
except Exception as e:
logger.error(f"Failed to fetch current weather for {city}: {e}")
return FetchResult(
success=False,
namespace="weather",
key=city.lower(),
error=str(e)
)
async def fetch_forecast(
self,
user: str,
city: str,
days: int = 7,
ttl: int = 43200, # 12 hours
) -> FetchResult:
"""
Fetch weather forecast for a city and store in volatile cache.
Args:
user: User identifier
city: City name (will be geocoded)
days: Number of forecast days (1-16)
ttl: Time-to-live in seconds (default 12 hours)
Returns:
FetchResult with success status and stored record
"""
try:
# Geocode city and get forecast
location = await self.weather.geocode(city)
if not location:
return FetchResult(
success=False,
namespace="forecast",
key=city.lower(),
error=f"Could not geocode city: {city}"
)
forecast = await self.weather.get_forecast(location, days=days)
# Build daily forecast array
daily_forecasts = []
for day in forecast.daily:
daily_forecasts.append({
"date": day.date.isoformat(),
"day_name": day.date.strftime("%A"),
"temp_high": day.temp_high,
"temp_low": day.temp_low,
"conditions": day.condition_text,
"condition_code": day.condition.value,
"precipitation_chance": day.precipitation_chance,
"precipitation_mm": day.precipitation_mm,
"uv_index_max": day.uv_index_max,
})
# Generate natural language summary
forecast_lines = [f"{city} {days}-day forecast:"]
for day in forecast.daily:
forecast_lines.append(day.to_text())
text = "\n".join(forecast_lines)
# Convert to storage format
data = {
"days": days,
"daily": daily_forecasts,
"location": forecast.current.location,
"text": text,
}
# Store in volatile cache
record = await self.volatile.store(
user=user,
namespace=VolatileNamespace.FORECAST,
key=city.lower(),
data=data,
source="openmeteo",
ttl=ttl,
)
logger.info(f"Stored {days}-day forecast for {city} (user={user})")
return FetchResult(
success=True,
namespace="forecast",
key=city.lower(),
record=record
)
except Exception as e:
logger.error(f"Failed to fetch forecast for {city}: {e}")
return FetchResult(
success=False,
namespace="forecast",
key=city.lower(),
error=str(e)
)
async def fetch_news(
self,
user: str,
category: str = "general",
limit: int = 10,
ttl: int = 7200, # 2 hours
) -> FetchResult:
"""
Fetch news headlines and store in volatile cache.
Args:
user: User identifier
category: News category (general, tech, world, etc.)
limit: Maximum headlines to fetch
ttl: Time-to-live in seconds
Returns:
FetchResult with success status and stored record
"""
if not self.news:
return FetchResult(
success=False,
namespace="news",
key=category,
error="News provider not configured"
)
try:
feed = await self.news.get_feed(category, limit=limit)
# Convert to storage format
headlines = []
for item in feed.items:
headlines.append({
"title": item.title,
"description": item.description,
"url": item.url,
"source": item.source,
"published": item.published.isoformat() if item.published else None,
})
data = {
"category": category,
"headlines": headlines,
"count": len(headlines),
"sources": list(set(h["source"] for h in headlines)),
"text": feed.to_text(),
}
# Store in volatile cache
record = await self.volatile.store(
user=user,
namespace=VolatileNamespace.NEWS,
key=category,
data=data,
source="aggregated",
ttl=ttl,
)
logger.info(f"Stored {len(headlines)} headlines for {category} (user={user})")
return FetchResult(
success=True,
namespace="news",
key=category,
record=record
)
except Exception as e:
logger.error(f"Failed to fetch news for {category}: {e}")
return FetchResult(
success=False,
namespace="news",
key=category,
error=str(e)
)
async def fetch_stock(
self,
user: str,
symbol: str,
ttl: int = 300, # 5 minutes
) -> FetchResult:
"""
Fetch stock quote and store in volatile cache.
Args:
user: User identifier
symbol: Stock ticker symbol (e.g., "AAPL")
ttl: Time-to-live in seconds
Returns:
FetchResult with success status and stored record
"""
if not self.financial:
return FetchResult(
success=False,
namespace="financial",
key=symbol.lower(),
error="Financial provider not configured"
)
try:
quote = await self.financial.get_quote(symbol)
if not quote:
return FetchResult(
success=False,
namespace="financial",
key=symbol.lower(),
error=f"No quote found for symbol: {symbol}"
)
# Convert to storage format
data = {
"symbol": quote.symbol,
"name": quote.name,
"price": quote.price,
"currency": quote.currency,
"change": quote.change,
"change_percent": quote.change_percent,
"text": quote.to_text(),
}
# Store in volatile cache
record = await self.volatile.store(
user=user,
namespace=VolatileNamespace.FINANCIAL,
key=symbol.lower(),
data=data,
source="alphavantage",
ttl=ttl,
)
logger.info(f"Stored quote for {symbol} (user={user})")
return FetchResult(
success=True,
namespace="financial",
key=symbol.lower(),
record=record
)
except Exception as e:
logger.error(f"Failed to fetch quote for {symbol}: {e}")
return FetchResult(
success=False,
namespace="financial",
key=symbol.lower(),
error=str(e)
)
async def fetch_crypto(
self,
user: str,
symbol: str,
market: str = "USD",
ttl: int = 300, # 5 minutes
) -> FetchResult:
"""
Fetch cryptocurrency quote and store in volatile cache.
Args:
user: User identifier
symbol: Crypto symbol (e.g., "BTC", "ETH")
market: Market currency (default: USD)
ttl: Time-to-live in seconds
Returns:
FetchResult with success status and stored record
"""
if not self.financial:
return FetchResult(
success=False,
namespace="financial",
key=f"{symbol.lower()}_{market.lower()}",
error="Financial provider not configured"
)
try:
quote = await self.financial.get_crypto_quote(symbol, market)
if not quote:
return FetchResult(
success=False,
namespace="financial",
key=f"{symbol.lower()}_{market.lower()}",
error=f"No quote found for crypto: {symbol}/{market}"
)
key = f"{symbol.lower()}_{market.lower()}"
# Convert to storage format
data = {
"symbol": quote.symbol,
"name": quote.name,
"price": quote.price,
"currency": quote.currency,
"text": quote.to_text(),
}
# Store in volatile cache
record = await self.volatile.store(
user=user,
namespace=VolatileNamespace.FINANCIAL,
key=key,
data=data,
source="alphavantage",
ttl=ttl,
)
logger.info(f"Stored crypto quote for {symbol}/{market} (user={user})")
return FetchResult(
success=True,
namespace="financial",
key=key,
record=record
)
except Exception as e:
logger.error(f"Failed to fetch crypto quote for {symbol}: {e}")
return FetchResult(
success=False,
namespace="financial",
key=f"{symbol.lower()}_{market.lower()}",
error=str(e)
)
async def fetch_sun_times(
self,
user: str,
city: str,
ttl: int = 86400, # 24 hours
) -> FetchResult:
"""
Fetch sunrise/sunset times for a city and store in volatile cache.
Args:
user: User identifier
city: City name (will be geocoded)
ttl: Time-to-live in seconds
Returns:
FetchResult with success status and stored record
"""
try:
# Geocode city and get sun times
location = await self.weather.geocode(city)
if not location:
return FetchResult(
success=False,
namespace="sun",
key=city.lower(),
error=f"Could not geocode city: {city}"
)
sun_times = await self.weather.get_sun_times(location)
# Convert to storage format
data = {
"location": sun_times.location,
"date": sun_times.date.isoformat(),
"sunrise": sun_times.sunrise.strftime("%H:%M"),
"sunset": sun_times.sunset.strftime("%H:%M"),
"sunrise_iso": sun_times.sunrise.isoformat(),
"sunset_iso": sun_times.sunset.isoformat(),
"daylight_duration_seconds": sun_times.daylight_duration,
"daylight_hours": sun_times.daylight_duration / 3600,
"text": sun_times.to_text(),
}
# Store in volatile cache
record = await self.volatile.store(
user=user,
namespace=VolatileNamespace.SUN,
key=city.lower(),
data=data,
source="openmeteo",
ttl=ttl,
)
logger.info(f"Stored sun times for {city} (user={user})")
return FetchResult(
success=True,
namespace="sun",
key=city.lower(),
record=record
)
except Exception as e:
logger.error(f"Failed to fetch sun times for {city}: {e}")
return FetchResult(
success=False,
namespace="sun",
key=city.lower(),
error=str(e)
)
async def fetch_air_quality(
self,
user: str,
city: str,
ttl: int = 3600, # 1 hour
) -> FetchResult:
"""
Fetch air quality data for a city and store in volatile cache.
Args:
user: User identifier
city: City name (will be geocoded)
ttl: Time-to-live in seconds
Returns:
FetchResult with success status and stored record
"""
try:
# Geocode city and get air quality
location = await self.weather.geocode(city)
if not location:
return FetchResult(
success=False,
namespace="air_quality",
key=city.lower(),
error=f"Could not geocode city: {city}"
)
air_quality = await self.weather.get_air_quality(location)
# Convert to storage format
data = {
"location": air_quality.location,
"aqi_european": air_quality.aqi_european,
"aqi_us": air_quality.aqi_us,
"pm2_5": air_quality.pm2_5,
"pm10": air_quality.pm10,
"ozone": air_quality.ozone,
"nitrogen_dioxide": air_quality.nitrogen_dioxide,
"sulphur_dioxide": air_quality.sulphur_dioxide,
"carbon_monoxide": air_quality.carbon_monoxide,
"pollen_grass": air_quality.pollen_grass,
"pollen_birch": air_quality.pollen_birch,
"pollen_alder": air_quality.pollen_alder,
"text": air_quality.to_text(),
}
# Store in volatile cache
record = await self.volatile.store(
user=user,
namespace=VolatileNamespace.AIR_QUALITY,
key=city.lower(),
data=data,
source="openmeteo",
ttl=ttl,
)
logger.info(f"Stored air quality for {city} (user={user})")
return FetchResult(
success=True,
namespace="air_quality",
key=city.lower(),
record=record
)
except Exception as e:
logger.error(f"Failed to fetch air quality for {city}: {e}")
return FetchResult(
success=False,
namespace="air_quality",
key=city.lower(),
error=str(e)
)
async def fetch_environment(
self,
user: str,
city: str,
weather_ttl: int = 3600,
air_quality_ttl: int = 3600,
) -> EnvironmentFetchResult:
"""
Fetch weather and air quality concurrently for a city.
Performs a single geocode lookup and fetches both weather and air quality
data in parallel, storing both in volatile cache.
Args:
user: User identifier
city: City name (will be geocoded once)
weather_ttl: TTL for weather data (default 1 hour)
air_quality_ttl: TTL for air quality data (default 1 hour)
Returns:
EnvironmentFetchResult with both weather and air quality results
"""
errors: list[str] = []
key = city.lower()
# Single geocode lookup (shared by both fetches)
try:
location = await self.weather.geocode(city)
if not location:
return EnvironmentFetchResult(
success=False,
key=key,
errors=[f"Could not geocode city: {city}"]
)
except Exception as e:
return EnvironmentFetchResult(
success=False,
key=key,
errors=[f"Geocoding failed: {e}"]
)
# Fetch weather and air quality concurrently
async def fetch_weather_data() -> FetchResult:
try:
current = await self.weather.get_current(location)
text = current.to_text()
data = {
"temperature": current.temperature,
"feels_like": current.feels_like,
"humidity": current.humidity,
"wind_speed": current.wind_speed,
"wind_direction": current.wind_direction,
"conditions": current.condition_text,
"condition_code": current.condition.value,
"uv_index": current.uv_index,
"location": current.location,
"text": text,
}
record = await self.volatile.store(
user=user,
namespace=VolatileNamespace.WEATHER,
key=key,
data=data,
source="openmeteo",
ttl=weather_ttl,
)
return FetchResult(success=True, namespace="weather", key=key, record=record)
except Exception as e:
return FetchResult(success=False, namespace="weather", key=key, error=str(e))
async def fetch_air_quality_data() -> FetchResult:
try:
air_quality = await self.weather.get_air_quality(location)
data = {
"location": air_quality.location,
"aqi_european": air_quality.aqi_european,
"aqi_us": air_quality.aqi_us,
"pm2_5": air_quality.pm2_5,
"pm10": air_quality.pm10,
"ozone": air_quality.ozone,
"nitrogen_dioxide": air_quality.nitrogen_dioxide,
"sulphur_dioxide": air_quality.sulphur_dioxide,
"carbon_monoxide": air_quality.carbon_monoxide,
"pollen_grass": air_quality.pollen_grass,
"pollen_birch": air_quality.pollen_birch,
"pollen_alder": air_quality.pollen_alder,
"text": air_quality.to_text(),
}
record = await self.volatile.store(
user=user,
namespace=VolatileNamespace.AIR_QUALITY,
key=key,
data=data,
source="openmeteo",
ttl=air_quality_ttl,
)
return FetchResult(success=True, namespace="air_quality", key=key, record=record)
except Exception as e:
return FetchResult(success=False, namespace="air_quality", key=key, error=str(e))
# Run both fetches concurrently
weather_result, air_quality_result = await asyncio.gather(
fetch_weather_data(),
fetch_air_quality_data(),
)
# Collect any errors
if not weather_result.success:
errors.append(f"Weather: {weather_result.error}")
if not air_quality_result.success:
errors.append(f"Air quality: {air_quality_result.error}")
success = weather_result.success or air_quality_result.success
logger.info(
f"Environment fetch for {city} (user={user}): "
f"weather={'ok' if weather_result.success else 'failed'}, "
f"air_quality={'ok' if air_quality_result.success else 'failed'}"
)
return EnvironmentFetchResult(
success=success,
key=key,
weather=weather_result,
air_quality=air_quality_result,
errors=errors,
)
-18
View File
@@ -116,24 +116,6 @@ class WikiChangeListener:
except Exception as e:
logger.error(f"Failed to handle notification: {e}", exc_info=True)
def _is_automated_user(self, email: str) -> bool:
"""
Check if email belongs to an automated system user.
These are edits made by library-desk via Wiki.js API (entity linking).
We skip processing these to prevent loops.
Customize this list based on your Wiki.js username for library-desk.
"""
automated_users = [
self.settings.wikijs_username, # Library-desk's Wiki.js API user
"library-desk@system",
"automation@system",
"bot@system"
]
return email.lower() in [u.lower() for u in automated_users]
def _is_recently_processed(self, page_id: int) -> bool:
"""Check if page was processed recently (debouncing)."""
if page_id not in self._recent_notifications:
+1 -2
View File
@@ -45,8 +45,7 @@ def wikijs_test_config() -> dict:
settings = get_settings()
return {
"base_url": f"http://{TEST_HOST}:3000",
"username": settings.wikijs_username,
"password": settings.wikijs_password
"api_token": settings.wiki_graphql_api
}
+59 -26
View File
@@ -158,9 +158,43 @@ def sample_web_results():
]
@pytest.fixture
def sample_unified_classification():
"""Sample unified classification response for memory routing."""
return [
{
"url": "https://kubernetes.io/docs",
"title": "Kubernetes Container Orchestration",
"route_type": "wiki",
"wiki_action": "create",
"wiki_path": "infrastructure/kubernetes",
"wiki_summary": "Overview of Kubernetes orchestration capabilities",
"confidence": 0.9,
"reason": "Stable reference documentation"
},
{
"url": "https://docs.docker.com/swarm",
"title": "Docker Swarm Documentation",
"route_type": "wiki",
"wiki_action": "update",
"wiki_path": "infrastructure/docker",
"wiki_summary": "Docker Swarm container orchestration tool",
"confidence": 0.85,
"reason": "Technical documentation"
},
{
"url": "https://example.com/k8s-tutorial",
"title": "Kubernetes Tutorial",
"route_type": "skip",
"confidence": 0.7,
"reason": "Redundant with main docs"
}
]
@pytest.fixture
def sample_llm_analysis():
"""Sample LLM analysis response."""
"""Sample LLM analysis response (legacy format for _analyze_web_results tests)."""
return {
"has_novel_info": True,
"new_pages": [
@@ -534,12 +568,13 @@ async def test_process_search_dry_run(
mock_ollama,
sample_unprocessed_searches,
sample_web_results,
sample_llm_analysis
sample_unified_classification
):
"""Test processing search in dry run mode."""
# Mock responses
mock_neo4j.execute_query.return_value = sample_web_results
mock_ollama.generate_text.return_value = json.dumps(sample_llm_analysis)
# Return unified classification format (JSON array)
mock_ollama.generate_text.return_value = json.dumps(sample_unified_classification)
result = await consolidation_service._process_search(
search=sample_unprocessed_searches[0],
@@ -549,9 +584,8 @@ async def test_process_search_dry_run(
assert result is not None
assert result.search_id == 'search-1'
assert result.pages_created == 1
assert result.pages_updated == 1
assert result.entities_added == 2
# Unified classification: 2 wiki (1 create, 1 update), 1 skip
assert result.pages_created == 2 # wiki_routed count in dry run
@pytest.mark.asyncio
@@ -582,42 +616,41 @@ async def test_consolidate_knowledge_success(
mock_wiki,
sample_unprocessed_searches,
sample_web_results,
sample_llm_analysis
sample_unified_classification
):
"""Test successful knowledge consolidation."""
# Mock finding searches and entity creation
# Each search processes: get web results, add 2 entities, mark processed
mock_neo4j.execute_query.side_effect = [
sample_unprocessed_searches, # Find searches
sample_web_results, # Get web results for search 1
None, # Add entity 1 (Kubernetes)
None, # Add entity 2 (Docker Swarm)
None, # Mark search 1 processed
sample_web_results, # Get web results for search 2
None, # Add entity 1 (Kubernetes)
None, # Add entity 2 (Docker Swarm)
None, # Mark search 2 processed
]
# Use a flexible mock that returns appropriate data based on call patterns
call_count = [0]
def flexible_neo4j_response(*args, **kwargs):
call_count[0] += 1
if call_count[0] == 1:
return sample_unprocessed_searches # Find searches
elif "WebResult" in str(args) or "FOUND" in str(args):
return sample_web_results # Get web results
else:
return [] # Mark processed, etc.
mock_neo4j.execute_query.side_effect = flexible_neo4j_response
# Mock wiki operations
mock_wiki.search_pages.return_value = [] # No existing pages
mock_wiki.create_page.return_value = None
mock_wiki.create_page.return_value = {"id": 1}
mock_wiki.update_page.return_value = None
mock_wiki.get_page.return_value = None
mock_wiki.get_page.return_value = {"content": "existing content"}
# Mock LLM analysis and WikiPageWriter LLM calls
mock_ollama.generate_text.return_value = json.dumps(sample_llm_analysis)
# Mock unified classification response
mock_ollama.generate_text.return_value = json.dumps(sample_unified_classification)
response = await consolidation_service.consolidate_knowledge(
process_limit=10,
lookback_days=7,
min_web_results=2,
dry_run=False
dry_run=True # Use dry run to avoid wiki page creation complexity
)
assert response.total_found == 2
assert response.processed_count == 2
assert response.dry_run is False
assert response.dry_run is True
@pytest.mark.asyncio
+6 -7
View File
@@ -55,8 +55,7 @@ async def wiki_client(wikijs_test_config) -> AsyncGenerator[WikiJSClient, None]:
"""Get Wiki.js client."""
client = WikiJSClient(
base_url=wikijs_test_config["base_url"],
username=wikijs_test_config["username"],
password=wikijs_test_config["password"]
api_token=wikijs_test_config["api_token"]
)
yield client
@@ -212,7 +211,7 @@ class TestAddEntityLinksToContent:
updated, count = add_entity_links_to_content(content, entities)
assert count == 1
assert "[Docker](/docker)" in updated
assert "[Docker](/users/test/docker)" in updated
def test_add_multiple_instances(self):
"""Test linking all instances of an entity."""
@@ -224,7 +223,7 @@ class TestAddEntityLinksToContent:
updated, count = add_entity_links_to_content(content, entities)
assert count == 2 # Both instances linked
assert updated.count("[Docker](/docker)") == 2
assert updated.count("[Docker](/users/test/docker)") == 2
def test_skip_entities_without_path(self):
"""Test that entities without wiki pages are not linked."""
@@ -237,7 +236,7 @@ class TestAddEntityLinksToContent:
updated, count = add_entity_links_to_content(content, entities)
assert count == 1 # Only Docker
assert "[Docker](/docker)" in updated
assert "[Docker](/users/test/docker)" in updated
assert "[Kubernetes]" not in updated
def test_protect_existing_links(self):
@@ -252,7 +251,7 @@ class TestAddEntityLinksToContent:
# Should link the second "Docker" but not the one already linked
assert count == 1
assert "[Docker](https://docker.com)" in updated # Preserved
assert updated.count("[Docker](/docker)") == 1
assert updated.count("[Docker](/users/test/docker)") == 1
def test_no_nested_links(self):
"""Test that entity names in URLs are not linked."""
@@ -278,7 +277,7 @@ class TestAddEntityLinksToContent:
updated, count = add_entity_links_to_content(content, entities)
# Should link "Machine Learning" first, leaving "Machine" alone
assert "[Machine Learning](/ml)" in updated
assert "[Machine Learning](/users/test/ml)" in updated
assert count >= 1
+1 -2
View File
@@ -66,8 +66,7 @@ async def wiki_client(wikijs_test_config) -> AsyncGenerator[WikiJSClient, None]:
"""Get Wiki.js client."""
client = WikiJSClient(
base_url=wikijs_test_config["base_url"],
username=wikijs_test_config["username"],
password=wikijs_test_config["password"]
api_token=wikijs_test_config["api_token"]
)
yield client
+1 -2
View File
@@ -53,8 +53,7 @@ async def wikijs_client(wikijs_test_config) -> AsyncGenerator[WikiJSClient, None
"""Get Wiki.js client."""
client = WikiJSClient(
base_url=wikijs_test_config["base_url"],
username=wikijs_test_config["username"],
password=wikijs_test_config["password"]
api_token=wikijs_test_config["api_token"]
)
yield client
await client.close()
+4 -4
View File
@@ -98,7 +98,7 @@ class TestVolatileNamespaces:
def test_weather_default_ttl(self):
"""Test weather namespace default TTL."""
assert NAMESPACE_DEFAULT_TTL[VolatileNamespace.WEATHER] == 1800 # 30 min
assert NAMESPACE_DEFAULT_TTL[VolatileNamespace.WEATHER] == 3600 # 1 hour (current conditions)
def test_financial_default_ttl(self):
"""Test financial namespace default TTL."""
@@ -110,7 +110,7 @@ class TestVolatileNamespaces:
def test_namespace_count(self):
"""Test we have the expected number of namespaces."""
assert len(VolatileNamespace) == 11
assert len(VolatileNamespace) == 13 # Including SUN, FORECAST
class TestVolatileListResponse:
@@ -274,7 +274,7 @@ class TestVolatileService:
def test_get_default_ttl_known_namespace(self, volatile_service):
"""Test default TTL for known namespace."""
ttl = volatile_service._get_default_ttl("weather")
assert ttl == 1800 # Weather namespace default
assert ttl == 3600 # Weather namespace default (1 hour)
def test_get_default_ttl_unknown_namespace(self, volatile_service):
"""Test default TTL for unknown namespace."""
@@ -366,7 +366,7 @@ class TestVolatileService:
ttl=None # Not specified
)
assert result.ttl == 1800 # Weather default
assert result.ttl == 3600 # Weather default (1 hour)
@pytest.mark.asyncio
async def test_search_empty_collection(self, volatile_service, mock_qdrant, mock_ollama):
+15 -21
View File
@@ -50,22 +50,6 @@ class TestWikiChangeListener:
assert listener._debounce_seconds == 5
assert len(listener._recent_notifications) == 0
@pytest.mark.asyncio
async def test_automated_user_filtering(self, listener):
"""Test that automated users are correctly identified."""
# Automated users should be filtered
assert listener._is_automated_user("librarian@schweitz.net") is True
assert listener._is_automated_user("library-desk@system") is True
assert listener._is_automated_user("automation@system") is True
assert listener._is_automated_user("bot@system") is True
# Case insensitive
assert listener._is_automated_user("LIBRARIAN@SCHWEITZ.NET") is True
# Regular users should not be filtered
assert listener._is_automated_user("user@example.com") is False
assert listener._is_automated_user("john@example.com") is False
@pytest.mark.asyncio
async def test_debouncing_prevents_duplicates(self, listener):
"""Test that debouncing prevents duplicate processing."""
@@ -163,19 +147,29 @@ class TestWikiChangeListener:
assert mock_process.call_args[1]['event'] == 'page.delete'
@pytest.mark.asyncio
async def test_automated_user_notification_filtered(self, listener):
"""Test that notifications from automated users are filtered out."""
async def test_any_user_notification_processed(self, listener):
"""Test that notifications are processed regardless of user email.
Note: The user_email in PostgreSQL notifications is the page CREATOR,
not the editor. We cannot filter by user email because:
- A page created by 'librarian' but edited by a human should be processed
- Filtering by creator would break legitimate page ingestion
Loop prevention is handled by debouncing instead.
"""
mock_connection = AsyncMock()
with patch.object(listener, '_process_page_change', new_callable=AsyncMock) as mock_process:
# Notification from automated user should be skipped
# Even system user notifications should be processed
# (debouncing handles loop prevention, not user filtering)
await listener._handle_notification(
mock_connection, 1234, 'wiki_page_changes',
'UPDATE:123:librarian@schweitz.net'
)
# Process should NOT be called
mock_process.assert_not_called()
# Process SHOULD be called (user filtering is not used)
mock_process.assert_called_once()
assert mock_process.call_args[1]['page_id'] == 123
assert mock_process.call_args[1]['event'] == 'page.update'
@pytest.mark.asyncio
async def test_duplicate_notification_filtered(self, listener):