#!/usr/bin/env python3 """ Startup script for chat service with documentation indexing. Runs indexing if needed, then starts the chat server. """ import asyncio import logging import os import subprocess import sys from pathlib import Path logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" ) logger = logging.getLogger(__name__) async def main() -> None: """Main startup routine""" logger.info("=" * 50) logger.info("Datacenter Documentation Chat Service") logger.info("Starting initialization...") logger.info("=" * 50) # Check if vector store needs initialization vector_store_path = Path("/app/data/chroma_db") index_marker = vector_store_path / ".indexed" if not index_marker.exists(): logger.info("") logger.info("=" * 50) logger.info("First Time Setup") logger.info("=" * 50) logger.info("Indexing documentation into vector store...") logger.info("This may take a few minutes...") logger.info("") # Run indexing script try: result = subprocess.run( [sys.executable, "/app/scripts/index_docs.py"], check=True, capture_output=True, text=True ) logger.info(result.stdout) # Create marker file vector_store_path.mkdir(parents=True, exist_ok=True) index_marker.touch() logger.info("") logger.info("✓ Documentation indexed successfully!") except subprocess.CalledProcessError as e: logger.error("") logger.error(f"⚠ Warning: Documentation indexing failed: {e}") logger.error(e.stdout) logger.error(e.stderr) logger.error(" The chat service will still start but won't have access to indexed documentation.") else: logger.info(f"✓ Vector store already initialized (marker: {index_marker})") logger.info(" To re-index, delete the volume: docker volume rm datacenter-docs-chat-data-dev") logger.info("") logger.info("=" * 50) logger.info("Starting Chat Server") logger.info("=" * 50) logger.info("Listening on port 8001...") logger.info("") # Start the chat server by importing and running it # This keeps everything in the same process os.chdir("/app") sys.path.insert(0, "/app/src") from datacenter_docs.chat import main as chat_main # Run the chat server import uvicorn from datacenter_docs.chat.main import socket_app uvicorn.run(socket_app, host="0.0.0.0", port=8001) if __name__ == "__main__": asyncio.run(main())