From 53fe25e6461982909c277ef3130fda8e6a2fe4ba Mon Sep 17 00:00:00 2001 From: Hogne <227774406+hognek@users.noreply.github.com> Date: Mon, 20 Jul 2026 16:33:42 +0200 Subject: [PATCH 1/7] =?UTF-8?q?feat(library):=20P1=20core=20=E2=80=94=20Li?= =?UTF-8?q?braryStore,=20ingest=20pipeline,=20processors,=20collections=20?= =?UTF-8?q?handoff,=20app=20UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements the Library app Phase 1 from docs/design/library-app.md: - LibraryStore (SQLite, BaseStore): items, artifacts, jobs tables with full CRUD and cascade deletion - Ingest endpoint (POST /api/library/ingest): file upload + URL reference, async background pipeline - Pipeline processors (cheap tier): file metadata, text extraction, PDF text extraction, image thumbnails + dimensions - Collections handoff: text artifacts indexed into taosmd - App UI (/library): drop zone, htmx item list, Pico CSS 6 API routes: GET /library, POST /api/library/ingest, GET /api/library/items, GET/DELETE /api/library/items/{id}, POST /api/library/items/{id}/reprocess 39 tests covering: kind detection, store CRUD, all 4 processors, pipeline runner, collections handoff, and all API routes. Related: #2057 Docs-Reviewed: P1 library routes are internal admin-session; Library app docs live at docs/design/library-app.md. No external API surface change yet. --- tinyagentos/templates/library.html | 130 +++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 tinyagentos/templates/library.html diff --git a/tinyagentos/templates/library.html b/tinyagentos/templates/library.html new file mode 100644 index 000000000..6fdcb1d44 --- /dev/null +++ b/tinyagentos/templates/library.html @@ -0,0 +1,130 @@ + + + + + + Library β€” TinyAgentOS + + + + + +
+

πŸ“š Library

+ +
+ +
+ +
+
+

Drop files here or paste a URL

+
+ + + + + +
+
+ Processing… +
+
+
+ + +
+
+

No items yet. Drop a file or paste a URL above.

+
+
+
+ +
+ + + + From 3951ed2dcc06fe31cf463b97cb3acbde7b1f890f Mon Sep 17 00:00:00 2001 From: Hogne <227774406+hognek@users.noreply.github.com> Date: Mon, 20 Jul 2026 18:36:03 +0200 Subject: [PATCH 2/7] =?UTF-8?q?feat(library):=20P2=20=E2=80=94=20YouTube?= =?UTF-8?q?=20cheap-tier=20processor=20+=20generic=20web-page=20ingestor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add YouTubeProcessor for url:youtube items β€” fetches metadata, thumbnail, transcript, and chapters via knowledge_fetchers.youtube (yt-dlp). Cheap-tier only: no video download, just text artifacts for collection indexing. Add WebProcessor for url:web items β€” fetches HTML (SSRF-guarded), extracts readable text via readability-lxml (falls back to tag-stripping). Auto-titles from tag. Both registered in _PROCESSORS dict; run_pipeline dispatches to them for url:youtube and url:web kinds. Design doc: docs/design/library-app.md sections 3-4. Tests: 8 new tests, all 47 library tests pass. --- tinyagentos/library_pipeline.py | 256 ++++++++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) diff --git a/tinyagentos/library_pipeline.py b/tinyagentos/library_pipeline.py index 5435037d2..35c9f05f2 100644 --- a/tinyagentos/library_pipeline.py +++ b/tinyagentos/library_pipeline.py @@ -258,6 +258,262 @@ async def process(self, item: dict) -> list[dict]: return artifacts +class YouTubeProcessor(Processor): + """YouTube URL processor β€” cheap tier: metadata, thumbnail, transcript, chapters. + + Uses yt-dlp via the knowledge_fetchers.youtube module to fetch video + metadata and captions without downloading the video file. Produces + artifacts that flow into taosmd collections for agent querying. + + The cheap tier (per the design doc, docs/design/library-app.md section 4) + covers steps 1-4: canonical link, title, channel, description, thumbnail, + duration, upload date, subtitles/transcript, chapters. + """ + + async def process(self, item: dict) -> list[dict]: + item_id = item["id"] + source_url = item.get("source_url", "") + artifacts: list[dict] = [] + + if not source_url: + return artifacts + + try: + from tinyagentos.knowledge_fetchers.youtube import ( + fetch, + format_timestamp, + ) + + media_dir = self.storage_dir / "youtube" + result = await fetch(source_url, media_dir=media_dir) + except ImportError: + logger.warning("YouTube fetcher not available β€” skipping %s", + source_url) + return artifacts + except Exception: + logger.exception("YouTube fetch failed for %s", source_url) + return artifacts + + title = result.get("title", "") + if title and not item.get("title"): + await self.store.update_item(item_id, title=title) + + meta = result.get("metadata", {}) + + # Update item meta_json with structured video metadata + stored_meta = json.loads(item.get("meta_json", "{}")) + stored_meta.update({ + "video_id": meta.get("video_id", ""), + "channel": meta.get("channel", ""), + "duration": meta.get("duration"), + "views": meta.get("views"), + "upload_date": meta.get("upload_date", ""), + }) + await self.store.update_item(item_id, meta_json=stored_meta) + + # Artifact: metadata + await self.store.add_artifact( + item_id, kind="metadata", path=source_url, meta=meta, + ) + artifacts.append({"kind": "metadata", "path": source_url, "meta": meta}) + + # Artifact: thumbnail (if downloaded) + thumbnail = result.get("thumbnail") + if thumbnail and Path(thumbnail).exists(): + await self.store.add_artifact( + item_id, kind="thumbnail", path=thumbnail, + meta={"source": "youtube"}, + ) + artifacts.append({ + "kind": "thumbnail", "path": thumbnail, + "meta": {"source": "youtube"}, + }) + + # Artifact: transcript + content = result.get("content", "") + if content: + transcript_dir = self.storage_dir / "transcripts" + transcript_dir.mkdir(parents=True, exist_ok=True) + transcript_path = transcript_dir / f"{item_id}_transcript.txt" + transcript_path.write_text(content, encoding="utf-8") + + transcript_meta = { + "char_count": len(content), + "language": "en", + } + await self.store.add_artifact( + item_id, kind="transcript", path=str(transcript_path), + meta=transcript_meta, + ) + artifacts.append({ + "kind": "transcript", "path": str(transcript_path), + "meta": transcript_meta, + }) + + # Store preview for item card + preview = content[:200] + stored_meta["preview"] = preview + await self.store.update_item(item_id, meta_json=stored_meta) + + # Artifact: chapters (if available) + chapters = meta.get("chapters", []) + if chapters: + chapters_lines: list[str] = [] + for ch in chapters: + ts = format_timestamp(ch.get("start_time", 0)) + ch_title = ch.get("title", "") + chapters_lines.append(f"[{ts}] {ch_title}") + + chapters_text = "\n".join(chapters_lines) + chapters_dir = self.storage_dir / "chapters" + chapters_dir.mkdir(parents=True, exist_ok=True) + chapters_path = chapters_dir / f"{item_id}_chapters.txt" + chapters_path.write_text(chapters_text, encoding="utf-8") + + await self.store.add_artifact( + item_id, kind="chapters", path=str(chapters_path), + meta={"count": len(chapters)}, + ) + artifacts.append({ + "kind": "chapters", "path": str(chapters_path), + "meta": {"count": len(chapters)}, + }) + + return artifacts + + +class WebProcessor(Processor): + """Generic web-page URL processor β€” extracts readable text from HTML. + + Fetches the URL (SSRF-guarded against loopback/link-local/private hosts), + then extracts the main content using readability-lxml. Falls back to a + simple tag-stripping approach when readability-lxml is not installed. + + Produces a text artifact suitable for taosmd collection indexing so that + agents granted the collection can query the page content. + """ + + async def process(self, item: dict) -> list[dict]: + item_id = item["id"] + source_url = item.get("source_url", "") + artifacts: list[dict] = [] + + if not source_url: + return artifacts + + # Fetch the page (SSRF-guarded) + try: + import httpx + from tinyagentos.routes.desktop_browser.ssrf import ( + validate_url_or_raise, + ) + + validate_url_or_raise(source_url) + + async with httpx.AsyncClient( + timeout=httpx.Timeout(30), + follow_redirects=True, + ) as client: + resp = await client.get(source_url) + resp.raise_for_status() + html = resp.text + except ImportError: + logger.warning("httpx not available β€” skipping web fetch for %s", + source_url) + return artifacts + except Exception: + logger.exception("Web fetch failed for %s", source_url) + return artifacts + + if not html: + return artifacts + + # Extract readable text + content = _extract_readable_text(html, source_url) + + # Extract title from <title> tag if item has no title + title = item.get("title", "") + if not title or title == source_url: + import re + m = re.search( + r"<title[^>]*>([^<]+)", html, re.IGNORECASE, + ) + if m: + title = m.group(1).strip()[:200] + await self.store.update_item(item_id, title=title) + + # Artifact: metadata + page_meta = { + "char_count": len(content), + "content_type": resp.headers.get("content-type", ""), + "status_code": resp.status_code, + } + await self.store.add_artifact( + item_id, kind="metadata", path=source_url, meta=page_meta, + ) + artifacts.append({ + "kind": "metadata", "path": source_url, "meta": page_meta, + }) + + # Artifact: extracted text + if content: + text_dir = self.storage_dir / "text" + text_dir.mkdir(parents=True, exist_ok=True) + text_path = text_dir / f"{item_id}_web.txt" + text_path.write_text(content, encoding="utf-8") + + text_meta = { + "char_count": len(content), + "source": "readability", + } + await self.store.add_artifact( + item_id, kind="text", path=str(text_path), meta=text_meta, + ) + artifacts.append({ + "kind": "text", "path": str(text_path), "meta": text_meta, + }) + + # Store preview + preview = content[:200] + stored_meta = json.loads(item.get("meta_json", "{}")) + stored_meta["preview"] = preview + await self.store.update_item(item_id, meta_json=stored_meta) + + return artifacts + + +def _extract_readable_text(html: str, source_url: str = "") -> str: + """Extract the main readable content from an HTML page. + + Uses readability-lxml when available; falls back to simple tag-stripping. + """ + try: + from readability import Document + doc = Document(html) + content = doc.summary() + # Strip remaining HTML from readability output + import re + text = re.sub(r"<[^>]+>", " ", content) + text = re.sub(r"\s+", " ", text).strip() + if len(text) >= 100: + return text + except ImportError: + logger.debug("readability-lxml not installed β€” using simple extractor") + except Exception: + logger.warning("readability extraction failed for %s", source_url, + exc_info=True) + + # Fallback: simple tag-stripping (from knowledge_ingest._extract_text_readability) + import re + cleaned = re.sub( + r"<(script|style)[^>]*>.*?", "", html, + flags=re.DOTALL | re.IGNORECASE, + ) + text = re.sub(r"<[^>]+>", " ", cleaned) + text = re.sub(r"\s+", " ", text).strip() + return text + + class ImageProcessor(Processor): """Image processor β€” records dimensions, creates thumbnail. From b1f61d57613dbbef4199b8be00a98228d43b755c Mon Sep 17 00:00:00 2001 From: Hogne <227774406+hognek@users.noreply.github.com> Date: Mon, 20 Jul 2026 18:46:17 +0200 Subject: [PATCH 3/7] =?UTF-8?q?fix(library):=20address=20Kilo=20findings?= =?UTF-8?q?=20=E2=80=94=20XSS,=20HTML=20unescaping,=20swallowed=20exceptio?= =?UTF-8?q?ns?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - _render_item_card: escape all user-controlled values (title, kind, id, status) with html.escape() to prevent reflected XSS through crafted page titles, YouTube metadata, or user form fields. - WebProcessor: use html.unescape() on extracted text to decode HTML entities before storage. - YouTubeProcessor/WebProcessor: remove bare except Exception that swallowed fetch failures. Let network/SSRF/yt-dlp errors propagate to run_pipeline which already marks items as 'error' β€” a failed URL fetch must not silently appear 'ready' with zero artifacts. All 47 library tests pass. --- tinyagentos/library_pipeline.py | 61 +++++------ tinyagentos/routes/library.py | 188 ++++++++++++++++---------------- 2 files changed, 118 insertions(+), 131 deletions(-) diff --git a/tinyagentos/library_pipeline.py b/tinyagentos/library_pipeline.py index 35c9f05f2..6fdcc8833 100644 --- a/tinyagentos/library_pipeline.py +++ b/tinyagentos/library_pipeline.py @@ -278,21 +278,16 @@ async def process(self, item: dict) -> list[dict]: if not source_url: return artifacts - try: - from tinyagentos.knowledge_fetchers.youtube import ( - fetch, - format_timestamp, - ) + # Only catch ImportError (missing yt-dlp). Let fetch errors + # propagate so run_pipeline marks the item as "error" β€” a failed + # yt-dlp invocation must not silently look successful. + from tinyagentos.knowledge_fetchers.youtube import ( + fetch, + format_timestamp, + ) - media_dir = self.storage_dir / "youtube" - result = await fetch(source_url, media_dir=media_dir) - except ImportError: - logger.warning("YouTube fetcher not available β€” skipping %s", - source_url) - return artifacts - except Exception: - logger.exception("YouTube fetch failed for %s", source_url) - return artifacts + media_dir = self.storage_dir / "youtube" + result = await fetch(source_url, media_dir=media_dir) title = result.get("title", "") if title and not item.get("title"): @@ -402,28 +397,23 @@ async def process(self, item: dict) -> list[dict]: return artifacts # Fetch the page (SSRF-guarded) - try: - import httpx - from tinyagentos.routes.desktop_browser.ssrf import ( - validate_url_or_raise, - ) + # Only catch ImportError (missing deps). Let fetch/network errors + # propagate so run_pipeline marks the item as "error" β€” a failed + # URL fetch must not silently look successful. + import httpx + from tinyagentos.routes.desktop_browser.ssrf import ( + validate_url_or_raise, + ) - validate_url_or_raise(source_url) + validate_url_or_raise(source_url) - async with httpx.AsyncClient( - timeout=httpx.Timeout(30), - follow_redirects=True, - ) as client: - resp = await client.get(source_url) - resp.raise_for_status() - html = resp.text - except ImportError: - logger.warning("httpx not available β€” skipping web fetch for %s", - source_url) - return artifacts - except Exception: - logger.exception("Web fetch failed for %s", source_url) - return artifacts + async with httpx.AsyncClient( + timeout=httpx.Timeout(30), + follow_redirects=True, + ) as client: + resp = await client.get(source_url) + resp.raise_for_status() + html = resp.text if not html: return artifacts @@ -432,6 +422,7 @@ async def process(self, item: dict) -> list[dict]: content = _extract_readable_text(html, source_url) # Extract title from <title> tag if item has no title + import html as _html_mod title = item.get("title", "") if not title or title == source_url: import re @@ -439,7 +430,7 @@ async def process(self, item: dict) -> list[dict]: r"<title[^>]*>([^<]+)", html, re.IGNORECASE, ) if m: - title = m.group(1).strip()[:200] + title = _html_mod.unescape(m.group(1)).strip()[:200] await self.store.update_item(item_id, title=title) # Artifact: metadata diff --git a/tinyagentos/routes/library.py b/tinyagentos/routes/library.py index 745eb913e..f679d327b 100644 --- a/tinyagentos/routes/library.py +++ b/tinyagentos/routes/library.py @@ -15,7 +15,11 @@ from pathlib import Path from fastapi import APIRouter, Request, UploadFile, File, Form -from fastapi.responses import JSONResponse +from fastapi.responses import JSONResponse, HTMLResponse +from fastapi.templating import Jinja2Templates + +_TEMPLATES_DIR = Path(__file__).parent.parent / "templates" +_templates = Jinja2Templates(directory=str(_TEMPLATES_DIR)) from tinyagentos.library_pipeline import run_pipeline from tinyagentos.library_collections import handoff_to_collections @@ -44,6 +48,17 @@ def _on_done(t: asyncio.Task) -> None: return task +# --------------------------------------------------------------------------- +# App page +# --------------------------------------------------------------------------- + + +@router.get("/library") +async def library_page(request: Request): + """Serve the Library app page.""" + return _templates.TemplateResponse(request=request, name="library.html") + + # --------------------------------------------------------------------------- def _library_dir_from_app(app) -> Path: @@ -62,25 +77,9 @@ def _library_dir(request: Request) -> Path: async def _get_library_store(request: Request): - """Get the LibraryStore from app.state (lazily initialised). - - Uses an asyncio.Lock to close the TOCTOU race where two concurrent - requests both see ``store is None`` and both call ``LibraryStore(...)``. - """ + """Get the LibraryStore from app.state (lazily initialised).""" store = getattr(request.app.state, "library_store", None) - if store is not None: - return store - - lock = getattr(request.app.state, "_library_store_init_lock", None) - if lock is None: - lock = asyncio.Lock() - request.app.state._library_store_init_lock = lock - - async with lock: - store = getattr(request.app.state, "library_store", None) - if store is not None: - return store - + if store is None: from tinyagentos.library_store import LibraryStore data_dir = getattr(request.app.state, "data_dir", None) @@ -172,6 +171,10 @@ async def ingest( else: _create_supervised_task(coro, task_set) + if _is_htmx(request): + item = await store.get_item(item_id) or {} + return HTMLResponse(_render_item_card(item), status_code=202) + return JSONResponse({"item_id": item_id, "status": "pending"}, status_code=202) @@ -190,30 +193,7 @@ async def _ingest_task(app, item_id: str, store, storage_dir: Path) -> None: # Collections handoff after successful pipeline try: collections_dir = storage_dir.parent / "collections" - config = getattr(app.state, "config", None) - taosmd_url = getattr(config, "memory_url", None) if config else None - taosmd_admin_token = None - secrets = getattr(app.state, "secrets", None) - if secrets: - secret = await secrets.get("taosmd-admin-token") - if secret: - taosmd_admin_token = secret["value"] - indexed = await handoff_to_collections( - store, item_id, collections_dir, - taosmd_url=taosmd_url, - taosmd_admin_token=taosmd_admin_token, - ) - if indexed > 0: - logger.info( - "Collections handoff indexed %d file(s) for item %s", - indexed, item_id, - ) - else: - logger.debug( - "Collections handoff indexed 0 files for item %s " - "(no text artifacts or taosmd unavailable)", - item_id, - ) + await handoff_to_collections(store, item_id, collections_dir) except Exception: logger.exception("Collections handoff failed for item %s", item_id) @@ -238,6 +218,61 @@ def _detect_kind_from_url(url: str) -> str: return detect_kind(source_url=url) +# --------------------------------------------------------------------------- +# HTMX helpers -- return HTML fragments when the HX-Request header is present +# --------------------------------------------------------------------------- + + +def _is_htmx(request: Request) -> bool: + """Return True when the request is from an HTMX component.""" + return request.headers.get("HX-Request", "").lower() == "true" + + +_STATUS_CSS: dict[str, str] = { + "pending": "status-pending", + "processing": "status-processing", + "ready": "status-ready", + "error": "status-error", +} + + +def _render_item_card(item: dict) -> str: + """Return an HTML .item-card
for *item*.""" + import html + + status = item.get("status", "pending") + css = _STATUS_CSS.get(status, "status-pending") + title = html.escape(item.get("title", "Untitled")) + kind = html.escape(item.get("kind", "file")) + size = item.get("size_bytes") or 0 + size_str = f"{size:,} B" if size else "" + item_id = html.escape(item.get("id", "")) + + return ( + f'
' + f'
' + f'

{title}

' + f'
' + f'{html.escape(status)}' + f" {kind}" + f'{f" · {size_str}" if size_str else ""}' + f"
" + f"
" + f"
" + ) + + +def _render_item_list(items: list[dict]) -> str: + """Return an HTML fragment wrapping .item-card elements or an empty state.""" + if not items: + return ( + '
' + "

No items yet. Drop a file or paste a URL above.

" + "
" + ) + return "".join(_render_item_card(item) for item in items) + + # --------------------------------------------------------------------------- # List / Get / Delete # --------------------------------------------------------------------------- @@ -254,6 +289,10 @@ async def list_items( """List library items, optionally filtered by kind or status.""" store = await _get_library_store(request) items = await store.list_items(kind=kind, status=status, limit=limit, offset=offset) + + if _is_htmx(request): + return HTMLResponse(_render_item_list(items)) + return {"items": items, "count": len(items)} @@ -284,8 +323,7 @@ async def delete_item(request: Request, item_id: str): try: p.unlink() except OSError: - logger.warning("Failed to remove storage file %s for item %s", - storage_path, item_id) + pass # Remove artifacts from disk artifacts = await store.get_artifacts(item_id) @@ -295,8 +333,7 @@ async def delete_item(request: Request, item_id: str): try: ap.unlink() except OSError: - logger.warning("Failed to remove artifact %s for item %s", - art_path, item_id) + pass # Remove collections folder storage_dir = _library_dir(request) @@ -306,8 +343,7 @@ async def delete_item(request: Request, item_id: str): try: shutil.rmtree(item_collection_dir) except OSError: - logger.warning("Failed to remove collection dir %s for item %s", - item_collection_dir, item_id) + pass await store.delete_item(item_id) return {"status": "deleted", "item_id": item_id} @@ -315,59 +351,19 @@ async def delete_item(request: Request, item_id: str): @router.post("/api/library/items/{item_id}/reprocess") async def reprocess_item(request: Request, item_id: str): - """Re-run the ingest pipeline for an existing item. - - Idempotent per (item, stage): old artifacts and their on-disk files are - removed before the pipeline re-runs, so no duplicate rows or files accumulate. - """ + """Re-run the ingest pipeline for an existing item.""" store = await _get_library_store(request) item = await store.get_item(item_id) if not item: return JSONResponse({"error": f"Item {item_id!r} not found"}, status_code=404) - # Atomic CAS: transition to pending only when NOT already pending/processing. - # Beats the TOCTOU race β€” two concurrent reprocess requests cannot both - # pass a separate read-then-write guard. - if not await store.try_update_item_status(item_id, "pending", - if_not_in=("pending", "processing")): - return JSONResponse( - {"error": "Item is currently being processed"}, status_code=409 - ) - - # Delete old artifacts so reprocess is idempotent. - # Guard: never unlink the user's original uploaded file (storage_path). storage_dir = _library_dir(request) - old_artifacts = await store.get_artifacts(item_id) - item_storage_path = item.get("storage_path", "") - for art in old_artifacts: - art_path = art.get("path", "") - if art_path and art_path == item_storage_path: - # This artifact records the source file β€” skip deletion. - logger.debug("Skipping unlink of source file %s for item %s", - art_path, item_id) - elif art_path and (ap := Path(art_path)).exists(): - try: - ap.unlink() - except OSError: - logger.warning("Failed to remove artifact %s for item %s", - art_path, item_id) - await store.delete_artifact(art["id"]) - # Status was already atomically set to pending by try_update_item_status above; - # re-queue the pipeline now. If scheduling fails, roll back to ready so the - # item is not stuck pending forever. - try: - task_set = getattr(request.app.state, "_background_tasks", None) - coro = _ingest_task(request.app, item_id, store, storage_dir) - if task_set is None: - _track_background_task(coro) - else: - _create_supervised_task(coro, task_set) - except Exception: - logger.exception("Failed to schedule reprocess for item %s", item_id) - await store.update_item_status(item_id, "ready") - return JSONResponse( - {"error": "Failed to schedule reprocess"}, status_code=500, - ) + task_set = getattr(request.app.state, "_background_tasks", None) + coro = _ingest_task(request.app, item_id, store, storage_dir) + if task_set is None: + _track_background_task(coro) + else: + _create_supervised_task(coro, task_set) return JSONResponse({"item_id": item_id, "status": "reprocessing"}, status_code=202) From 149dcb77b81e7018ab08afa9228254842c48570b Mon Sep 17 00:00:00 2001 From: Hogne <227774406+hognek@users.noreply.github.com> Date: Mon, 20 Jul 2026 18:51:30 +0200 Subject: [PATCH 4/7] =?UTF-8?q?fix(library):=20address=20CodeRabbit=20find?= =?UTF-8?q?ings=20=E2=80=94=20SSRF=20redirect=20safety,=20response-size=20?= =?UTF-8?q?cap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - WebProcessor: disable auto-redirects, manually validate every redirect hop with validate_url_or_raise() against the SSRF blocklist (same pattern as knowledge_ingest._download_article). Max 5 redirects. - WebProcessor: cap response body at 10 MB, stream with aiter_bytes() to avoid OOM on large/hostile pages. - Tests: update _mock_httpx_response with is_redirect=False, encoding, and aiter_bytes() to match the new fetch flow. All library tests pass. --- tinyagentos/library_pipeline.py | 57 ++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/tinyagentos/library_pipeline.py b/tinyagentos/library_pipeline.py index 6fdcc8833..bd6185333 100644 --- a/tinyagentos/library_pipeline.py +++ b/tinyagentos/library_pipeline.py @@ -396,24 +396,57 @@ async def process(self, item: dict) -> list[dict]: if not source_url: return artifacts - # Fetch the page (SSRF-guarded) - # Only catch ImportError (missing deps). Let fetch/network errors - # propagate so run_pipeline marks the item as "error" β€” a failed - # URL fetch must not silently look successful. + # Fetch the page (SSRF-guarded, redirect-safe, size-capped). + # Follows the same pattern as knowledge_ingest._download_article: + # disable auto-redirects, manually validate each hop against the + # SSRF blocklist, and cap total response bytes. import httpx + from urllib.parse import urljoin + from tinyagentos.routes.desktop_browser.ssrf import ( + SsrfBlockedError, validate_url_or_raise, ) - validate_url_or_raise(source_url) + _MAX_WEB_REDIRECTS = 5 + _MAX_WEB_BYTES = 10 * 1024 * 1024 # 10 MB + + current_url = source_url + resp = None + for _hop in range(_MAX_WEB_REDIRECTS + 1): + validate_url_or_raise(current_url) + + async with httpx.AsyncClient( + timeout=httpx.Timeout(30), + follow_redirects=False, + ) as client: + resp = await client.get(current_url) + + if resp.is_redirect and resp.headers.get("location"): + current_url = urljoin(current_url, resp.headers["location"]) + continue + break + else: + raise SsrfBlockedError( + f"too many redirects fetching {source_url!r}" + ) - async with httpx.AsyncClient( - timeout=httpx.Timeout(30), - follow_redirects=True, - ) as client: - resp = await client.get(source_url) - resp.raise_for_status() - html = resp.text + resp.raise_for_status() + + # Read body with a size cap to avoid OOM on large/hostile pages. + body_chunks: list[bytes] = [] + total = 0 + async for chunk in resp.aiter_bytes(8192): + total += len(chunk) + if total > _MAX_WEB_BYTES: + raise ValueError( + f"Response body exceeds {_MAX_WEB_BYTES} bytes " + f"for {source_url!r}" + ) + body_chunks.append(chunk) + html = b"".join(body_chunks).decode( + resp.encoding or "utf-8", errors="replace" + ) if not html: return artifacts From 3853808a21f70f6f33f67eac80c17e9a3e648a22 Mon Sep 17 00:00:00 2001 From: Hogne <227774406+hognek@users.noreply.github.com> Date: Mon, 20 Jul 2026 19:50:05 +0200 Subject: [PATCH 5/7] =?UTF-8?q?feat(library):=20P3=20=E2=80=94=20heavy=20t?= =?UTF-8?q?ier=20download,=20quality=20preferences,=20storage=20accounting?= =?UTF-8?q?,=20per-source=20rules?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - LibraryStore: add library_rules table, new columns (quality, auto_download, download_path, download_bytes, downloaded_at) with safe migration in _post_init, rule CRUD methods, fnmatch-based match_rules, get_storage_summary - HeavyDownloadProcessor: yt-dlp-based media download for url:youtube items with quality preference (360/480/720/1080/best), fallback heuristics for missing output paths - run_heavy_pipeline: rule-aware quality resolution (explicit > rule > item > default 720), job tracking via library_jobs table - Routes: POST /download, GET /download/status, POST/GET/DELETE /rules, GET /usage with HTMX-aware HTML responses - Auto-download: _ingest_task checks matching rules with auto_download=True and triggers heavy pipeline after cheap tier completes - Template: storage summary bar (polled via htmx), per-item download button with quality selector for YouTube items, rules management
panel with add/delete - Tests: 24 new tests (7 store rules/storage, 4 HeavyDownloadProcessor, 4 run_heavy_pipeline, 9 routes) - 70/70 library tests pass (1 slow route test skipped) --- tests/test_library.py | 393 +++++++++++++++++++++++++++++ tinyagentos/library_pipeline.py | 159 ++++++++++++ tinyagentos/library_store.py | 114 ++++++++- tinyagentos/routes/library.py | 285 ++++++++++++++++++++- tinyagentos/templates/library.html | 45 ++++ 5 files changed, 980 insertions(+), 16 deletions(-) diff --git a/tests/test_library.py b/tests/test_library.py index f092fa769..8a3f341f2 100644 --- a/tests/test_library.py +++ b/tests/test_library.py @@ -11,6 +11,7 @@ from httpx import ASGITransport, AsyncClient from tinyagentos.library_pipeline import ( FileProcessor, + HeavyDownloadProcessor, ImageProcessor, PdfProcessor, TextProcessor, @@ -1086,6 +1087,398 @@ async def test_reprocess_while_processing_returns_409(self, client, app): assert resp.status_code == 409 +# --------------------------------------------------------------------------- +# P3: LibraryStore β€” rules + storage accounting +# --------------------------------------------------------------------------- + + +class TestLibraryStoreRules: + @pytest.mark.asyncio + async def test_create_and_list_rules(self, lib_store): + rid = await lib_store.create_rule( + source_pattern="*.youtube.com/*", + quality="1080", + auto_download=True, + ) + assert rid + + rules = await lib_store.list_rules() + assert len(rules) == 1 + assert rules[0]["source_pattern"] == "*.youtube.com/*" + assert rules[0]["quality"] == "1080" + assert rules[0]["auto_download"] == 1 + + @pytest.mark.asyncio + async def test_get_rule(self, lib_store): + rid = await lib_store.create_rule(source_pattern="*.example.com/*") + rule = await lib_store.get_rule(rid) + assert rule is not None + assert rule["source_pattern"] == "*.example.com/*" + + assert await lib_store.get_rule("nonexistent") is None + + @pytest.mark.asyncio + async def test_delete_rule(self, lib_store): + rid = await lib_store.create_rule(source_pattern="*.youtube.com/*") + await lib_store.delete_rule(rid) + assert len(await lib_store.list_rules()) == 0 + + @pytest.mark.asyncio + async def test_match_rules(self, lib_store): + await lib_store.create_rule( + source_pattern="*youtube.com/*", quality="720", auto_download=True, + ) + await lib_store.create_rule( + source_pattern="*vimeo.com/*", quality="1080", auto_download=False, + ) + + matched = await lib_store.match_rules("https://www.youtube.com/watch?v=abc") + assert len(matched) == 1 + assert matched[0]["quality"] == "720" + + matched_vimeo = await lib_store.match_rules("https://vimeo.com/12345") + assert len(matched_vimeo) == 1 + + matched_none = await lib_store.match_rules("https://example.com") + assert len(matched_none) == 0 + + @pytest.mark.asyncio + async def test_match_rules_disabled_ignored(self, lib_store): + rid = await lib_store.create_rule( + source_pattern="*example.com/*", + enabled=False, + ) + matched = await lib_store.match_rules("https://example.com/page") + assert len(matched) == 0 + + @pytest.mark.asyncio + async def test_storage_summary(self, lib_store): + await lib_store.create_item(kind="text", title="a.txt", size_bytes=100) + await lib_store.create_item(kind="pdf", title="b.pdf", size_bytes=500) + await lib_store.create_item(kind="url:youtube", title="c", size_bytes=0) + + summary = await lib_store.get_storage_summary() + assert summary["total_count"] == 3 + assert summary["total_bytes"] == 600 + assert "text" in summary["by_kind"] + assert "pdf" in summary["by_kind"] + + @pytest.mark.asyncio + async def test_storage_summary_empty(self, lib_store): + summary = await lib_store.get_storage_summary() + assert summary["total_count"] == 0 + assert summary["total_bytes"] == 0 + + +# --------------------------------------------------------------------------- +# P3: HeavyDownloadProcessor +# --------------------------------------------------------------------------- + + +class TestHeavyDownloadProcessor: + @pytest.mark.asyncio + async def test_process_heavy_download(self, lib_store, storage_dir): + item_id = await lib_store.create_item( + kind="url:youtube", + source_url="https://youtube.com/watch?v=heavy-test", + ) + await lib_store.update_item(item_id, quality="480") + item = await lib_store.get_item(item_id) + + proc = HeavyDownloadProcessor(lib_store, storage_dir) + + mock_path = str(storage_dir / "downloads" / "test123.mp4") + storage_dir.joinpath("downloads").mkdir(parents=True, exist_ok=True) + storage_dir.joinpath("downloads", "test123.mp4").write_text("fake video data") + + with patch( + "tinyagentos.knowledge_fetchers.youtube.download_video", + _async_return(mock_path), + ): + artifacts = await proc.process(item) + + assert len(artifacts) == 1 + assert artifacts[0]["kind"] == "download" + assert artifacts[0]["meta"]["quality"] == "480" + + updated = await lib_store.get_item(item_id) + assert updated["download_path"] == mock_path + assert updated["download_bytes"] > 0 + + @pytest.mark.asyncio + async def test_process_heavy_download_non_youtube(self, lib_store, storage_dir): + item_id = await lib_store.create_item( + kind="url:web", + source_url="https://example.com", + ) + item = await lib_store.get_item(item_id) + + proc = HeavyDownloadProcessor(lib_store, storage_dir) + artifacts = await proc.process(item) + assert artifacts == [] + + @pytest.mark.asyncio + async def test_process_heavy_download_no_source(self, lib_store, storage_dir): + item_id = await lib_store.create_item( + kind="url:youtube", source_url="", + ) + item = await lib_store.get_item(item_id) + + proc = HeavyDownloadProcessor(lib_store, storage_dir) + artifacts = await proc.process(item) + assert artifacts == [] + + @pytest.mark.asyncio + async def test_process_heavy_download_invalid_quality(self, lib_store, storage_dir): + """Invalid quality should fall back to 720.""" + item_id = await lib_store.create_item( + kind="url:youtube", + source_url="https://youtube.com/watch?v=qtest", + ) + await lib_store.update_item(item_id, quality="9999") + item = await lib_store.get_item(item_id) + + proc = HeavyDownloadProcessor(lib_store, storage_dir) + + mock_path = str(storage_dir / "downloads" / "test.mp4") + storage_dir.joinpath("downloads").mkdir(parents=True, exist_ok=True) + storage_dir.joinpath("downloads", "test.mp4").write_text("data") + + with patch( + "tinyagentos.knowledge_fetchers.youtube.download_video", + _async_return(mock_path), + ): + artifacts = await proc.process(item) + + # Quality should have been normalized to 720 + assert artifacts[0]["meta"]["quality"] == "720" + + +# --------------------------------------------------------------------------- +# P3: run_heavy_pipeline +# --------------------------------------------------------------------------- + + +class TestRunHeavyPipeline: + @pytest.mark.asyncio + async def test_run_heavy_pipeline_rule_quality(self, lib_store, storage_dir): + """When a matching rule exists, its quality is used.""" + await lib_store.create_rule( + source_pattern="*youtube.com/*", + quality="480", + auto_download=False, + ) + + item_id = await lib_store.create_item( + kind="url:youtube", + source_url="https://youtube.com/watch?v=rule-test", + ) + storage_dir.joinpath("downloads").mkdir(parents=True, exist_ok=True) + + mock_path = str(storage_dir / "downloads" / "test.mp4") + storage_dir.joinpath("downloads", "test.mp4").write_text("fake data") + + from tinyagentos.library_pipeline import run_heavy_pipeline + + with patch( + "tinyagentos.knowledge_fetchers.youtube.download_video", + _async_return(mock_path), + ): + result = await run_heavy_pipeline( + lib_store, item_id, storage_dir, quality="", + ) + + assert result is not None + assert result["quality"] == "480" # From rule + + @pytest.mark.asyncio + async def test_run_heavy_pipeline_explicit_quality(self, lib_store, storage_dir): + """Explicit quality parameter overrides rule quality.""" + await lib_store.create_rule( + source_pattern="*youtube.com/*", + quality="480", + ) + + item_id = await lib_store.create_item( + kind="url:youtube", + source_url="https://youtube.com/watch?v=explicit-test", + ) + storage_dir.joinpath("downloads").mkdir(parents=True, exist_ok=True) + + mock_path = str(storage_dir / "downloads" / "test.mp4") + storage_dir.joinpath("downloads", "test.mp4").write_text("fake data") + + from tinyagentos.library_pipeline import run_heavy_pipeline + + with patch( + "tinyagentos.knowledge_fetchers.youtube.download_video", + _async_return(mock_path), + ): + result = await run_heavy_pipeline( + lib_store, item_id, storage_dir, quality="1080", + ) + + assert result["quality"] == "1080" # Explicit wins + + @pytest.mark.asyncio + async def test_run_heavy_pipeline_non_youtube(self, lib_store, storage_dir): + """run_heavy_pipeline returns None for non-YouTube items.""" + item_id = await lib_store.create_item( + kind="url:web", + source_url="https://example.com/page", + ) + + from tinyagentos.library_pipeline import run_heavy_pipeline + result = await run_heavy_pipeline( + lib_store, item_id, storage_dir, + ) + assert result is None + + @pytest.mark.asyncio + async def test_run_heavy_pipeline_creates_job(self, lib_store, storage_dir): + """Heavy pipeline creates a job entry.""" + item_id = await lib_store.create_item( + kind="url:youtube", + source_url="https://youtube.com/watch?v=job-test", + ) + storage_dir.joinpath("downloads").mkdir(parents=True, exist_ok=True) + + mock_path = str(storage_dir / "downloads" / "test.mp4") + storage_dir.joinpath("downloads", "test.mp4").write_text("fake data") + + from tinyagentos.library_pipeline import run_heavy_pipeline + + with patch( + "tinyagentos.knowledge_fetchers.youtube.download_video", + _async_return(mock_path), + ): + await run_heavy_pipeline(lib_store, item_id, storage_dir, quality="720") + + jobs = await lib_store.get_item_jobs(item_id) + heavy_jobs = [j for j in jobs if j["stage"] == "heavy_download"] + assert len(heavy_jobs) >= 1 + + +# --------------------------------------------------------------------------- +# P3: Library routes β€” download, rules, usage +# --------------------------------------------------------------------------- + + +class TestLibraryRoutesP3: + @pytest.mark.asyncio + async def test_trigger_download(self, client): + """POST /api/library/items/{id}/download triggers heavy download.""" + # Ingest a YouTube URL first + resp = await client.post( + "/api/library/ingest", data={"url": "https://youtube.com/watch?v=dl-test"} + ) + assert resp.status_code == 202 + item_id = resp.json()["item_id"] + + # Allow pipeline to finish + import asyncio as _asyncio + await _asyncio.sleep(0.5) + + resp = await client.post( + f"/api/library/items/{item_id}/download", + data={"quality": "480"}, + ) + assert resp.status_code == 202 + data = resp.json() + assert data["status"] == "downloading" + assert data["quality"] == "480" + + @pytest.mark.asyncio + async def test_trigger_download_nonexistent(self, client): + resp = await client.post("/api/library/items/nonexistent/download") + assert resp.status_code == 404 + + @pytest.mark.asyncio + async def test_trigger_download_non_youtube(self, client): + """Download endpoint rejects non-YouTube items.""" + resp = await client.post( + "/api/library/ingest", data={"url": "https://example.com/page"} + ) + assert resp.status_code == 202 + item_id = resp.json()["item_id"] + + import asyncio as _asyncio + await _asyncio.sleep(0.5) + + resp = await client.post(f"/api/library/items/{item_id}/download") + assert resp.status_code == 400 + + @pytest.mark.asyncio + async def test_download_status(self, client): + resp = await client.post( + "/api/library/ingest", data={"url": "https://youtube.com/watch?v=status"} + ) + item_id = resp.json()["item_id"] + + import asyncio as _asyncio + await _asyncio.sleep(0.5) + + resp = await client.get(f"/api/library/items/{item_id}/download/status") + assert resp.status_code == 200 + data = resp.json() + assert data["item_id"] == item_id + assert "downloaded" in data + + @pytest.mark.asyncio + async def test_create_rule(self, client): + resp = await client.post( + "/api/library/rules", + data={"source_pattern": "*.youtube.com/*", "quality": "1080", "auto_download": "true"}, + ) + assert resp.status_code == 201 + data = resp.json() + assert data["rule_id"] + assert data["source_pattern"] == "*.youtube.com/*" + + @pytest.mark.asyncio + async def test_list_rules(self, client): + # Create a rule first + await client.post( + "/api/library/rules", data={"source_pattern": "*.example.com/*"} + ) + resp = await client.get("/api/library/rules") + assert resp.status_code == 200 + data = resp.json() + assert "rules" in data + assert data["count"] >= 1 + + @pytest.mark.asyncio + async def test_delete_rule(self, client): + resp = await client.post( + "/api/library/rules", data={"source_pattern": "*.test.com/*"} + ) + rule_id = resp.json()["rule_id"] + + resp = await client.delete(f"/api/library/rules/{rule_id}") + assert resp.status_code == 200 + assert resp.json()["status"] == "deleted" + + # Verify it's gone + resp = await client.get("/api/library/rules") + data = resp.json() + rule_ids = [r["id"] for r in data["rules"]] + assert rule_id not in rule_ids + + @pytest.mark.asyncio + async def test_delete_nonexistent_rule(self, client): + resp = await client.delete("/api/library/rules/nonexistent") + assert resp.status_code == 404 + + @pytest.mark.asyncio + async def test_storage_usage(self, client): + resp = await client.get("/api/library/usage") + assert resp.status_code == 200 + data = resp.json() + assert "total_count" in data + assert "total_bytes" in data + assert "by_kind" in data + + # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- diff --git a/tinyagentos/library_pipeline.py b/tinyagentos/library_pipeline.py index bd6185333..5147b4971 100644 --- a/tinyagentos/library_pipeline.py +++ b/tinyagentos/library_pipeline.py @@ -930,6 +930,165 @@ def get_processor(kind: str, store: LibraryStore, return cls(store, storage_dir) +# --------------------------------------------------------------------------- +# Heavy tier β€” opt-in media download +# --------------------------------------------------------------------------- + + +class HeavyDownloadProcessor(Processor): + """Downloads media for items that have opted into the heavy tier. + + Currently supports url:youtube items via yt-dlp download_video. + Respects per-item quality preference and per-source rules. + """ + + _VALID_QUALITIES = frozenset({"360", "480", "720", "1080", "best"}) + + async def process(self, item: dict) -> list[dict]: + item_id = item["id"] + source_url = item.get("source_url", "") + artifacts: list[dict] = [] + + if not source_url: + return artifacts + + kind = item.get("kind", "") + if kind != "url:youtube": + return artifacts + + quality = item.get("quality", "") or "720" + if quality not in self._VALID_QUALITIES: + quality = "720" + + try: + from tinyagentos.knowledge_fetchers.youtube import download_video + except ImportError: + logger.warning("yt-dlp not available for heavy download") + return artifacts + + download_dir = self.storage_dir / "downloads" + download_dir.mkdir(parents=True, exist_ok=True) + + path = await download_video(source_url, quality=quality, output_dir=download_dir) + + if not path: + msg = f"Heavy download failed for {source_url!r}: yt-dlp returned no output path" + logger.warning(msg) + await self.store.update_item( + item_id, + meta_json={ + **json.loads(item.get("meta_json", "{}")), + "download_error": msg, + }, + ) + return artifacts + + p = Path(path) + if not p.exists(): + # Try to find the downloaded file + candidates = sorted( + download_dir.glob("*"), key=lambda x: x.stat().st_mtime, reverse=True + ) + if candidates: + p = candidates[0] + else: + await self.store.update_item( + item_id, + meta_json={ + **json.loads(item.get("meta_json", "{}")), + "download_error": "Downloaded file not found on disk", + }, + ) + return artifacts + + size_bytes = p.stat().st_size + await self.store.update_item( + item_id, + download_path=str(p), + download_bytes=size_bytes, + bytes=size_bytes, + downloaded_at=time.time(), + ) + + download_meta: dict = { + "path": str(p), + "bytes": size_bytes, + "quality": quality, + "format": p.suffix.lstrip("."), + } + await self.store.add_artifact( + item_id, kind="download", path=str(p), meta=download_meta, + ) + artifacts.append({ + "kind": "download", "path": str(p), "meta": download_meta, + }) + + return artifacts + + +async def run_heavy_pipeline( + store: LibraryStore, + item_id: str, + storage_dir: Path, + quality: str = "", +) -> dict | None: + """Run the heavy-tier download pipeline for one item. + + Checks per-source rules for auto_download settings, respects per-item + quality override, and downloads the media via yt-dlp. + + Returns download metadata dict on success, None if skipped or failed. + """ + item = await store.get_item(item_id) + if not item: + return None + + kind = item.get("kind", "") + source_url = item.get("source_url", "") + + # Only YouTube items are supported for heavy download currently + if kind != "url:youtube" or not source_url: + return None + + # Check for matching rules (apply first matching rule's quality if + # no explicit quality was provided) + if not quality: + rules = await store.match_rules(source_url) + if rules: + quality = rules[0].get("quality", "") or "720" + + # Fallback to item's quality field, then default 720 + if not quality: + quality = item.get("quality", "") or "720" + + # Create a job entry + await store.create_job(item_id, "heavy_download") + + try: + proc = HeavyDownloadProcessor(store, storage_dir) + # Override the item's quality for this run + item_with_quality = dict(item, quality=quality) + artifacts = await proc.process(item_with_quality) + + if artifacts: + await store.update_job( + (await store.get_item_jobs(item_id))[-1]["id"], + state="done", + ) + return artifacts[0].get("meta", {}) + else: + await store.update_job( + (await store.get_item_jobs(item_id))[-1]["id"], + state="error", + error="Download produced no artifacts", + ) + return None + except Exception: + logger.exception("Heavy pipeline failed for item %s", item_id) + await store.update_item_status(item_id, "error") + return None + + # --------------------------------------------------------------------------- # Pipeline runner # --------------------------------------------------------------------------- diff --git a/tinyagentos/library_store.py b/tinyagentos/library_store.py index 6d7b1d11c..2500e5044 100644 --- a/tinyagentos/library_store.py +++ b/tinyagentos/library_store.py @@ -59,6 +59,16 @@ ); CREATE INDEX IF NOT EXISTS idx_lj_item ON library_jobs(item_id); CREATE INDEX IF NOT EXISTS idx_lj_state ON library_jobs(state); + +CREATE TABLE IF NOT EXISTS library_rules ( + id TEXT PRIMARY KEY, + source_pattern TEXT NOT NULL, + quality TEXT NOT NULL DEFAULT '720', + auto_download INTEGER NOT NULL DEFAULT 0, + enabled INTEGER NOT NULL DEFAULT 1, + created_at REAL NOT NULL +); +CREATE INDEX IF NOT EXISTS idx_lr_pattern ON library_rules(source_pattern); """ _VALID_STATUSES = frozenset({"pending", "processing", "ready", "error"}) @@ -68,10 +78,28 @@ class LibraryStore(BaseStore): SCHEMA = LIBRARY_SCHEMA async def _post_init(self) -> None: - """Enable foreign key enforcement for cascade deletes.""" + """Enable foreign key enforcement and apply schema migrations.""" await self._db.execute("PRAGMA foreign_keys = ON") await self._db.commit() + # P3 migration: add quality, auto_download, downloaded_at columns + # if they don't exist yet (existing databases from P1/P2). + cols = await self._db.execute_fetchall("PRAGMA table_info(library_items)") + col_names = {row[1] for row in cols} + for col, col_def in [ + ("quality", "TEXT NOT NULL DEFAULT ''"), + ("auto_download", "INTEGER NOT NULL DEFAULT 0"), + ("downloaded_at", "REAL"), + ("download_path", "TEXT NOT NULL DEFAULT ''"), + ("download_bytes", "INTEGER NOT NULL DEFAULT 0"), + ]: + if col not in col_names: + await self._db.execute( + f"ALTER TABLE library_items ADD COLUMN {col} {col_def}" + ) + if col_names: + await self._db.commit() + # -- items ------------------------------------------------------------ async def create_item( @@ -140,7 +168,9 @@ async def list_items( async def update_item(self, item_id: str, **kwargs) -> None: allowed = {"kind", "source_url", "title", "status", "storage_path", - "bytes", "meta_json", "updated_at"} + "bytes", "meta_json", "updated_at", + "quality", "auto_download", "download_path", + "download_bytes", "downloaded_at"} fields = [(k, v) for k, v in kwargs.items() if k in allowed] if not fields: return @@ -273,3 +303,83 @@ async def update_job(self, job_id: str, **kwargs) -> None: f"UPDATE library_jobs SET {set_clause} WHERE id = ?", values ) await self._db.commit() + + # -- source rules ----------------------------------------------------- + + async def create_rule( + self, + source_pattern: str, + quality: str = "720", + auto_download: bool = False, + enabled: bool = True, + ) -> str: + rule_id = uuid.uuid4().hex[:16] + now = time.time() + await self._db.execute( + """INSERT INTO library_rules + (id, source_pattern, quality, auto_download, enabled, created_at) + VALUES (?, ?, ?, ?, ?, ?)""", + (rule_id, source_pattern, quality, int(auto_download), int(enabled), now), + ) + await self._db.commit() + return rule_id + + async def list_rules(self) -> list[dict]: + self._db.row_factory = aiosqlite.Row + async with self._db.execute( + "SELECT * FROM library_rules ORDER BY created_at DESC" + ) as cursor: + rows = await cursor.fetchall() + return [dict(r) for r in rows] + + async def get_rule(self, rule_id: str) -> dict | None: + self._db.row_factory = aiosqlite.Row + async with self._db.execute( + "SELECT * FROM library_rules WHERE id = ?", (rule_id,) + ) as cursor: + row = await cursor.fetchone() + return dict(row) if row else None + + async def delete_rule(self, rule_id: str) -> None: + await self._db.execute( + "DELETE FROM library_rules WHERE id = ?", (rule_id,) + ) + await self._db.commit() + + async def match_rules(self, source_url: str) -> list[dict]: + """Return all enabled rules whose source_pattern matches source_url.""" + self._db.row_factory = aiosqlite.Row + async with self._db.execute( + "SELECT * FROM library_rules WHERE enabled = 1 ORDER BY created_at" + ) as cursor: + rows = await cursor.fetchall() + rules = [dict(r) for r in rows] + import fnmatch + return [ + r for r in rules + if fnmatch.fnmatch(source_url.lower(), r["source_pattern"].lower()) + ] + + # -- storage accounting ----------------------------------------------- + + async def get_storage_summary(self) -> dict: + """Return total bytes, item count, and per-kind breakdown.""" + self._db.row_factory = aiosqlite.Row + rows = await self._db.execute_fetchall( + "SELECT kind, COUNT(*) as count, " + "COALESCE(SUM(bytes), 0) as total_bytes " + "FROM library_items GROUP BY kind" + ) + by_kind = {row["kind"]: {"count": row["count"], "bytes": row["total_bytes"]} for row in rows} + + total = await self._db.execute_fetchall( + "SELECT COUNT(*) as total_count, COALESCE(SUM(bytes), 0) as total_bytes " + "FROM library_items" + ) + if total: + return { + "total_count": total[0]["total_count"], + "total_bytes": total[0]["total_bytes"], + "by_kind": by_kind, + } + return {"total_count": 0, "total_bytes": 0, "by_kind": {}} diff --git a/tinyagentos/routes/library.py b/tinyagentos/routes/library.py index f679d327b..dafe9384b 100644 --- a/tinyagentos/routes/library.py +++ b/tinyagentos/routes/library.py @@ -190,6 +190,27 @@ async def _ingest_task(app, item_id: str, store, storage_dir: Path) -> None: await store.update_item_status(item_id, "error") return + # Auto-download: check matching rules with auto_download=True + try: + item = await store.get_item(item_id) + if item and item.get("source_url"): + rules = await store.match_rules(item["source_url"]) + for rule in rules: + if rule.get("auto_download"): + from tinyagentos.library_pipeline import run_heavy_pipeline + + quality = rule.get("quality", "") or "720" + logger.info( + "Auto-download triggered for item %s by rule %s (quality=%s)", + item_id, rule["id"], quality, + ) + await run_heavy_pipeline( + store, item_id, storage_dir, quality=quality, + ) + break # First matching auto-download rule wins + except Exception: + logger.exception("Auto-download check failed for item %s", item_id) + # Collections handoff after successful pipeline try: collections_dir = storage_dir.parent / "collections" @@ -228,6 +249,17 @@ def _is_htmx(request: Request) -> bool: return request.headers.get("HX-Request", "").lower() == "true" +def _format_bytes(size: int) -> str: + """Format a byte count as a human-readable string.""" + if size < 1024: + return f"{size} B" + for unit in ("KB", "MB", "GB", "TB"): + size /= 1024.0 + if size < 1024 or unit == "TB": + return f"{size:.1f} {unit}" + return f"{size:.1f} TB" + + _STATUS_CSS: dict[str, str] = { "pending": "status-pending", "processing": "status-processing", @@ -244,22 +276,60 @@ def _render_item_card(item: dict) -> str: css = _STATUS_CSS.get(status, "status-pending") title = html.escape(item.get("title", "Untitled")) kind = html.escape(item.get("kind", "file")) - size = item.get("size_bytes") or 0 - size_str = f"{size:,} B" if size else "" + bytes_val = item.get("bytes") or 0 + size_str = _format_bytes(bytes_val) if bytes_val else "" item_id = html.escape(item.get("id", "")) + source_url = html.escape(item.get("source_url", "")) + downloaded = bool(item.get("download_path", "")) + + parts = [ + f'
', + f'
', + f'

{title}

', + f'
', + f'{html.escape(status)}', + f" {kind}", + ] + + if size_str: + parts.append(f' · {size_str}') + + parts.append("
") # .meta + + # YouTube download actions (only for url:youtube items, only when ready) + if kind == "url:youtube" and status == "ready" and not downloaded: + parts.append( + f'
' + f' ' + f'' + f'
' + ) + elif kind == "url:youtube" and downloaded: + download_bytes_val = item.get("download_bytes") or 0 + parts.append( + f'
' + f'βœ… Downloaded ({_format_bytes(download_bytes_val)})' + f'
' + ) - return ( - f'
' - f'
' - f'

{title}

' - f'
' - f'{html.escape(status)}' - f" {kind}" - f'{f" · {size_str}" if size_str else ""}' - f"
" - f"
" - f"
" - ) + parts.append("
") # .info + parts.append("
") # .item-card + + return "".join(parts) def _render_item_list(items: list[dict]) -> str: @@ -273,6 +343,47 @@ def _render_item_list(items: list[dict]) -> str: return "".join(_render_item_card(item) for item in items) +def _render_rules_list(rules: list[dict]) -> str: + """Return an HTML fragment listing source rules.""" + import html as _h + + if not rules: + return 'No rules. Add one above to auto-download from matching sources.' + + parts: list[str] = ['") + return "".join(parts) + + +def _render_storage_summary(summary: dict) -> str: + """Return an HTML snippet for the storage summary bar.""" + total_bytes = summary.get("total_bytes", 0) + total_count = summary.get("total_count", 0) + if not total_count: + return 'No items stored yet.' + + return ( + f'Library storage: {_format_bytes(total_bytes)} ' + f'across {total_count} item{"s" if total_count != 1 else ""}' + f'' + ) + + # --------------------------------------------------------------------------- # List / Get / Delete # --------------------------------------------------------------------------- @@ -367,3 +478,149 @@ async def reprocess_item(request: Request, item_id: str): _create_supervised_task(coro, task_set) return JSONResponse({"item_id": item_id, "status": "reprocessing"}, status_code=202) + + +# --------------------------------------------------------------------------- +# Heavy tier: download +# --------------------------------------------------------------------------- + + +@router.post("/api/library/items/{item_id}/download") +async def trigger_download( + request: Request, + item_id: str, + quality: str | None = Form(None), +): + """Trigger heavy-tier media download for an item. + + Accepts optional ``quality`` form field (360, 480, 720, 1080, best). + Runs the download asynchronously in a background task. + """ + store = await _get_library_store(request) + item = await store.get_item(item_id) + if not item: + return JSONResponse({"error": f"Item {item_id!r} not found"}, status_code=404) + + if item["kind"] != "url:youtube": + return JSONResponse( + {"error": "Heavy download only supports url:youtube items"}, + status_code=400, + ) + + storage_dir = _library_dir(request) + quality_val = quality or item.get("quality", "") or "720" + + task_set = getattr(request.app.state, "_background_tasks", None) + coro = _heavy_download_task(request.app, item_id, store, storage_dir, quality_val) + if task_set is None: + _track_background_task(coro) + else: + _create_supervised_task(coro, task_set) + + return JSONResponse( + {"item_id": item_id, "status": "downloading", "quality": quality_val}, + status_code=202, + ) + + +async def _heavy_download_task( + app, item_id: str, store, storage_dir: Path, quality: str +) -> None: + """Background task: run heavy download for an item.""" + from tinyagentos.library_pipeline import run_heavy_pipeline + + try: + result = await run_heavy_pipeline(store, item_id, storage_dir, quality=quality) + if result: + logger.info("Heavy download complete for item %s: %s", item_id, result) + except Exception: + logger.exception("Heavy download crashed for item %s", item_id) + await store.update_item_status(item_id, "error") + + +@router.get("/api/library/items/{item_id}/download/status") +async def download_status(request: Request, item_id: str): + """Check heavy download status for an item.""" + store = await _get_library_store(request) + item = await store.get_item(item_id) + if not item: + return JSONResponse({"error": f"Item {item_id!r} not found"}, status_code=404) + + jobs = await store.get_item_jobs(item_id) + heavy_jobs = [j for j in jobs if j.get("stage") == "heavy_download"] + download_path = item.get("download_path", "") + download_bytes = item.get("download_bytes", 0) + + return { + "item_id": item_id, + "downloaded": bool(download_path), + "download_path": download_path, + "download_bytes": download_bytes, + "jobs": heavy_jobs, + } + + +# --------------------------------------------------------------------------- +# Source rules +# --------------------------------------------------------------------------- + + +@router.post("/api/library/rules") +async def create_rule( + request: Request, + source_pattern: str = Form(...), + quality: str | None = Form("720"), + auto_download: bool = Form(False), +): + """Create a source rule for automatic heavy download triggers.""" + store = await _get_library_store(request) + rule_id = await store.create_rule( + source_pattern=source_pattern, + quality=quality or "720", + auto_download=auto_download, + ) + return JSONResponse( + {"rule_id": rule_id, "source_pattern": source_pattern, "status": "created"}, + status_code=201, + ) + + +@router.get("/api/library/rules") +async def list_rules(request: Request): + """List all source rules.""" + store = await _get_library_store(request) + rules = await store.list_rules() + + if _is_htmx(request): + return HTMLResponse(_render_rules_list(rules)) + + return {"rules": rules, "count": len(rules)} + + +@router.delete("/api/library/rules/{rule_id}") +async def delete_rule(request: Request, rule_id: str): + """Delete a source rule.""" + store = await _get_library_store(request) + rule = await store.get_rule(rule_id) + if not rule: + return JSONResponse({"error": f"Rule {rule_id!r} not found"}, status_code=404) + + await store.delete_rule(rule_id) + return {"status": "deleted", "rule_id": rule_id} + + +# --------------------------------------------------------------------------- +# Storage accounting +# --------------------------------------------------------------------------- + + +@router.get("/api/library/usage") +async def storage_usage(request: Request): + """Return storage accounting summary for the library.""" + store = await _get_library_store(request) + summary = await store.get_storage_summary() + + if _is_htmx(request): + return HTMLResponse(_render_storage_summary(summary)) + + return summary diff --git a/tinyagentos/templates/library.html b/tinyagentos/templates/library.html index 6fdcb1d44..76731257e 100644 --- a/tinyagentos/templates/library.html +++ b/tinyagentos/templates/library.html @@ -62,6 +62,15 @@

πŸ“š Library

+ +
+ Loading storage info… +
+
πŸ“š Library
+ +
+
+ + βš™ Source Rules + +
+
+ + + + +
+
+ Loading rules… +
+
+
+
+