dp-zp-agent/tools/runtime/bootstrap_indexes.py

45 lines
1.6 KiB
Python

"""Keep a valid generation, or create both indexes before starting the API."""
import hashlib
import json
from app.security import validate_security_configuration
from tools.rag.common import DB_FILE, ZPWIKI_ROOT
from tools.rag.embedding_utils import embed_query
from tools.rag.rebuild_index import rebuild_index
from tools.runtime.runtime_assets import asset_root, verify
from tools.sync.sync_indexes import git_revision, source_files
from tools.runtime.tool_health import tools_readiness
def main():
validate_security_configuration()
verify(asset_root())
embed_query("overenie pripravenosti")
tools, version = tools_readiness(DB_FILE, security_configured=True, embedding_ready=True)
source_hash = hashlib.sha256(
json.dumps(source_files(ZPWIKI_ROOT), sort_keys=True).encode()
).hexdigest()
current = (
all(value["ready"] for value in tools.values())
and version
and version.get("git_revision") == git_revision(ZPWIKI_ROOT)
and version.get("source_sha256") == source_hash
)
if not current:
result = rebuild_index(pull_git=False)
tools, version = tools_readiness(DB_FILE, security_configured=True, embedding_ready=True)
if not all(value["ready"] for value in tools.values()):
raise RuntimeError("Nové indexy nie sú pripravené; API sa nespustí.")
print(
json.dumps(
{"bootstrap": "rebuilt", "counts": result["counts"], "index_version": version}
)
)
else:
print(json.dumps({"bootstrap": "existing", "index_version": version}))
if __name__ == "__main__":
main()