From f4aef4eb9c55d4ad6fb7e9c7732f201a64c4077e Mon Sep 17 00:00:00 2001 From: Yiorgis Gozadinos Date: Mon, 14 Jul 2025 12:13:01 +0300 Subject: [PATCH] Check pypi version on startup --- src/haiku/rag/cli.py | 19 ++++++++++++++++++- src/haiku/rag/utils.py | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/haiku/rag/cli.py b/src/haiku/rag/cli.py index 426af784..80ee047e 100644 --- a/src/haiku/rag/cli.py +++ b/src/haiku/rag/cli.py @@ -5,7 +5,7 @@ import typer from rich.console import Console from haiku.rag.app import HaikuRAGApp -from haiku.rag.utils import get_default_data_dir +from haiku.rag.utils import get_default_data_dir, is_up_to_date cli = typer.Typer( context_settings={"help_option_names": ["-h", "--help"]}, no_args_is_help=True @@ -15,6 +15,23 @@ console = Console() event_loop = asyncio.get_event_loop() +async def check_version(): + """Check if haiku.rag is up to date and show warning if not.""" + up_to_date, current_version, latest_version = await is_up_to_date() + if not up_to_date: + console.print( + f"[yellow]Warning: haiku.rag is outdated. Current: {current_version}, Latest: {latest_version}[/yellow]" + ) + console.print("[yellow]Please update.[/yellow]") + + +@cli.callback() +def main(): + """haiku.rag CLI - SQLite-based RAG system""" + # Run version check before any command + event_loop.run_until_complete(check_version()) + + @cli.command("list", help="List all stored documents") def list_documents( db: Path = typer.Option( diff --git a/src/haiku/rag/utils.py b/src/haiku/rag/utils.py index 03c160bf..8b78f008 100644 --- a/src/haiku/rag/utils.py +++ b/src/haiku/rag/utils.py @@ -1,6 +1,10 @@ import sys +from importlib import metadata from pathlib import Path +import httpx +from packaging.version import Version, parse + def get_default_data_dir() -> Path: """ @@ -23,3 +27,23 @@ def get_default_data_dir() -> Path: data_path = system_paths[sys.platform] return data_path + + +async def is_up_to_date() -> tuple[bool, Version, Version]: + """ + Checks whether haiku.rag is current. + + :return: A tuple containing a boolean indicating whether haiku.rag is current, the running version and the latest version + :rtype: tuple[bool, Version, Version] + """ + + async with httpx.AsyncClient() as client: + running_version = parse(metadata.version("haiku.rag")) + try: + response = await client.get("https://pypi.org/pypi/haiku.rag/json") + data = response.json() + pypi_version = parse(data["info"]["version"]) + except Exception: + # If no network connection, do not raise alarms. + pypi_version = running_version + return running_version >= pypi_version, running_version, pypi_version