Check pypi version on startup

This commit is contained in:
Yiorgis Gozadinos 2025-07-14 12:13:01 +03:00
parent a2d7e527d1
commit f4aef4eb9c
No known key found for this signature in database
2 changed files with 42 additions and 1 deletions

View file

@ -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(

View file

@ -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