File size: 3,821 Bytes
a2b0d2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
Bot runner for local development.

This script is called as a subprocess by the local FastAPI server.
It reads configuration from command line arguments and starts the bot.
"""

import argparse
import asyncio
import json
import os
import sys
from loguru import logger

# Add parent directory (project root) to Python path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from bot_config import BotConfig


def parse_args():
    """Parse command line arguments."""
    parser = argparse.ArgumentParser(description="Pipecat bot runner")
    parser.add_argument("--session-id", required=True, help="Session ID for the bot")
    parser.add_argument("--room-url", required=True, help="Daily room URL")
    parser.add_argument("--token", required=True, help="Daily room token")

    # Bot data can be provided as JSON string or file
    bot_data_group = parser.add_mutually_exclusive_group(required=True)
    bot_data_group.add_argument("--bot-data", help="JSON bot configuration data")
    bot_data_group.add_argument(
        "--bot-config-file", help="Path to JSON bot configuration file"
    )

    parser.add_argument("--bot-name", default="bot", help="Bot name")
    parser.add_argument("--recording-dir", default="/tmp", help="Recording directory")
    parser.add_argument(
        "--local-recording", action="store_true", help="Enable local recording"
    )
    parser.add_argument(
        "--disable-rtvi",
        action="store_true",
        help="Disable RTVI processor and observer",
    )
    return parser.parse_args()


async def main():
    """Main entry point for the bot runner."""
    try:
        # Parse command line arguments
        args = parse_args()

        # Parse and validate bot data
        try:
            if args.bot_data:
                raw_bot_data = json.loads(args.bot_data)
            elif args.bot_config_file:
                with open(args.bot_config_file, "r") as f:
                    raw_bot_data = json.load(f)
                logger.info(f"πŸ“„ Loaded bot config from file: {args.bot_config_file}")
            else:
                logger.error("❌ No bot data provided")
                sys.exit(1)
        except FileNotFoundError as e:
            logger.error(f"❌ Bot config file not found: {e}")
            sys.exit(1)
        except json.JSONDecodeError as e:
            logger.error(f"❌ Failed to parse bot data JSON: {e}")
            sys.exit(1)
        except ValueError as e:
            logger.error(f"❌ Invalid bot data format: {e}")
            sys.exit(1)
        except Exception as e:
            logger.error(f"❌ Failed to validate bot data: {e}")
            sys.exit(1)

        # Create bot config
        config = BotConfig(
            session_id=args.session_id,
            room_url=args.room_url,
            token=args.token,
            bot_data=raw_bot_data,
            bot_name=args.bot_name,
            recording_dir=args.recording_dir,
            local_recording=args.local_recording,
            rtvi_enabled=not args.disable_rtvi,
        )

        logger.info(f"πŸ€– Starting bot runner for session: {config.session_id}")
        logger.info(f"🏠 Room URL: {config.room_url}")
        logger.info(f"πŸ“‹ Bot name: {config.bot_name}")

        # Import and run the bot
        from bot import main as bot_main

        await bot_main(config)

    except KeyboardInterrupt:
        logger.info("πŸ›‘ Bot runner interrupted by user")


if __name__ == "__main__":
    # Configure logging
    logger.remove()  # Remove default handler
    logger.add(
        sys.stdout,
        format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>bot</cyan> | <level>{message}</level>",
        level="INFO",
    )

    # Run the bot
    asyncio.run(main())