File size: 7,258 Bytes
5196bf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
"""
Project Jarvis β€” Command Line Interface
Interactive CLI for testing Jarvis from the terminal.
Supports both text and voice modes.
"""

import sys
import os
import time

# Add backend to path
sys.path.insert(0, os.path.dirname(__file__))

from rich.console import Console
from rich.panel import Panel
from rich.text import Text
from rich.table import Table
from rich.live import Live
from rich import box


console = Console()


def print_banner():
    banner = """
╔══════════════════════════════════════════════════╗
β•‘                                                  β•‘
β•‘          β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•—   β–ˆβ–ˆβ•—β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β•‘
β•‘          β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•β•β• β•‘
β•‘          β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β•‘
β•‘     β–ˆβ–ˆ   β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β•šβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘β•šβ•β•β•β•β–ˆβ–ˆβ•‘ β•‘
β•‘     β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘ β•šβ–ˆβ–ˆβ–ˆβ–ˆβ•”β• β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β•‘
β•‘      β•šβ•β•β•β•β• β•šβ•β•  β•šβ•β•β•šβ•β•  β•šβ•β•  β•šβ•β•β•β•  β•šβ•β•β•šβ•β•β•β•β•β•β• β•‘
β•‘                                                  β•‘
β•‘        Personal AI Assistant β€” 100% Local        β•‘
β•‘                                                  β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
"""
    console.print(banner, style="bold blue")


def init_services():
    """Initialize all Jarvis services."""
    console.print("\n[bold yellow]Initializing Jarvis...[/bold yellow]\n")

    from app.core import brain
    from config import settings

    table = Table(box=box.ROUNDED, title="Service Status", title_style="bold")
    table.add_column("Service", style="cyan")
    table.add_column("Status", style="green")
    table.add_column("Detail")

    try:
        brain.init()
        statuses = brain.get_all_status()
        for svc in statuses:
            status_style = {
                "ready": "[green]βœ“ Ready[/green]",
                "error": "[red]βœ— Error[/red]",
                "not_loaded": "[yellow]β—Œ Not loaded[/yellow]",
                "unavailable": "[dim]β€” Unavailable[/dim]",
            }.get(svc["status"], f"[dim]{svc['status']}[/dim]")

            table.add_row(svc["name"].upper(), status_style, svc.get("detail", ""))

        console.print(table)
        console.print()
        return True
    except Exception as e:
        console.print(f"[bold red]Initialization error: {e}[/bold red]")
        return False


def text_mode():
    """Interactive text command mode."""
    from app.core import brain

    console.print("[bold green]Text mode active.[/bold green] Type your commands below.")
    console.print("[dim]Type 'quit' to exit, 'voice' to switch to voice mode, 'status' for service status.[/dim]\n")

    session_id = "cli-session"

    while True:
        try:
            user_input = console.input("[bold cyan]You:[/bold cyan] ").strip()

            if not user_input:
                continue

            if user_input.lower() in ("quit", "exit", "q"):
                console.print("[dim]Goodbye, sir.[/dim]")
                break

            if user_input.lower() == "voice":
                voice_mode()
                continue

            if user_input.lower() == "status":
                show_status()
                continue

            if user_input.lower() == "clear":
                console.clear()
                print_banner()
                continue

            # Process the command
            start = time.time()
            result = brain.process_text(user_input, session_id=session_id)
            elapsed = (time.time() - start) * 1000

            response = result.get("response_text", "I couldn't process that.")
            intent = result.get("intent", "unknown")

            console.print()
            console.print(f"[bold magenta]Jarvis:[/bold magenta] {response}")
            console.print(f"[dim]  intent: {intent} | {elapsed:.0f}ms[/dim]\n")

        except KeyboardInterrupt:
            console.print("\n[dim]Interrupted. Type 'quit' to exit.[/dim]")
        except EOFError:
            break


def voice_mode():
    """Voice command mode β€” records, transcribes, processes, speaks."""
    from app.core import brain
    from app.services import tts

    console.print("\n[bold green]Voice mode active.[/bold green] Speak after the prompt.")
    console.print("[dim]Press Ctrl+C to stop and return to text mode.[/dim]\n")

    session_id = "cli-voice"

    while True:
        try:
            console.print("[bold yellow]🎀 Listening...[/bold yellow] (speak now)")

            # Record and process
            result = brain.voice_loop_once()

            transcription = result.get("transcription", "")
            response = result.get("response_text", "")
            intent = result.get("intent", "unknown")

            if transcription:
                console.print(f"[bold cyan]You said:[/bold cyan] {transcription}")
            console.print(f"[bold magenta]Jarvis:[/bold magenta] {response}")
            console.print(f"[dim]  intent: {intent} | {result.get('timing', {}).get('total_ms', 0):.0f}ms[/dim]\n")

        except KeyboardInterrupt:
            console.print("\n[dim]Returning to text mode...[/dim]\n")
            break


def show_status():
    """Show service status."""
    from app.core import brain

    table = Table(box=box.ROUNDED, title="Service Status")
    table.add_column("Service", style="cyan")
    table.add_column("Status")
    table.add_column("Detail")

    for svc in brain.get_all_status():
        status_icon = {"ready": "βœ“", "error": "βœ—", "unavailable": "β€”"}.get(svc["status"], "?")
        status_style = {"ready": "green", "error": "red", "unavailable": "dim"}.get(svc["status"], "yellow")
        table.add_row(
            svc["name"].upper(),
            f"[{status_style}]{status_icon} {svc['status']}[/{status_style}]",
            svc.get("detail", ""),
        )

    console.print(table)
    console.print()


def main():
    print_banner()

    if not init_services():
        console.print("[bold red]Failed to initialize. Make sure Ollama is running.[/bold red]")
        console.print("[dim]Run: ollama serve[/dim]")
        sys.exit(1)

    # Check for command line args
    if len(sys.argv) > 1:
        if sys.argv[1] == "--voice":
            voice_mode()
            return
        else:
            # Process single command
            from app.core import brain
            text = " ".join(sys.argv[1:])
            result = brain.process_text(text)
            console.print(f"[bold magenta]Jarvis:[/bold magenta] {result['response_text']}")
            return

    # Default: interactive text mode
    text_mode()


if __name__ == "__main__":
    main()