File size: 4,262 Bytes
93917f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
"""

Command-line interface for Codette AI

"""
import argparse
import sys
import logging
from typing import Optional

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

try:
    from codette_new import Codette
except ImportError:
    logger.warning("Could not import from codette_new, attempting fallback import...")
    try:
        from Codette.codette_new import Codette
    except ImportError:
        logger.error("Failed to import Codette class. Ensure codette_new.py is available.")
        sys.exit(1)


def process_single_query(prompt: str, user_name: str = "CLI_User") -> str:
    """Process a single query and return response

    

    Args:

        prompt: The user prompt to process

        user_name: Name for personalization

        

    Returns:

        Codette's response

    """
    try:
        codette = Codette(user_name=user_name)
        response = codette.respond(prompt)
        return response
    except Exception as e:
        logger.error(f"Error processing query: {e}")
        return f"Error: {str(e)}"


def process_interactive_mode(user_name: str = "CLI_User"):
    """Run Codette in interactive mode

    

    Args:

        user_name: Name for personalization

    """
    try:
        codette = Codette(user_name=user_name)
        print(f"\n{'='*60}")
        print("🤖 Codette CLI - Interactive Mode")
        print(f"{'='*60}")
        print(f"Hello {user_name}! Type your queries. 'exit' to quit.\n")
        
        while True:
            try:
                prompt = input(f"{user_name}: ").strip()
                if not prompt:
                    continue
                if prompt.lower() in ('exit', 'quit'):
                    print("\nGoodbye!")
                    break
                
                response = codette.respond(prompt)
                print(f"\nCodette: {response}\n")
                
            except KeyboardInterrupt:
                print("\n\nInterrupted. Goodbye!")
                break
                
    except Exception as e:
        logger.error(f"Error in interactive mode: {e}")
        print(f"Fatal error: {e}")
        sys.exit(1)


def main():
    """Main CLI entry point"""
    parser = argparse.ArgumentParser(
        description='Codette AI Command-Line Interface',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""

Examples:

  # Single query

  python codette_cli.py "What is the nature of consciousness?"

  

  # Interactive mode

  python codette_cli.py -i

  

  # With custom user name

  python codette_cli.py -u Alice "Tell me something profound"

        """
    )
    
    parser.add_argument(
        'prompt',
        nargs='?',
        default=None,
        help='Query prompt for Codette to process (optional)'
    )
    parser.add_argument(
        '-i', '--interactive',
        action='store_true',
        help='Run in interactive mode (ignore prompt if provided)'
    )
    parser.add_argument(
        '-u', '--user',
        default='CLI_User',
        help='User name for personalization (default: CLI_User)'
    )
    parser.add_argument(
        '-v', '--verbose',
        action='store_true',
        help='Enable verbose logging'
    )
    
    args = parser.parse_args()
    
    # Set logging level
    if args.verbose:
        logging.getLogger().setLevel(logging.DEBUG)
    
    # Run in interactive mode if requested or no prompt provided
    if args.interactive or args.prompt is None:
        process_interactive_mode(user_name=args.user)
    else:
        # Process single query
        response = process_single_query(args.prompt, user_name=args.user)
        print(f"\n{'='*60}")
        print("🤖 Codette CLI Output")
        print(f"{'='*60}\n")
        print(response)
        print(f"\n{'='*60}\n")


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print("\n\nShutting down...")
    except Exception as e:
        logger.error(f"Fatal error: {e}")
        sys.exit(1)