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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
"""
AGI CLI - Custom Terminal Application
A beautiful, minimal terminal interface for AGI CLI
"""

import customtkinter as ctk
import threading
import queue
import sys
import os
from typing import Callable, Optional, List
from PIL import Image
import tkinter as tk

# Add the project root to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

# Version
VERSION = "1.0.0"


class TokenTracker:
    """Global token usage tracker for AI model interactions."""
    
    def __init__(self):
        self.tokens_sent = 0
        self.tokens_received = 0
        self._lock = threading.Lock()
        self._callbacks: List[Callable] = []
    
    def add_tokens(self, sent: int = 0, received: int = 0):
        """Add tokens to the counters."""
        with self._lock:
            self.tokens_sent += sent
            self.tokens_received += received
        self._notify_callbacks()
    
    def reset(self):
        """Reset the token counters."""
        with self._lock:
            self.tokens_sent = 0
            self.tokens_received = 0
        self._notify_callbacks()
    
    def get_totals(self) -> tuple:
        """Get current token totals."""
        with self._lock:
            return (self.tokens_sent, self.tokens_received)
    
    def register_callback(self, callback: Callable):
        """Register a callback to be notified on token updates."""
        self._callbacks.append(callback)
    
    def _notify_callbacks(self):
        """Notify all registered callbacks."""
        for callback in self._callbacks:
            try:
                callback()
            except Exception:
                pass


# Global token tracker instance
token_tracker = TokenTracker()


class SettingsWindow(ctk.CTkToplevel):
    """Settings window for AGI CLI configuration."""
    
    def __init__(self, parent):
        super().__init__(parent)
        
        self.title("AGI CLI Settings")
        self.geometry("500x400")
        self.minsize(400, 300)
        
        # Make it modal-like
        self.transient(parent)
        self.grab_set()
        
        # Dark theme
        self.configure(fg_color="#0D1117")
        
        # Header
        header = ctk.CTkFrame(self, fg_color="#161B22", corner_radius=0, height=50)
        header.pack(fill="x")
        header.pack_propagate(False)
        
        title = ctk.CTkLabel(
            header,
            text="โš™๏ธ  Settings",
            font=ctk.CTkFont(family="SF Mono, Monaco, Consolas, monospace", size=16, weight="bold"),
            text_color="#F8F9FA"
        )
        title.pack(side="left", padx=20, pady=12)
        
        # Content area
        content = ctk.CTkFrame(self, fg_color="transparent")
        content.pack(fill="both", expand=True, padx=20, pady=20)
        
        # Placeholder message
        placeholder = ctk.CTkLabel(
            content,
            text="Settings coming soon...\n\nThis is a placeholder settings page.\nConfiguration options will be added here.",
            font=ctk.CTkFont(family="SF Mono, Monaco, Consolas, monospace", size=14),
            text_color="#8B949E",
            justify="center"
        )
        placeholder.pack(expand=True)
        
        # Close button
        close_btn = ctk.CTkButton(
            content,
            text="Close",
            font=ctk.CTkFont(family="SF Mono, Monaco, Consolas, monospace", size=13),
            fg_color="#30363D",
            hover_color="#484F58",
            text_color="#F8F9FA",
            corner_radius=6,
            height=36,
            command=self.destroy
        )
        close_btn.pack(side="bottom", pady=10)
        
        # Center on parent
        self.update_idletasks()
        parent_x = parent.winfo_x()
        parent_y = parent.winfo_y()
        parent_w = parent.winfo_width()
        parent_h = parent.winfo_height()
        w = self.winfo_width()
        h = self.winfo_height()
        x = parent_x + (parent_w - w) // 2
        y = parent_y + (parent_h - h) // 2
        self.geometry(f"+{x}+{y}")


class CustomTerminal(ctk.CTkFrame):
    """A custom terminal widget with modern, minimal design."""
    
    # ANSI color mapping to CTk-compatible colors (dark theme optimized)
    COLOR_MAP = {
        "red": "#FF6B6B",
        "green": "#69DB7C",
        "yellow": "#FFE066",
        "blue": "#74C0FC",
        "magenta": "#DA77F2",
        "cyan": "#66D9E8",
        "orange": "#FFA94D",
        "black": "#868E96",
        "white": "#F8F9FA",
        "reset": "#DEE2E6",
    }
    
    def __init__(self, master, on_settings_click: Callable = None, **kwargs):
        super().__init__(master, **kwargs)
        
        self.configure(fg_color="transparent")
        self.on_settings_click = on_settings_click
        
        # Command history
        self.command_history = []
        self.history_index = -1
        
        # Input callback
        self.input_callback: Optional[Callable] = None
        self.waiting_for_input = False
        self.input_prompt = ""
        self.input_color = "green"
        
        # Message queue for thread-safe updates
        self.message_queue = queue.Queue()
        
        # User message queue (messages queued while AI is processing)
        self.user_message_queue: List[str] = []
        self._queue_lock = threading.Lock()
        
        # Create main container - pure black, no border
        self.main_container = ctk.CTkFrame(
            self,
            fg_color="#000000",
            corner_radius=0,
            border_width=0
        )
        self.main_container.pack(fill="both", expand=True, padx=0, pady=0)
        
        # Scrollable output area
        self.output_frame = ctk.CTkScrollableFrame(
            self.main_container,
            fg_color="#000000",
            corner_radius=0,
            scrollbar_button_color="#30363D",
            scrollbar_button_hover_color="#484F58"
        )
        self.output_frame.pack(fill="both", expand=True, padx=16, pady=(8, 0))
        
        # Input area container (fixed at bottom, above status bar)
        self.input_container = ctk.CTkFrame(
            self.main_container,
            fg_color="#0A0A0A",
            corner_radius=0,
            height=48
        )
        self.input_container.pack(fill="x", side="bottom", padx=0, pady=0)
        self.input_container.pack_propagate(False)
        
        # Prompt label
        self.prompt_label = ctk.CTkLabel(
            self.input_container,
            text="โ€บ",
            font=ctk.CTkFont(family="SF Mono, Monaco, Consolas, monospace", size=16, weight="bold"),
            text_color="#69DB7C",
            width=24
        )
        self.prompt_label.pack(side="left", padx=(16, 8), pady=10)
        
        # Queue indicator (shows number of queued messages)
        self.queue_indicator = ctk.CTkLabel(
            self.input_container,
            text="",
            font=ctk.CTkFont(family="SF Mono, Monaco, Consolas, monospace", size=10),
            text_color="#FFE066",
            width=40
        )
        self.queue_indicator.pack(side="right", padx=(0, 12), pady=10)
        
        # Input entry
        self.input_entry = ctk.CTkEntry(
            self.input_container,
            placeholder_text="Type a message to the AI...",
            font=ctk.CTkFont(family="SF Mono, Monaco, Consolas, monospace", size=14),
            fg_color="transparent",
            border_width=0,
            text_color="#DEE2E6",
            placeholder_text_color="#484F58"
        )
        self.input_entry.pack(fill="x", expand=True, side="left", padx=(0, 8), pady=10)
        
        # Status bar at the very bottom
        self.status_bar = ctk.CTkFrame(
            self.main_container,
            fg_color="#0D1117",
            corner_radius=0,
            height=28,
            border_width=1,
            border_color="#30363D"
        )
        self.status_bar.pack(fill="x", side="bottom", padx=0, pady=0)
        self.status_bar.pack_propagate(False)
        
        # Version label (left side)
        self.version_label = ctk.CTkLabel(
            self.status_bar,
            text=f"AGI CLI v{VERSION}",
            font=ctk.CTkFont(family="SF Mono, Monaco, Consolas, monospace", size=11),
            text_color="#6E7681"
        )
        self.version_label.pack(side="left", padx=12, pady=4)
        
        # Separator
        sep1 = ctk.CTkLabel(
            self.status_bar,
            text="โ”‚",
            font=ctk.CTkFont(size=11),
            text_color="#30363D"
        )
        sep1.pack(side="left", padx=4)
        
        # Token counters
        self.tokens_label = ctk.CTkLabel(
            self.status_bar,
            text="โ†‘ 0  โ†“ 0",
            font=ctk.CTkFont(family="SF Mono, Monaco, Consolas, monospace", size=11),
            text_color="#8B949E"
        )
        self.tokens_label.pack(side="left", padx=8, pady=4)
        
        # Register for token updates
        token_tracker.register_callback(self._update_token_display)
        
        # Settings button (right side)
        self.settings_btn = ctk.CTkButton(
            self.status_bar,
            text="โš™",
            font=ctk.CTkFont(size=14),
            fg_color="transparent",
            hover_color="#30363D",
            text_color="#8B949E",
            width=28,
            height=22,
            corner_radius=4,
            command=self._on_settings_click
        )
        self.settings_btn.pack(side="right", padx=8, pady=3)
        
        # Bind events
        self.input_entry.bind("<Return>", self._on_enter)
        self.input_entry.bind("<Up>", self._on_up)
        self.input_entry.bind("<Down>", self._on_down)
        
        # Start message processor
        self._process_messages()
        
        # Focus input
        self.input_entry.focus_set()
    
    def _on_settings_click(self):
        """Handle settings button click."""
        if self.on_settings_click:
            self.on_settings_click()
    
    def _update_token_display(self):
        """Update the token counter display."""
        sent, received = token_tracker.get_totals()
        # Format numbers with K suffix for thousands
        sent_str = f"{sent/1000:.1f}K" if sent >= 1000 else str(sent)
        recv_str = f"{received/1000:.1f}K" if received >= 1000 else str(received)
        self.after(0, lambda: self.tokens_label.configure(text=f"โ†‘ {sent_str}  โ†“ {recv_str}"))
    
    def _update_queue_indicator(self):
        """Update the queue indicator."""
        with self._queue_lock:
            count = len(self.user_message_queue)
        if count > 0:
            self.queue_indicator.configure(text=f"[{count}]")
        else:
            self.queue_indicator.configure(text="")
    
    def queue_user_message(self, message: str):
        """Add a message to the user message queue."""
        with self._queue_lock:
            self.user_message_queue.append(message)
        self._update_queue_indicator()
        self.print(f"๐Ÿ“ Queued: {message}", "yellow")
    
    def get_queued_messages(self) -> List[str]:
        """Get and clear all queued messages."""
        with self._queue_lock:
            messages = self.user_message_queue.copy()
            self.user_message_queue.clear()
        self.after(0, self._update_queue_indicator)
        return messages
    
    def has_queued_messages(self) -> bool:
        """Check if there are queued messages."""
        with self._queue_lock:
            return len(self.user_message_queue) > 0
    
    def _process_messages(self):
        """Process queued messages from other threads."""
        try:
            while True:
                msg_type, content, color = self.message_queue.get_nowait()
                if msg_type == "print":
                    self._add_output(content, color)
                elif msg_type == "clear":
                    self._clear_output()
        except queue.Empty:
            pass
        self.after(50, self._process_messages)
    
    def _add_output(self, text: str, color: str = "white"):
        """Add text to the output area."""
        hex_color = self.COLOR_MAP.get(color, self.COLOR_MAP["white"])
        
        # Check if text contains ASCII art characters
        is_ascii_art = any(c in text for c in 'โ•โ•‘โ•”โ•—โ•šโ•โ–ˆโ–€โ–„โ–Œโ–โ–‘โ–’โ–“โ• โ•ฃโ•ฆโ•ฉโ•ฌโ•ฎโ•ฏโ•ฐโ•ญโ•—โ•”')
        
        if is_ascii_art and '\n' in text:
            # For multi-line ASCII art, use tkinter Text widget for precise control
            lines = text.split('\n')
            line_count = len(lines)
            
            # Create a frame to hold the text widget
            text_frame = ctk.CTkFrame(self.output_frame, fg_color="transparent")
            text_frame.pack(fill="x", anchor="w", pady=0)
            
            # Use standard tk.Text for better control over spacing
            text_widget = tk.Text(
                text_frame,
                font=("Menlo", 11),
                fg=hex_color,
                bg="#000000",
                height=line_count,
                relief="flat",
                borderwidth=0,
                highlightthickness=0,
                spacing1=0,  # Space above first line
                spacing2=0,  # Space between lines
                spacing3=0,  # Space after last line
                padx=0,
                pady=0,
                wrap="none"
            )
            text_widget.insert("1.0", text)
            text_widget.configure(state="disabled")
            text_widget.pack(fill="x", anchor="w")
        else:
            # Handle regular text (line by line)
            lines = text.split('\n')
            
            for line in lines:
                # Check if this specific line is ASCII art
                line_is_ascii = any(c in line for c in 'โ•โ•‘โ•”โ•—โ•šโ•โ–ˆโ–€โ–„โ–Œโ–โ–‘โ–’โ–“โ• โ•ฃโ•ฆโ•ฉโ•ฌโ•ฎโ•ฏโ•ฐโ•ญ')
                
                label = ctk.CTkLabel(
                    self.output_frame,
                    text=line if line else " ",
                    font=ctk.CTkFont(
                        family="Menlo" if line_is_ascii else "SF Mono, Monaco, Consolas, monospace",
                        size=11 if line_is_ascii else 13
                    ),
                    text_color=hex_color,
                    anchor="w",
                    justify="left",
                    wraplength=0 if line_is_ascii else 900
                )
                label.pack(fill="x", anchor="w", pady=0)
        
        # Auto-scroll to bottom - update canvas first, then scroll
        self.output_frame.update_idletasks()
        self.output_frame._parent_canvas.yview_moveto(1.0)
    
    def _clear_output(self):
        """Clear all outputs."""
        for widget in self.output_frame.winfo_children():
            widget.destroy()
    
    def print(self, text: str, color: str = "white"):
        """Thread-safe print to terminal."""
        self.message_queue.put(("print", str(text), color))
    
    def clear(self):
        """Thread-safe clear terminal."""
        self.message_queue.put(("clear", None, None))
    
    def set_input_callback(self, callback: Callable, prompt: str = "", color: str = "green"):
        """Set a callback for when the user enters input."""
        self.input_callback = callback
        self.waiting_for_input = True
        self.input_prompt = prompt
        self.input_color = color
        
        # Update prompt appearance
        hex_color = self.COLOR_MAP.get(color, self.COLOR_MAP["green"])
        self.prompt_label.configure(text_color=hex_color)
        
        # Show prompt in output
        if prompt:
            self.print(prompt, color)
    
    def _on_enter(self, event):
        """Handle enter key press."""
        command = self.input_entry.get().strip()
        
        if not command:
            return "break"
        
        self.command_history.append(command)
        self.history_index = len(self.command_history)
        self.input_entry.delete(0, "end")
        
        # If waiting for input and have a callback, process normally
        if self.waiting_for_input and self.input_callback:
            
            self.waiting_for_input = False
            callback = self.input_callback
            self.input_callback = None
            
            # Show user input
            self.print(f"โ€บ {command}", self.input_color)
            
            # Reset status after processing
            def run_callback():
                try:
                    callback(command)
                finally:
                    pass  # Status updates removed with header
            
            # Run in thread to prevent blocking
            threading.Thread(target=run_callback, daemon=True).start()
        else:
            # AI is processing, queue the message for later
            self.queue_user_message(command)
        
        return "break"
    
    def _on_up(self, event):
        """Navigate command history up."""
        if self.command_history and self.history_index > 0:
            self.history_index -= 1
            self.input_entry.delete(0, "end")
            self.input_entry.insert(0, self.command_history[self.history_index])
        return "break"
    
    def _on_down(self, event):
        """Navigate command history down."""
        if self.command_history and self.history_index < len(self.command_history) - 1:
            self.history_index += 1
            self.input_entry.delete(0, "end")
            self.input_entry.insert(0, self.command_history[self.history_index])
        elif self.history_index >= len(self.command_history) - 1:
            self.history_index = len(self.command_history)
            self.input_entry.delete(0, "end")
        return "break"
    
    def set_processing(self, is_processing: bool):
        """Update the status indicator for processing state."""
        # Header removed, status no longer displayed
        pass


class AGICliApp(ctk.CTk):
    """Main AGI CLI Application Window."""
    
    def __init__(self):
        super().__init__()
        
        # Configure appearance
        ctk.set_appearance_mode("dark")
        ctk.set_default_color_theme("dark-blue")
        
        # Window setup
        self.title("AGI CLI")
        self.geometry("1100x700")
        self.minsize(800, 500)
        
        # Set dock icon
        try:
            icon_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logo.png")
            if os.path.exists(icon_path):
                # For macOS dock icon
                self.iconphoto(True, tk.PhotoImage(file=icon_path))
        except Exception:
            pass  # Ignore icon errors
        
        # Set pure black background
        self.configure(fg_color="#000000")
        
        # Settings window reference
        self.settings_window = None
        
        # Create custom terminal
        self.terminal = CustomTerminal(self, on_settings_click=self._open_settings)
        self.terminal.pack(fill="both", expand=True, padx=0, pady=0)
        
        # Make terminal globally accessible for printplus module
        import agicli.printplus as pp
        pp._terminal = self.terminal
        
        # Make token tracker globally accessible
        pp._token_tracker = token_tracker
        
        # Start the CLI in a separate thread
        self.after(500, self._start_cli)
    
    def _open_settings(self):
        """Open the settings window."""
        if self.settings_window is None or not self.settings_window.winfo_exists():
            self.settings_window = SettingsWindow(self)
        else:
            self.settings_window.focus()
    
    def _start_cli(self):
        """Start the CLI initialization."""
        def run_cli():
            try:
                from agicli.welcome import initialize
                initialize()
            except Exception as e:
                self.terminal.print(f"Error: {e}", "red")
        
        threading.Thread(target=run_cli, daemon=True).start()


def main():
    """Main entry point for the AGI CLI application."""
    app = AGICliApp()
    app.mainloop()


if __name__ == "__main__":
    main()