File size: 42,681 Bytes
be6ceb5
6435a84
be6ceb5
6435a84
 
be6ceb5
6435a84
 
68f4bf9
6435a84
68f4bf9
 
 
 
be6ceb5
 
68f4bf9
 
 
be6ceb5
68f4bf9
 
be6ceb5
68f4bf9
6435a84
be6ceb5
 
6435a84
6e507b9
68f4bf9
 
 
 
 
 
 
 
 
 
 
 
be6ceb5
68f4bf9
 
 
 
6e507b9
 
68f4bf9
6e507b9
68f4bf9
6e507b9
68f4bf9
6e507b9
 
 
 
 
68f4bf9
6e507b9
 
 
 
 
 
 
 
 
68f4bf9
6e507b9
be6ceb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6435a84
be6ceb5
 
 
 
 
 
 
 
6435a84
 
be6ceb5
 
 
 
 
6435a84
68f4bf9
be6ceb5
 
 
68f4bf9
be6ceb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68f4bf9
 
 
be6ceb5
 
 
 
 
 
 
 
 
 
68f4bf9
 
 
be6ceb5
6435a84
be6ceb5
 
 
 
 
 
 
 
 
 
68f4bf9
be6ceb5
68f4bf9
be6ceb5
 
68f4bf9
be6ceb5
 
 
6e507b9
68f4bf9
 
 
6e507b9
be6ceb5
 
6e507b9
6435a84
 
 
 
 
 
be6ceb5
6435a84
be6ceb5
 
 
6e507b9
3fa0b25
 
be6ceb5
3fa0b25
 
be6ceb5
 
 
6435a84
 
 
 
 
 
 
be6ceb5
6435a84
 
 
be6ceb5
68f4bf9
6435a84
be6ceb5
68f4bf9
 
6435a84
 
68f4bf9
6435a84
6e507b9
68f4bf9
 
 
6e507b9
be6ceb5
 
 
6e507b9
be6ceb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
be6ceb5
 
 
 
 
 
 
 
6e507b9
be6ceb5
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
be6ceb5
 
 
 
 
 
 
 
 
 
68f4bf9
 
 
be6ceb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
be6ceb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68f4bf9
6e507b9
be6ceb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
be6ceb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
be6ceb5
 
 
 
 
 
 
 
 
 
 
 
 
 
68f4bf9
be6ceb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
be6ceb5
 
 
 
 
 
 
 
6e507b9
be6ceb5
 
 
 
 
 
 
 
6e507b9
be6ceb5
 
 
6e507b9
be6ceb5
 
 
 
 
68f4bf9
 
6435a84
be6ceb5
68f4bf9
be6ceb5
 
 
 
 
 
 
 
 
 
 
 
 
 
6435a84
68f4bf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be6ceb5
 
 
6e507b9
be6ceb5
 
 
 
6e507b9
be6ceb5
 
 
 
 
 
 
 
 
6e507b9
68f4bf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
68f4bf9
6e507b9
6435a84
68f4bf9
6435a84
6e507b9
6435a84
6e507b9
68f4bf9
 
6e507b9
68f4bf9
6e507b9
6435a84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
 
6435a84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
6435a84
 
 
 
 
6e507b9
6435a84
 
 
 
6e507b9
 
 
 
 
6435a84
 
6e507b9
6435a84
 
6e507b9
 
6435a84
 
6e507b9
6435a84
 
6e507b9
6435a84
 
 
 
 
6e507b9
 
6435a84
 
 
6e507b9
 
6435a84
 
 
6e507b9
 
6435a84
 
6e507b9
6435a84
 
 
6e507b9
6435a84
 
6e507b9
 
 
6435a84
 
 
 
6e507b9
68f4bf9
 
 
 
6435a84
 
 
6e507b9
 
6435a84
 
 
 
 
 
6e507b9
6435a84
6e507b9
 
 
6435a84
68f4bf9
 
 
 
 
 
 
 
6e507b9
68f4bf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
 
68f4bf9
 
6e507b9
 
 
 
 
 
 
 
 
 
 
 
 
68f4bf9
6435a84
 
 
6e507b9
6435a84
6e507b9
68f4bf9
6435a84
68f4bf9
6435a84
6e507b9
 
6435a84
 
6e507b9
 
 
6435a84
 
6e507b9
68f4bf9
6435a84
 
 
 
 
68f4bf9
6435a84
68f4bf9
6e507b9
6435a84
 
6e507b9
6435a84
68f4bf9
6435a84
 
 
 
6e507b9
6435a84
68f4bf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
 
3fa0b25
68f4bf9
3fa0b25
 
68f4bf9
 
 
 
3fa0b25
6e507b9
 
68f4bf9
3fa0b25
 
 
 
 
68f4bf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
 
68f4bf9
 
 
 
 
 
 
 
 
 
 
 
 
 
6e507b9
 
 
 
3fa0b25
6435a84
3fa0b25
6e507b9
 
 
 
 
 
 
 
 
6435a84
68f4bf9
 
 
6e507b9
 
 
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
# File: enhanced_gradio_interface.py
import asyncio
from collections import defaultdict
import json
import os
import re
import time
import uuid
from typing import List, Dict, Any, Optional
from dataclasses import dataclass
from threading import Lock
import threading
import json
import os
import queue
import traceback
import uuid
from typing import Coroutine, Dict, List, Any, Optional, Callable
from dataclasses import dataclass
from queue import Queue, Empty
from threading import Lock, Event, Thread
import threading
from concurrent.futures import ThreadPoolExecutor
import time
import gradio as gr
from openai import AsyncOpenAI, OpenAI
import pyttsx3
from rich.console import Console

BASE_URL="http://localhost:1234/v1"
BASE_API_KEY="not-needed"
BASE_CLIENT = AsyncOpenAI(
    base_url=BASE_URL,
    api_key=BASE_API_KEY
) # Global state for client
BASEMODEL_ID = "leroydyer/qwen/qwen3-0.6b-q4_k_m.gguf"  # Global state for selected model ID
CLIENT =OpenAI(
    base_url=BASE_URL,
    api_key=BASE_API_KEY
) # Global state for client
# --- Global Variables (if needed) ---
console = Console()
# --- Configuration ---
LOCAL_BASE_URL = "http://localhost:1234/v1"
LOCAL_API_KEY = "not-needed"
# HuggingFace Spaces configuration
HF_INFERENCE_URL = "https://api-inference.huggingface.co/models/"
HF_API_KEY = os.getenv("HF_API_KEY", "")
# Available model options
MODEL_OPTIONS = {
    "Local LM Studio": LOCAL_BASE_URL,
    "Codellama 7B": "codellama/CodeLlama-7b-hf",
    "Mistral 7B": "mistralai/Mistral-7B-v0.1", 
    "Llama 2 7B": "meta-llama/Llama-2-7b-chat-hf",
    "Falcon 7B": "tiiuae/falcon-7b-instruct"
}
DEFAULT_TEMPERATURE = 0.7
DEFAULT_MAX_TOKENS = 5000
console = Console()

# --- Canvas Artifact Support ---
@dataclass
class CanvasArtifact:
    id: str
    type: str  # 'code', 'diagram', 'text', 'image'
    content: str
    title: str
    timestamp: float
    metadata: Dict[str, Any]

@dataclass
class LLMMessage:
    role: str
    content: str
    message_id: str = None
    conversation_id: str = None
    timestamp: float = None
    metadata: Dict[str, Any] = None
    def __post_init__(self):
        if self.message_id is None:
            self.message_id = str(uuid.uuid4())
        if self.timestamp is None:
            self.timestamp = time.time()
        if self.metadata is None:
            self.metadata = {}

@dataclass
class LLMRequest:
    message: LLMMessage
    response_event: str = None
    callback: Callable = None
    def __post_init__(self):
        if self.response_event is None:
            self.response_event = f"llm_response_{self.message.message_id}"

@dataclass
class LLMResponse:
    message: LLMMessage
    request_id: str
    success: bool = True
    error: str = None

# --- Event Manager (copied from your original code or imported) ---
class EventManager:
    def __init__(self):
        self._handlers = defaultdict(list)
        self._lock = threading.Lock()
    def register(self, event: str, handler: Callable):
        with self._lock:
            self._handlers[event].append(handler)
    def unregister(self, event: str, handler: Callable):
        with self._lock:
            if event in self._handlers and handler in self._handlers[event]:
                self._handlers[event].remove(handler)
    def raise_event(self, event: str, data: Any):
        with self._lock:
            handlers = self._handlers[event][:]
        for handler in handlers:
            try:
                handler(data)
            except Exception as e:
                console.log(f"Error in event handler for {event}: {e}", style="bold red")

EVENT_MANAGER = EventManager()
def RegisterEvent(event: str, handler: Callable):
    EVENT_MANAGER.register(event, handler)

def RaiseEvent(event: str, data: Any):
    EVENT_MANAGER.raise_event(event, data)

def UnregisterEvent(event: str, handler: Callable):
    EVENT_MANAGER.unregister(event, handler)

class LLMAgent:
    """Main Agent Driver ! 
    Agent For Multiple messages at once , 
    has a message queing service as well as agenerator method for easy intergration with console     
    applications as well as ui !"""
    def __init__(
        self,
        model_id: str = BASEMODEL_ID,
        system_prompt: str = None,
        max_queue_size: int = 1000,
        max_retries: int = 3,
        timeout: int = 30000,
        max_tokens: int = 5000,
        temperature: float = 0.3,
        base_url: str = "http://localhost:1234/v1",
        api_key: str = "not-needed",
        generate_fn: Callable[[List[Dict[str, str]]], Coroutine[Any, Any, str]] = None
    ):
        self.model_id = model_id
        self.system_prompt = system_prompt or "You are a helpful AI assistant."
        self.request_queue = Queue(maxsize=max_queue_size)
        self.max_retries = max_retries
        self.timeout = timeout
        self.is_running = False
        self._stop_event = Event()
        self.processing_thread = None
        # Conversation tracking
        self.conversations: Dict[str, List[LLMMessage]] = {}
        self.max_history_length = 20
        self._generate = generate_fn or self._default_generate
        self.api_key = api_key
        self.base_url = base_url        
        self.max_tokens = max_tokens
        self.temperature = temperature
        self.async_client = self.CreateClient(base_url, api_key)
        # Active requests waiting for responses
        self.pending_requests: Dict[str, LLMRequest] = {}
        self.pending_requests_lock = Lock()

        # Canvas Artifacts - NEW
        self.canvas_artifacts: Dict[str, List[CanvasArtifact]] = {}
        self.canvas_lock = Lock()

        # Register internal event handlers
        self._register_event_handlers()

        # Speech synthesis
        try:
            self.tts_engine = pyttsx3.init()
            self.setup_tts()
            self.speech_enabled = True
        except Exception as e:
            console.log(f"[yellow]TTS not available: {e}[/yellow]")
            self.speech_enabled = False
        console.log("[bold green]πŸš€ Enhanced LLM Agent Initialized[/bold green]")
        # Start the processing thread immediately
        self.start()

    def setup_tts(self):
        """Configure text-to-speech engine"""
        if hasattr(self, 'tts_engine'):
            voices = self.tts_engine.getProperty('voices')
            if voices:
                self.tts_engine.setProperty('voice', voices[0].id)
            self.tts_engine.setProperty('rate', 150)
            self.tts_engine.setProperty('volume', 0.8)

    def speak(self, text: str):
        """Convert text to speech in a non-blocking way"""
        if not hasattr(self, 'speech_enabled') or not self.speech_enabled:
            return
        def _speak():
            try:
                # Clean text for speech (remove markdown, code blocks)
                clean_text = re.sub(r'```.*?```', '', text, flags=re.DOTALL)
                clean_text = re.sub(r'`.*?`', '', clean_text)
                clean_text = clean_text.strip()
                if clean_text:
                    self.tts_engine.say(clean_text) 
                    self.tts_engine.runAndWait()
                else:
                    self.tts_engine.say(text)  
                    self.tts_engine.runAndWait()                    
            except Exception as e:
                console.log(f"[red]TTS Error: {e}[/red]")
        thread = threading.Thread(target=_speak, daemon=True)
        thread.start()

    async def _default_generate(self, messages: List[Dict[str, str]]) -> str:
        """Default generate function if none provided"""
        return await self.openai_generate(messages)

    def _register_event_handlers(self):
        """Register internal event handlers for response routing"""
        RegisterEvent("llm_internal_response", self._handle_internal_response)

    def _handle_internal_response(self, response: LLMResponse):
        """Route responses to the appropriate request handlers"""
        console.log(f"[bold cyan]Handling internal response for: {response.request_id}[/bold cyan]")
        request = None
        with self.pending_requests_lock:
            if response.request_id in self.pending_requests:
                request = self.pending_requests[response.request_id]
                del self.pending_requests[response.request_id]
                console.log(f"Found pending request for: {response.request_id}")
            else:
                console.log(f"No pending request found for: {response.request_id}", style="yellow")
                return
        # Raise the specific response event
        if request.response_event:
            console.log(f"[bold green]Raising event: {request.response_event}[/bold green]")
            RaiseEvent(request.response_event, response)
        # Call callback if provided
        if request.callback:
            try:
                console.log(f"[bold yellow]Calling callback for: {response.request_id}[/bold yellow]")
                request.callback(response)
            except Exception as e:
                console.log(f"Error in callback: {e}", style="bold red")

    def _add_to_conversation_history(self, conversation_id: str, message: LLMMessage):
        """Add message to conversation history"""
        if conversation_id not in self.conversations:
            self.conversations[conversation_id] = []
        self.conversations[conversation_id].append(message)
        # Trim history if too long
        if len(self.conversations[conversation_id]) > self.max_history_length * 2:
            self.conversations[conversation_id] = self.conversations[conversation_id][-(self.max_history_length * 2):]

    def _build_messages_from_conversation(self, conversation_id: str, new_message: LLMMessage) -> List[Dict[str, str]]:
        """Build message list from conversation history"""
        messages = []
        # Add system prompt
        if self.system_prompt:
            messages.append({"role": "system", "content": self.system_prompt})
        # Add conversation history
        if conversation_id in self.conversations:
            for msg in self.conversations[conversation_id][-self.max_history_length:]:
                messages.append({"role": msg.role, "content": msg.content})
        # Add the new message
        messages.append({"role": new_message.role, "content": new_message.content})
        return messages

    def _process_llm_request(self, request: LLMRequest):
        """Process a single LLM request"""
        console.log(f"[bold green]Processing LLM request: {request.message.message_id}[/bold green]")
        try:
            # Build messages for LLM
            messages = self._build_messages_from_conversation(
                request.message.conversation_id or "default",
                request.message
            )
            console.log(f"Calling LLM with {len(messages)} messages")
            # Call LLM - Use sync call for thread compatibility
            response_content = self._call_llm_sync(messages)
            console.log(f"[bold green]LLM response received: {response_content}...[/bold green]")
            # Create response message
            response_message = LLMMessage(
                role="assistant",
                content=response_content,
                conversation_id=request.message.conversation_id,
                metadata={"request_id": request.message.message_id}
            )
            # Update conversation history
            self._add_to_conversation_history(
                request.message.conversation_id or "default",
                request.message
            )
            self._add_to_conversation_history(
                request.message.conversation_id or "default",
                response_message
            )
            # Create and send response
            response = LLMResponse(
                message=response_message,
                request_id=request.message.message_id,
                success=True
            )
            console.log(f"[bold blue]Sending internal response for: {request.message.message_id}[/bold blue]")
            RaiseEvent("llm_internal_response", response)
        except Exception as e:
            console.log(f"[bold red]Error processing LLM request: {e}[/bold red]")
            traceback.print_exc()
            # Create error response
            error_response = LLMResponse(
                message=LLMMessage(
                    role="system",
                    content=f"Error: {str(e)}",
                    conversation_id=request.message.conversation_id
                ),
                request_id=request.message.message_id,
                success=False,
                error=str(e)
            )
            RaiseEvent("llm_internal_response", error_response)

    def _call_llm_sync(self, messages: List[Dict[str, str]]) -> str:
        """Sync call to the LLM with retry logic"""
        console.log(f"Making LLM call to {self.model_id}")
        for attempt in range(self.max_retries):
            try:
                response = CLIENT.chat.completions.create(
                    model=self.model_id,
                    messages=messages,
                    temperature=self.temperature,
                    max_tokens=self.max_tokens
                )
                content = response.choices[0].message.content
                console.log(f"LLM call successful, response length: {len(content)}")
                return content
            except Exception as e:
                console.log(f"LLM call attempt {attempt + 1} failed: {e}")
                if attempt == self.max_retries - 1:
                    raise e
                 # Wait before retry

    def _process_queue(self):
        """Main queue processing loop"""
        console.log("[bold cyan]LLM Agent queue processor started[/bold cyan]")
        while not self._stop_event.is_set():
            try:
                request = self.request_queue.get(timeout=1.0)
                if request:
                    console.log(f"Got request from queue: {request.message.message_id}")
                    self._process_llm_request(request)
                    self.request_queue.task_done()
            except Empty:
                continue
            except Exception as e:
                console.log(f"Error in queue processing: {e}", style="bold red")
                traceback.print_exc()
        console.log("[bold cyan]LLM Agent queue processor stopped[/bold cyan]")

    def send_message(
        self,
        content: str,
        role: str = "user",
        conversation_id: str = None,
        response_event: str = None,
        callback: Callable = None,
        metadata: Dict = None
    ) -> str:
        """Send a message to the LLM and get response via events"""
        if not self.is_running:
            raise RuntimeError("LLM Agent is not running. Call start() first.")
        # Create message
        message = LLMMessage(
            role=role,
            content=content,
            conversation_id=conversation_id,
            metadata=metadata or {}
        )
        # Create request
        request = LLMRequest(
            message=message,
            response_event=response_event,
            callback=callback
        )
        # Store in pending requests BEFORE adding to queue
        with self.pending_requests_lock:
            self.pending_requests[message.message_id] = request
            console.log(f"Added to pending requests: {message.message_id}")
        # Add to queue
        try:
            self.request_queue.put(request, timeout=5.0)
            console.log(f"[bold magenta]Message queued: {message.message_id}, Content: {content[:50]}...[/bold magenta]")
            return message.message_id
        except queue.Full:
            console.log(f"[bold red]Queue full, cannot send message[/bold red]")
            with self.pending_requests_lock:
                if message.message_id in self.pending_requests:
                    del self.pending_requests[message.message_id]
            raise RuntimeError("LLM Agent queue is full")

    async def chat(self, messages: List[Dict[str, str]]) -> str:
        """
        Async chat method that sends message via queue and returns response string.
        This is the main method you should use.
        """
        # Create future for the response
        loop = asyncio.get_event_loop()
        response_future = loop.create_future()
        def chat_callback(response: LLMResponse):
            """Callback when LLM responds - thread-safe"""
            console.log(f"[bold yellow]βœ“ CHAT CALLBACK TRIGGERED![/bold yellow]")
            if not response_future.done():
                if response.success:
                    content = response.message.content
                    console.log(f"Callback received content: {content}...")
                    # Schedule setting the future result on the main event loop
                    loop.call_soon_threadsafe(response_future.set_result, content)
                else:
                    console.log(f"Error in response: {response.error}")
                    error_msg = f"❌ Error: {response.error}"
                    loop.call_soon_threadsafe(response_future.set_result, error_msg)
            else:
                console.log(f"[bold red]Future already done, ignoring callback[/bold red]")
        console.log(f"Sending message to LLM agent...")
        # Extract the actual message content from the messages list
        user_message = ""
        for msg in messages:
            if msg.get("role") == "user":
                user_message = msg.get("content", "")
                break
        if not user_message.strip():
            return ""
        # Send message with callback using the queue system
        try:
            message_id = self.send_message(
                content=user_message,
                conversation_id="default",
                callback=chat_callback
            )
            console.log(f"Message sent with ID: {message_id}, waiting for response...")
            # Wait for the response and return it
            try:
                response = await asyncio.wait_for(response_future, timeout=self.timeout)
                console.log(f"[bold green]βœ“ Chat complete! Response length: {len(response)}[/bold green]")
                return response
            except asyncio.TimeoutError:
                console.log("[bold red]Response timeout[/bold red]")
                # Clean up the pending request
                with self.pending_requests_lock:
                    if message_id in self.pending_requests:
                        del self.pending_requests[message_id]
                return "❌ Response timeout - check if LLM server is running"
        except Exception as e:
            console.log(f"[bold red]Error sending message: {e}[/bold red]")
            traceback.print_exc()
            return f"❌ Error sending message: {e}"

    def start(self):
        """Start the LLM agent"""
        if not self.is_running:
            self.is_running = True
            self._stop_event.clear()
            self.processing_thread = Thread(target=self._process_queue, daemon=True)
            self.processing_thread.start()
            console.log("[bold green]LLM Agent started[/bold green]")

    def stop(self):
        """Stop the LLM agent"""
        console.log("Stopping LLM Agent...")
        self._stop_event.set()
        if self.processing_thread and self.processing_thread.is_alive():
            self.processing_thread.join(timeout=10)
        self.is_running = False
        console.log("LLM Agent stopped")

    def get_conversation_history(self, conversation_id: str = "default") -> List[LLMMessage]:
        """Get conversation history"""
        return self.conversations.get(conversation_id, [])[:]

    def clear_conversation(self, conversation_id: str = "default"):
        """Clear conversation history"""
        if conversation_id in self.conversations:
            del self.conversations[conversation_id]

    async def _chat(self, messages: List[Dict[str, str]]) -> str:
        return await self._generate(messages)

    @staticmethod
    async def openai_generate(messages: List[Dict[str, str]], max_tokens: int = 8096, temperature: float = 0.4, model: str = BASEMODEL_ID,tools=None) -> str:
        """Static method for generating responses using OpenAI API"""
        try:
            resp = await BASE_CLIENT.chat.completions.create(
                model=model,
                messages=messages,
                temperature=temperature,
                max_tokens=max_tokens,
                tools=tools
            )
            response_text = resp.choices[0].message.content or ""
            return response_text
        except Exception as e:
            console.log(f"[bold red]Error in openai_generate: {e}[/bold red]")
            return f"[LLM_Agent Error - openai_generate: {str(e)}]"

    async def _call_(self, messages: List[Dict[str, str]]) -> str:
        """Internal call method using instance client"""
        try:
            resp = await self.async_client.chat.completions.create(
                model=self.model_id,
                messages=messages,
                temperature=self.temperature,
                max_tokens=self.max_tokens
            )
            response_text = resp.choices[0].message.content or ""
            return response_text
        except Exception as e:
            console.log(f"[bold red]Error in _call_: {e}[/bold red]")
            return f"[LLM_Agent Error - _call_: {str(e)}]"  

    @staticmethod              
    def CreateClient(base_url: str, api_key: str) -> AsyncOpenAI:
        '''Create async OpenAI Client required for multi tasking'''
        return AsyncOpenAI(
            base_url=base_url,
            api_key=api_key
        ) 

    @staticmethod
    async def fetch_available_models(base_url: str, api_key: str) -> List[str]:
        """Fetches available models from the OpenAI API."""
        try:
            async_client = AsyncOpenAI(base_url=base_url, api_key=api_key)
            models = await async_client.models.list()
            model_choices = [model.id for model in models.data]
            return model_choices
        except Exception as e:
            console.log(f"[bold red]LLM_Agent Error fetching models: {e}[/bold red]")
            return ["LLM_Agent Error fetching models"]   

    def get_models(self) -> List[str]:             
        """Get available models using instance credentials"""
        return asyncio.run(self.fetch_available_models(self.base_url, self.api_key))

    def get_queue_size(self) -> int:
        """Get current queue size"""
        return self.request_queue.qsize()

    def get_pending_requests_count(self) -> int:
        """Get number of pending requests"""
        with self.pending_requests_lock:
            return len(self.pending_requests)

    def get_status(self) -> Dict[str, Any]:
        """Get agent status information"""
        return {
            "is_running": self.is_running,
            "queue_size": self.get_queue_size(),
            "pending_requests": self.get_pending_requests_count(),
            "conversations_count": len(self.conversations),
            "model": self.model_id
        }

    # --- ADDED CANVAS FUNCTIONALITY ---
    def add_canvas_artifact(self, conversation_id: str, artifact_type: str, content: str, title: str = ""):
        """Add an artifact to the canvas for a specific conversation."""
        conv_id = conversation_id or "default"
        with self.canvas_lock:
            if conv_id not in self.canvas_artifacts:
                self.canvas_artifacts[conv_id] = []
            artifact = CanvasArtifact(
                id=str(uuid.uuid4()),
                type=artifact_type,
                content=content,
                title=title,
                timestamp=time.time(),
                metadata={}
            )
            self.canvas_artifacts[conv_id].append(artifact)
        console.log(f"[green]Added {artifact_type} artifact to canvas '{conv_id}'[/green]")

    def get_canvas_summary(self, conversation_id: str) -> List[Dict]:
        """Get a summary of artifacts on the canvas for JSON display."""
        conv_id = conversation_id or "default"
        with self.canvas_lock:
            artifacts = self.canvas_artifacts.get(conv_id, [])
            # Convert artifacts to dictionaries for JSON serialization
            return [
                {
                    "id": art.id,
                    "type": art.type,
                    "title": art.title,
                    "timestamp": art.timestamp,
                    "content_preview": art.content[:100] + "..." if len(art.content) > 100 else art.content
                }
                for art in artifacts
            ]

    def clear_canvas(self, conversation_id: str):
        """Clear all artifacts from the canvas for a specific conversation."""
        conv_id = conversation_id or "default"
        with self.canvas_lock:
            if conv_id in self.canvas_artifacts:
                self.canvas_artifacts[conv_id].clear()
        console.log(f"[yellow]Cleared canvas artifacts for '{conv_id}'[/yellow]")

    async def chat_with_canvas(self, message: str, conversation_id: str, include_canvas: bool = False):
        """Chat method that can optionally include canvas context."""
        messages = [{"role": "user", "content": message}]
        
        if include_canvas:
            artifacts = self.get_canvas_summary(conversation_id)
            if artifacts:
                canvas_context = "Current Canvas Context:\\n" + "\\n".join([
                    f"- [{art['type'].upper()}] {art['title'] or 'Untitled'}: {art['content_preview']}"
                    for art in artifacts
                ])
                messages.insert(0, {"role": "system", "content": canvas_context})

        return await self.chat(messages)


console = Console()

# --- LCARS Styled Gradio Interface ---
class LcarsInterface:
    def __init__(self):
        # Start with HuggingFace by default for Spaces
        self.use_huggingface = True
        self.agent = LLMAgent(generate_fn=LLMAgent.openai_generate)
        self.current_conversation = "default"

    def create_interface(self):
        """Create the full LCARS-styled interface"""
        lcars_css = """
        :root {
            --lcars-orange: #FF9900;
            --lcars-red: #FF0033;
            --lcars-blue: #6699FF;
            --lcars-purple: #CC99FF;
            --lcars-pale-blue: #99CCFF;
            --lcars-black: #000000;
            --lcars-dark-blue: #3366CC;
            --lcars-gray: #424242;
            --lcars-yellow: #FFFF66;
        }
        body {
            background: var(--lcars-black);
            color: var(--lcars-orange);
            font-family: 'Antonio', 'LCD', 'Courier New', monospace;
            margin: 0;
            padding: 0;
        }
        .gradio-container {
            background: var(--lcars-black) !important;
            min-height: 100vh;
        }
        .lcars-container {
            background: var(--lcars-black);
            border: 4px solid var(--lcars-orange);
            border-radius: 0 30px 0 0;
            min-height: 100vh;
            padding: 20px;
        }
        .lcars-header {
            background: linear-gradient(90deg, var(--lcars-red), var(--lcars-orange));
            padding: 20px 40px;
            border-radius: 0 60px 0 0;
            margin: -20px -20px 20px -20px;
            border-bottom: 6px solid var(--lcars-blue);
        }
        .lcars-title {
            font-size: 2.5em;
            font-weight: bold;
            color: var(--lcars-black);
            margin: 0;
        }
        .lcars-subtitle {
            font-size: 1.2em;
            color: var(--lcars-black);
            margin: 10px 0 0 0;
        }
        .lcars-panel {
            background: rgba(66, 66, 66, 0.9);
            border: 2px solid var(--lcars-orange);
            border-radius: 0 20px 0 20px;
            padding: 15px;
            margin-bottom: 15px;
        }
        .lcars-button {
            background: var(--lcars-orange);
            color: var(--lcars-black) !important;
            border: none !important;
            border-radius: 0 15px 0 15px !important;
            padding: 10px 20px !important;
            font-family: inherit !important;
            font-weight: bold !important;
            margin: 5px !important;
        }
        .lcars-button:hover {
            background: var(--lcars-red) !important;
        }
        .lcars-input {
            background: var(--lcars-black) !important;
            color: var(--lcars-orange) !important;
            border: 2px solid var(--lcars-blue) !important;
            border-radius: 0 10px 0 10px !important;
            padding: 10px !important;
        }
        .lcars-chatbot {
            background: var(--lcars-black) !important;
            border: 2px solid var(--lcars-purple) !important;
            border-radius: 0 15px 0 15px !important;
        }
        .status-indicator {
            display: inline-block;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: var(--lcars-red);
            margin-right: 8px;
        }
        .status-online {
            background: var(--lcars-blue);
            animation: pulse 2s infinite;
        }
        @keyframes pulse {
            0% { opacity: 1; }
            50% { opacity: 0.5; }
            100% { opacity: 1; }
        }
        """
        with gr.Blocks(css=lcars_css, theme=gr.themes.Default(), title="LCARS Terminal") as interface:
            with gr.Column(elem_classes="lcars-container"):
                # Header
                with gr.Sidebar():
                    gr.LoginButton() 


                with gr.Row(elem_classes="lcars-header"):
                    gr.Markdown("""
                    <div style="text-align: center; width: 100%;">
                        <div class="lcars-title">πŸš€ LCARS TERMINAL</div>
                        <div class="lcars-subtitle">STARFLEET AI DEVELOPMENT CONSOLE</div>
                        <div style="margin-top: 10px;">
                            <span class="status-indicator status-online"></span>
                            <span style="color: var(--lcars-black); font-weight: bold;">SYSTEM ONLINE</span>
                        </div>
                    </div>
                    """)
                # Main Content
                with gr.Row():
                    # Left Sidebar
                    with gr.Column(scale=1):
                        # Configuration Panel
                        with gr.Column(elem_classes="lcars-panel"):
                            # Connection Type Selector
                            with gr.Row(elem_classes="lcars-panel"):
                            
                                connection_type = gr.Radio(label = "### 🌐 CONNECTION TYPE",
                                    choices=["HuggingFace Inference", "Local LM Studio"],
                                    value="HuggingFace Inference",
                                    elem_classes="lcars-input"
                                )                            
                            gr.Markdown("### πŸ”§ CONFIGURATION")
                            # Connection-specific settings
                            with gr.Row(visible=False) as local_settings:
                                base_url = gr.Textbox(
                                    value=LOCAL_BASE_URL,
                                    label="LM Studio URL",
                                    elem_classes="lcars-input"
                                )
                                api_key = gr.Textbox(
                                    value=LOCAL_API_KEY,
                                    label="API Key",
                                    type="password",
                                    elem_classes="lcars-input"
                                )
                            with gr.Row(visible=True) as hf_settings:
                                hf_api_key = gr.Textbox(
                                    value=HF_API_KEY,
                                    label="HuggingFace API Key",
                                    type="password",
                                    elem_classes="lcars-input",
                                    placeholder="Get from https://huggingface.co/settings/tokens"
                                )
                            with gr.Row():
                                model_dropdown = gr.Dropdown(
                                    choices=list(MODEL_OPTIONS.keys())[1:],
                                    value=list(MODEL_OPTIONS.keys())[1],
                                    label="AI Model",
                                    elem_classes="lcars-input"
                                )
                                fetch_models_btn = gr.Button("πŸ“‘ Fetch Models", elem_classes="lcars-button")
                            with gr.Row():
                                temperature = gr.Slider(0.0, 2.0, value=0.7, label="Temperature")
                                max_tokens = gr.Slider(128, 8192, value=2000, step=128, label="Max Tokens")
                            with gr.Row():
                                update_config_btn = gr.Button("πŸ’Ύ Apply Config", elem_classes="lcars-button")
                                speech_toggle = gr.Checkbox(value=True, label="πŸ”Š Speech Output")
                        # Canvas Artifacts
                        with gr.Column(elem_classes="lcars-panel"):
                            gr.Markdown("### 🎨 CANVAS ARTIFACTS")
                            artifact_display = gr.JSON(label="")
                            with gr.Row():
                                refresh_artifacts_btn = gr.Button("πŸ”„ Refresh", elem_classes="lcars-button")
                                clear_canvas_btn = gr.Button("πŸ—‘οΈ Clear Canvas", elem_classes="lcars-button")
                    # Main Content Area
                    with gr.Column(scale=2):
                        # Code Canvas
                        with gr.Accordion("πŸ’» COLLABORATIVE CODE CANVAS", open=False):
                            code_editor = gr.Code(
                                value="# Welcome to LCARS Collaborative Canvas\\nprint('Hello, Starfleet!')",
                                language="python",
                                lines=15,
                                label=""
                            )
                            with gr.Row():
                                load_to_chat_btn = gr.Button("πŸ’¬ Discuss Code", elem_classes="lcars-button")
                                analyze_btn = gr.Button("πŸ” Analyze", elem_classes="lcars-button")
                                optimize_btn = gr.Button("⚑ Optimize", elem_classes="lcars-button")
                        # Chat Interface
                        with gr.Column(elem_classes="lcars-panel"):
                            gr.Markdown("### πŸ’¬ MISSION LOG")
                            chatbot = gr.Chatbot(label="", height=300)
                            with gr.Row():
                                message_input = gr.Textbox(
                                    placeholder="Enter your command or query...",
                                    show_label=False,
                                    lines=2,
                                    scale=4
                                )
                                send_btn = gr.Button("πŸš€ SEND", elem_classes="lcars-button", scale=1)
                        # Status
                        with gr.Row():
                            status_display = gr.Textbox(
                                value="LCARS terminal operational. Awaiting commands.",
                                label="Status",
                                max_lines=2
                            )
                            with gr.Column(scale=0):
                                clear_chat_btn = gr.Button("πŸ—‘οΈ Clear Chat", elem_classes="lcars-button")
                                new_session_btn = gr.Button("πŸ†• New Session", elem_classes="lcars-button")

            # === EVENT HANDLERS ===
            def switch_connection(connection_type):
                if connection_type == "Local LM Studio":
                    return [
                        gr.update(visible=True),
                        gr.update(visible=False),
                        gr.update(choices=list(MODEL_OPTIONS.keys())[1:], value=list(MODEL_OPTIONS.keys())[1])
                    ]
                else:
                    return [
                        gr.update(visible=False),
                        gr.update(visible=True),
                        gr.update(choices=list(MODEL_OPTIONS.keys())[1:], value=list(MODEL_OPTIONS.keys())[1])
                    ]

            async def fetch_models_updated(connection_type, base_url_val, api_key_val, hf_api_key_val):
                # Fixed: Removed the 'use_huggingface' parameter
                if connection_type == "Local LM Studio":
                    models = await LLMAgent.fetch_available_models(
                        base_url_val, api_key_val
                    )
                else:
                    # Using the HF_INFERENCE_URL and the key
                    models = await LLMAgent.fetch_available_models(
                        HF_INFERENCE_URL, hf_api_key_val
                    )
                if models:
                    return gr.update(choices=models, value=models[0])
                return gr.update(choices=["No models found"])

            def update_agent_connection(connection_type, model_id, base_url_val, api_key_val, hf_api_key_val):
                # Fixed: Removed the 'use_huggingface' parameter from the constructor
                use_hf = connection_type == "HuggingFace Inference"
                if use_hf:
                    # Use the model_id directly (it's the model name like 'codellama/CodeLlama-7b-hf')
                    self.agent = LLMAgent(
                        model_id=model_id,
                        base_url=HF_INFERENCE_URL,
                        api_key=hf_api_key_val,
                        generate_fn=LLMAgent.openai_generate
                    )
                    return f"βœ… Switched to HuggingFace: {model_id}"
                else:
                    self.agent = LLMAgent(
                        model_id=model_id, 
                        base_url=base_url_val,
                        api_key=api_key_val,
                        generate_fn=LLMAgent.openai_generate
                    )
                    return f"βœ… Switched to Local: {base_url_val}"

            async def process_message(message, history, speech_enabled):
                if not message.strip():
                    return "", history, "Please enter a message"
                history = history + [[message, None]]
                try:
                    # Fixed: Uses the new chat_with_canvas method which includes canvas context
                    response = await self.agent.chat_with_canvas(
                        message, self.current_conversation, include_canvas=True
                    )
                    history[-1][1] = response
                    if speech_enabled and self.agent.speech_enabled:
                        self.agent.speak(response)
                    artifacts = self.agent.get_canvas_summary(self.current_conversation)
                    status = f"βœ… Response received. Canvas artifacts: {len(artifacts)}"
                    return "", history, status, artifacts
                except Exception as e:
                    error_msg = f"❌ Error: {str(e)}"
                    history[-1][1] = error_msg
                    return "", history, error_msg, self.agent.get_canvas_summary(self.current_conversation)

            def get_artifacts():
                return self.agent.get_canvas_summary(self.current_conversation)

            def clear_canvas():
                self.agent.clear_canvas(self.current_conversation)
                return [], "βœ… Canvas cleared"

            def clear_chat():
                self.agent.clear_conversation(self.current_conversation)
                return [], "βœ… Chat cleared"

            def new_session():
                self.agent.clear_conversation(self.current_conversation)
                self.agent.clear_canvas(self.current_conversation)
                return [], "# New session started\\nprint('Ready!')", "πŸ†• New session started", []

            # Connect events
            connection_type.change(switch_connection, inputs=connection_type, 
                                 outputs=[local_settings, hf_settings, model_dropdown])
            fetch_models_btn.click(fetch_models_updated, 
                                 inputs=[connection_type, base_url, api_key, hf_api_key],
                                 outputs=model_dropdown)
            update_config_btn.click(update_agent_connection,
                                inputs=[connection_type, model_dropdown, base_url, api_key, hf_api_key],
                                outputs=status_display)
            send_btn.click(process_message,
                         inputs=[message_input, chatbot, speech_toggle],
                         outputs=[message_input, chatbot, status_display, artifact_display])
            message_input.submit(process_message,
                               inputs=[message_input, chatbot, speech_toggle],
                               outputs=[message_input, chatbot, status_display, artifact_display])
            refresh_artifacts_btn.click(get_artifacts, outputs=artifact_display)
            clear_canvas_btn.click(clear_canvas, outputs=[artifact_display, status_display])
            clear_chat_btn.click(clear_chat, outputs=[chatbot, status_display])
            new_session_btn.click(new_session, outputs=[chatbot, code_editor, status_display, artifact_display])
            interface.load(get_artifacts, outputs=artifact_display)
        return interface

# --- Main Application ---
def main():
    console.log("[bold blue]πŸš€ Starting LCARS Terminal...[/bold blue]")
    is_space = os.getenv('SPACE_ID') is not None
    if is_space:
        console.log("[green]🌐 Detected HuggingFace Space[/green]")
    else:
        console.log("[blue]πŸ’» Running locally[/blue]")
    interface = LcarsInterface()
    demo = interface.create_interface()
    demo.launch(
        share=is_space
    )

if __name__ == "__main__":
    main()