File size: 27,732 Bytes
c7188b5
 
 
 
 
 
 
8a8939e
81ae03a
f3ab7bd
e9b69d7
3fefcc3
 
 
 
 
 
e5ac220
3fefcc3
 
dcf4ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d22c32
dcf4ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d22c32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d93a81
1d22c32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fefcc3
33990d0
 
 
86c9f34
c7188b5
 
 
dcf4ed6
c7188b5
 
 
 
 
 
 
 
 
 
 
 
 
 
9febc25
c7188b5
 
 
 
 
 
 
 
dcf4ed6
c7188b5
 
 
 
 
 
 
dcf4ed6
c7188b5
 
 
 
 
2dd74e7
e175515
33990d0
c7188b5
 
 
 
 
81ae03a
 
5d08f94
a07a1e4
 
 
c7188b5
 
 
 
 
 
d60e9d2
c7188b5
d167a15
 
c7188b5
d60e9d2
 
d167a15
 
 
 
 
 
 
 
 
54880c7
d167a15
 
d60e9d2
54880c7
d167a15
c7188b5
 
 
35475f8
33990d0
 
 
c7188b5
 
 
81ae03a
0b58527
557b800
 
ce3c490
60a7f01
 
 
 
c7188b5
 
60a7f01
 
 
61404fd
bfcd07a
 
60a7f01
61404fd
 
60a7f01
 
 
cc8a692
 
 
 
 
 
 
 
 
 
 
60a7f01
cc8a692
 
 
 
 
 
 
 
 
 
 
60a7f01
 
cc8a692
 
 
60a7f01
cc8a692
 
 
 
bfcd07a
cc8a692
 
61404fd
cc8a692
295f49c
 
cc8a692
873bb2e
295f49c
 
 
 
873bb2e
 
 
 
 
 
 
295f49c
 
 
 
 
cc8a692
295f49c
cc8a692
295f49c
cc8a692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295f49c
61404fd
 
 
f5fffed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61404fd
 
 
 
 
 
295f49c
 
cc8a692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4d296a
 
61404fd
 
 
 
 
 
 
 
a6ce288
61404fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295f49c
61404fd
 
 
 
 
c7188b5
 
a1a7ddf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7188b5
 
a1a7ddf
 
60a7f01
 
 
 
 
 
 
a1a7ddf
bfcd07a
 
 
 
60a7f01
aaead94
60a7f01
 
 
 
 
 
 
bfcd07a
60a7f01
 
 
 
 
 
 
 
 
 
 
bfcd07a
 
60a7f01
 
 
 
c7188b5
60a7f01
 
61404fd
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
# pixal_agent_full.py
import os
import datetime
import gradio as gr
import requests
from typing import Optional, List
from langchain.llms.base import LLM
from langchain.agents import initialize_agent, AgentType,load_tools
from langchain.agents import AgentExecutor, create_structured_chat_agent
from langchain.tools import Tool
from langchain_experimental.tools.python.tool import PythonREPLTool
import queue
from typing import Any, Dict
import gradio as gr
from langchain.callbacks.base import BaseCallbackHandler


from langchain.tools import YouTubeSearchTool as YTS
# 2. μ»€μŠ€ν…€ 콜백 ν•Έλ“€λŸ¬

# github_model_llm.py
"""
GitHub Models API 기반 LLM 래퍼 (LangChain LLM ν˜Έν™˜)
- OpenAI-style chat completions ν˜Έν™˜
- function calling (OPENAI_MULTI_FUNCTIONS) 지원: functions, function_call 전달 κ°€λŠ₯
- system prompt (system_prompt) 지원
- μ˜΅μ…˜: temperature, max_tokens, top_p λ“± 전달
- raw response λ°˜ν™˜ λ©”μ„œλ“œ 포함
"""

from typing import Optional, List, Dict, Any
import os
import time
import json
import requests
from requests.adapters import HTTPAdapter, Retry
from langchain.llms.base import LLM

'''
class GitHubModelLLM(LLM):

    def __init__(
        self,
        model: str = "openai/gpt-4.1",
        token: Optional[str] = os.environ["token"],
        endpoint: str = "https://models.github.ai/inference",
        system_prompt: Optional[str] = "λ„ˆλŠ” PIXAL(Primary Interactive X-ternal Assistant with multi Language)이야.λ„ˆμ˜ κ°œλ°œμžλŠ” μ •μ„±μœ€ μ΄λΌλŠ” 6ν•™λ…„ 파이썬 ν”„λ‘œκ·Έλž˜λ¨Έμ•Ό.",
        request_timeout: float = 30.0,
        max_retries: int = 2,
        backoff_factor: float = 0.3,
        **kwargs,
    ):
        """
        Args:
            model: λͺ¨λΈ 이름 (예: "openai/gpt-4.1")
            token: GitHub Models API 토큰 (Bearer). ν™˜κ²½λ³€μˆ˜ GITHUB_TOKEN / token μ‚¬μš© κ°€λŠ₯ as fallback.
            endpoint: API endpoint (κΈ°λ³Έ: https://models.github.ai/inference)
            system_prompt: (선택) system role λ©”μ‹œμ§€λ‘œ 항상 μ•žμ— λΆ™μž„
            request_timeout: μš”μ²­ νƒ€μž„μ•„μ›ƒ (초)
            max_retries: λ„€νŠΈμ›Œν¬ μž¬μ‹œλ„ 횟수
            backoff_factor: μž¬μ‹œλ„ μ§€μˆ˜ 보정
            kwargs: LangChain LLM λΆ€λͺ¨μ— 전달할 μΆ”κ°€ 인자
        """
        super().__init__(**kwargs)
        self.model = model
        self.endpoint = endpoint.rstrip("/")
        self.token = token or os.getenv("GITHUB_TOKEN") or os.getenv("token")
        self.system_prompt = system_prompt
        self.request_timeout = request_timeout

        # requests μ„Έμ…˜ + μž¬μ‹œλ„ μ„€μ •
        self.session = requests.Session()
        retries = Retry(total=max_retries, backoff_factor=backoff_factor,
                        status_forcelist=[429, 500, 502, 503, 504],
                        allowed_methods=["POST", "GET"])
        self.session.mount("https://", HTTPAdapter(max_retries=retries))
        self.session.headers.update({
            "Content-Type": "application/json"
        })
        if self.token:
            self.session.headers.update({"Authorization": f"Bearer {self.token}"})

    @property
    def _llm_type(self) -> str:
        return "github_models_api"

    # ---------- 편의 internal helper ----------
    def _build_messages(self, prompt: str, extra_messages: Optional[List[Dict[str, Any]]] = None) -> List[Dict[str, Any]]:
        """
        messages λ°°μ—΄ 생성: system (optional) + extra_messages (if any) + user prompt
        extra_messages: 이미 role keys둜 κ΅¬μ„±λœ λ©”μ‹œμ§€ 리슀트 (예: conversation history)
        """
        msgs: List[Dict[str, Any]] = []
        if self.system_prompt:
            msgs.append({"role": "system", "content": self.system_prompt})
        if extra_messages:
            # ensure format: list of {"role":..,"content":..}
            msgs.extend(extra_messages)
        msgs.append({"role": "user", "content": prompt})
        return msgs

    def _post_chat(self, body: Dict[str, Any]) -> Dict[str, Any]:
        url = f"{self.endpoint}/chat/completions"
        # ensure Authorization present
        if "Authorization" not in self.session.headers and not self.token:
            raise ValueError("GitHub Models token not set. Provide token param or set GITHUB_TOKEN env var.")

        resp = self.session.post(url, json=body, timeout=self.request_timeout)
        try:
            resp.raise_for_status()
        except requests.HTTPError as e:
            # try to surface JSON error if present
            content = resp.text
            try:
                j = resp.json()
                content = json.dumps(j, ensure_ascii=False, indent=2)
            except Exception:
                pass
            raise RuntimeError(f"GitHub Models API error: {e} - {content}")
        return resp.json()

    # ---------- LangChain LLM interface ----------
    def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs) -> str:
        """
        LangChain LLM `_call` κ΅¬ν˜„ (동기).
        Supports kwargs:
            - functions: list[dict] (function schemas)
            - function_call: "auto" | {"name": "..."} | etc.
            - messages: list[dict]  (if you want to pass full conversation instead of prompt)
            - temperature, top_p, max_tokens, n, stream, etc.
        Returns:
            assistant content (string). If function_call is returned by model, returns the 'content' if present,
            otherwise returns function_call object as JSON string (so caller can parse).
        """
        # support passing full messages via kwargs['messages']
        messages = None
        extra_messages = None
        if "messages" in kwargs and isinstance(kwargs["messages"], list):
            messages = kwargs.pop("messages")
        else:
            # optionally allow 'history' or 'extra_messages'
            extra_messages = kwargs.pop("extra_messages", None)

        if messages is None:
            messages = self._build_messages(prompt, extra_messages=extra_messages)

        body: Dict[str, Any] = {
            "model": self.model,
            "messages": messages,
        }

        # pass optional top-level params (temperature, max_tokens, etc.) from kwargs
        for opt in ["temperature", "top_p", "max_tokens", "n", "stream", "presence_penalty", "frequency_penalty"]:
            if opt in kwargs:
                body[opt] = kwargs.pop(opt)

        # pass function-calling related keys verbatim if provided
        if "functions" in kwargs:
            body["functions"] = kwargs.pop("functions")
        if "function_call" in kwargs:
            body["function_call"] = kwargs.pop("function_call")

        # include stop if present
        if stop:
            body["stop"] = stop

        # send request
        raw = self._post_chat(body)

        # save raw for caller if needed
        self._last_raw = raw

        # parse assistant message
        choices = raw.get("choices") or []
        if not choices:
            return ""

        message_obj = choices[0].get("message", {})

        # if assistant returned a function_call, include that info
        if "function_call" in message_obj:
            # return function_call as JSON string so agent/tool orchestrator can parse it
            # but if content also exists, prefer content
            func = message_obj["function_call"]
            # sometimes content may be absent; return structured JSON string
            return json.dumps({"function_call": func}, ensure_ascii=False)

        # otherwise return assistant content
        return message_obj.get("content", "") or ""

    # optional: expose raw response getter
    def last_raw_response(self) -> Optional[Dict[str, Any]]:
        return getattr(self, "_last_raw", None)

    # optional: provide a convenience chat method to get full message object
    def chat_completions(self, prompt: str, messages: Optional[List[Dict[str, Any]]] = None, **kwargs) -> Dict[str, Any]:
        """
        Directly call chat completions and return full parsed JSON response.
        - If `messages` provided, it's used as the full messages array (system/user/assistant roles as needed)
        - else uses prompt + system_prompt to construct messages.
        """
        if messages is None:
            messages = self._build_messages(prompt)
        body: Dict[str, Any] = {"model": self.model, "messages": messages}
        for opt in ["temperature", "top_p", "max_tokens", "n", "stream"]:
            if opt in kwargs:
                body[opt] = kwargs.pop(opt)
        if "functions" in kwargs:
            body["functions"] = kwargs.pop("functions")
        if "function_call" in kwargs:
            body["function_call"] = kwargs.pop("function_call")
        raw = self._post_chat(body)
        self._last_raw = raw
        return raw
'''
from typing import Optional, List, Dict, Any
from langchain.llms.base import LLM
import requests, os, json
from requests.adapters import HTTPAdapter, Retry

class GitHubModelLLM(LLM):
    """GitHub Models API 기반 LangChain LLM (Pydantic ν˜Έν™˜)"""
    model: str = "openai/gpt-4.1"
    endpoint: str = "https://models.github.ai/inference"
    token: Optional[str] = os.environ["token"]
    system_prompt: Optional[str] = "λ„ˆλŠ” PIXAL(Primary Interactive X-ternal Assistant with multi Language)이야.λ„ˆμ˜ κ°œλ°œμžλŠ” μ •μ„±μœ€ μ΄λΌλŠ” 6ν•™λ…„ 파이썬 ν”„λ‘œκ·Έλž˜λ¨Έμ•Ό."
    request_timeout: float = 30.0
    max_retries: int = 2
    backoff_factor: float = 0.3

    @property
    def _llm_type(self) -> str:
        return "github_models_api"

    def _post_chat(self, body: Dict[str, Any]) -> Dict[str, Any]:
        token = self.token or os.getenv("GITHUB_TOKEN") or os.getenv("token")
        if not token:
            raise ValueError("❌ GitHub token이 μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.")

        session = requests.Session()
        retries = Retry(total=self.max_retries, backoff_factor=self.backoff_factor,
                        status_forcelist=[429, 500, 502, 503, 504])
        session.mount("https://", HTTPAdapter(max_retries=retries))
        session.headers.update({
            "Content-Type": "application/json",
            "Authorization": "Bearer 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"
        })
        resp = session.post(f"{self.endpoint}/chat/completions", json=body, timeout=self.request_timeout)
        resp.raise_for_status()
        return resp.json()

    def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs) -> str:
        body = {
            "model": self.model,
            "messages": []
        }
        if self.system_prompt:
            body["messages"].append({"role": "system", "content": self.system_prompt})
        body["messages"].append({"role": "user", "content": prompt})

        for key in ["temperature", "max_tokens", "functions", "function_call"]:
            if key in kwargs:
                body[key] = kwargs[key]
        if stop:
            body["stop"] = stop

        res = self._post_chat(body)
        msg = res.get("choices", [{}])[0].get("message", {})
        return msg.get("content") or json.dumps(msg.get("function_call", {}))

from langchain_community.retrievers import WikipediaRetriever
from langchain.tools.retriever import create_retriever_tool
retriever = WikipediaRetriever(lang="ko",top_k_results=10)
wiki=Tool(func=retriever.get_relevant_documents,name="WIKI SEARCH",description="μœ„ν‚€λ°±κ³Όμ—μ„œ ν•„μš”ν•œ 정보λ₯Ό λΆˆλŸ¬μ˜΅λ‹ˆλ‹€.κ²°κ΄΄λ₯Ό κ²€μ¦ν•˜μ—¬ μ‚¬μš©ν•˜μ‹œμ˜€.")
# ──────────────────────────────
# βœ… GitHub Models LLM
# ──────────────────────────────
'''
class GitHubModelLLM(LLM):
    model: str = "openai/gpt-4.1"
    endpoint: str = "https://models.github.ai/inference"
    token: Optional[str] = None

    @property
    def _llm_type(self) -> str:
        return "github_models_api"

    def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
        if not self.token:
            raise ValueError("GitHub API token이 ν•„μš”ν•©λ‹ˆλ‹€.")

        headers = {
            "Authorization": "Bearer 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",
            "Content-Type": "application/json",
        }
        body = {"model": self.model, "messages": [{"role": "user", "content": prompt}]}

        resp = requests.post(f"{self.endpoint}/chat/completions", json=body, headers=headers)
        if resp.status_code != 200:
            raise ValueError(f"API 였λ₯˜: {resp.status_code} - {resp.text}")
        return resp.json()["choices"][0]["message"]["content"]
'''
# ──────────────────────────────
# βœ… LLM μ„€μ •
# ──────────────────────────────
token = os.getenv("GITHUB_TOKEN") or os.getenv("token")
if not token:
    print("⚠️ GitHub Token이 ν•„μš”ν•©λ‹ˆλ‹€. 예: setx GITHUB_TOKEN your_token")

llm = GitHubModelLLM()

# ──────────────────────────────
# βœ… LangChain κΈ°λ³Έ 도ꡬ 뢈러였기
# ──────────────────────────────
tools = load_tools(
    ["ddg-search", "requests_all", "llm-math"],
    llm=llm,allow_dangerous_tools=True
)+[YTS()]+[wiki]
# ──────────────────────────────
# βœ… Python μ‹€ν–‰ 도ꡬ (LangChain λ‚΄μž₯)
# ──────────────────────────────
python_tool = PythonREPLTool()
tools.append(Tool(name="python_repl", func=python_tool.run, description="Python μ½”λ“œλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€."))
from langchain import hub
prompt=hub.pull("hwchase17/structured-chat-agent")
from langchain_community.tools.shell.tool import ShellTool
shell_tool = ShellTool()
tools.append(Tool(name="shell_exec", func=shell_tool.run, description="둜컬 λͺ…λ Ήμ–΄λ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€."))

# ──────────────────────────────
# βœ… 파일 도ꡬ
# ──────────────────────────────


# ──────────────────────────────
# βœ… μ •ν™•ν•œ ν•œκ΅­ μ‹œκ°„ ν•¨μˆ˜ (Asia/Seoul)
# ──────────────────────────────
import requests
from zoneinfo import ZoneInfo

def time_now(_=""):
    try:
        # μ •ν™•ν•œ UTC μ‹œκ°μ„ μ™ΈλΆ€ APIμ—μ„œ κ°€μ Έμ˜΄
        resp = requests.get("https://timeapi.io/api/Time/current/zone?timeZone=Asia/Seoul", timeout=5)
        if resp.status_code == 200:
            data = resp.json()
            dt = data["dateTime"].split(".")[0].replace("T", " ")
            return f"ν˜„μž¬ μ‹œκ°: {dt} (Asia/Seoul, μ„œλ²„ κΈ°μ€€ NTP 동기화)"
        else:
            # API μ‹€νŒ¨ μ‹œ 둜컬 μ‹œμŠ€ν…œ μ‹œκ°μœΌλ‘œ λŒ€μ²΄
            tz = ZoneInfo("Asia/Seoul")
            now = datetime.datetime.now(tz)
            return f"ν˜„μž¬ μ‹œκ°(둜컬): {now.strftime('%Y-%m-%d %H:%M:%S')} (Asia/Seoul)"
    except Exception as e:
        tz = ZoneInfo("Asia/Seoul")
        now = datetime.datetime.now(tz)
        return f"ν˜„μž¬ μ‹œκ°(λ°±μ—…): {now.strftime('%Y-%m-%d %H:%M:%S')} (Asia/Seoul, 였λ₯˜: {e})"
# ──────────────────────────────
# βœ… 도ꡬ 등둝
# ──────────────────────────────
tools.extend([Tool(name="time_now", func=time_now, description="ν˜„μž¬ μ‹œκ°„μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.")])
from langchain.memory import ConversationBufferMemory as MEM
from langchain.agents.agent_toolkits import FileManagementToolkit as FMT
tools.extend(FMT(root_dir=str(os.getcwd())).get_tools())
# ──────────────────────────────
# βœ… Agent μ΄ˆκΈ°ν™”
# ──────────────────────────────
mem=MEM()
agent=initialize_agent(tools,llm,agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,verbose=True,memory=mem)
#agent = create_structured_chat_agent(llm, tools, prompt)
#agent= AgentExecutor(agent=agent, tools=tools,memory=mem)

# ... (μœ„μ˜ LLM, tools, agent μ„€μ • 뢀뢄은 동일)


import json

# ──────────────────────────────
# βœ… λŒ€ν™” μš”μ•½ ν•¨μˆ˜
# ──────────────────────────────
def summarize_title(history):
    """λŒ€ν™” 제λͺ© μš”μ•½"""
    if not history: return "μƒˆ λŒ€ν™”"
    text = "\n".join(f"User:{h[0]} AI:{h[1]}" for h in history[-3:])
    try:
        title = llm._call(f"λ‹€μŒ λŒ€ν™”μ˜ 주제λ₯Ό ν•œ μ€„λ‘œ μš”μ•½ν•΄μ€˜:\n{text}")
        return title.strip().replace("\n", " ")[:60]
    except Exception:
        return "μš”μ•½ μ‹€νŒ¨"

import pickle
import os, datetime

# ν˜„μž¬ λ””λ ‰ν„°λ¦¬λ‘œ κ³ μ •
os.chdir(os.path.dirname(os.path.abspath(__file__)))
os.makedirs("user_logs", exist_ok=True)

# --- λŒ€ν™” 기둝 μ €μž₯/뢈러였기 ---
def save_conversation(username, history, conv_name="current"):
    """κ³„μ •λ³„λ‘œ λŒ€ν™”κΈ°λ‘μ„ pickle둜 μ‹€μ‹œκ°„ μ €μž₯"""
    os.makedirs(f"user_logs/{username}", exist_ok=True)
    title = summarize_title(history)
    fname = f"user_logs/{username}/{conv_name}.pkl"
    with open(fname, "wb") as f:
        pickle.dump({"title": title, "history": history}, f)

def load_conversation(username, conv_name="current"):
    path = f"user_logs/{username}/{conv_name}.pkl"
    if not os.path.exists(path):
        return []
    with open(path, "rb") as f:
        data = pickle.load(f)
    return data.get("history", [])

def list_conversations(username):
    os.makedirs(f"user_logs/{username}", exist_ok=True)
    files = [f for f in os.listdir(f"user_logs/{username}") if f.endswith(".pkl")]
    titles = []
    for f in files:
        with open(f"user_logs/{username}/" + f, "rb") as fp:
            data = pickle.load(fp)
            titles.append((data.get("title", f), f))
    return titles

# --- chat ν•¨μˆ˜ μˆ˜μ • ---
def chat(message, history, username="guest", conv_name="current"):
    try:
        raw_response = agent.run(message)
        text = str(raw_response)

        # JSON ν˜•μ‹ 응닡 νŒŒμ‹±
        output = text
        match = re.search(r"\{.*\}", text, re.DOTALL)
        if match:
            try:
                obj = json.loads(match.group(0))
                output = (
                    obj.get("action_input")
                    or obj.get("Final Answer")
                    or obj.get("output")
                    or obj.get("content")
                    or text
                )
            except Exception:
                output = text
    except Exception as e:
        output = f"⚠️ 였λ₯˜: {e}"

    # 기둝 μΆ”κ°€ 및 μ¦‰μ‹œ μ €μž₯
    history = history + [(message, output)]
    save_conversation(username, history, conv_name)
    return history, history, ""

# --- 뢈러였기 λ²„νŠΌ ν•¨μˆ˜ ---
def load_selected(username, file):
    path = f"user_logs/{username}/{file}"
    if not os.path.exists(path):
        return []
    with open(path, "rb") as f:
        data = pickle.load(f)
    return data.get("history", [])
# ──────────────────────────────
# βœ… 둜그인 ν›„ μ‚¬μš©μž 정보 κ°€μ Έμ˜€κΈ°
# ──────────────────────────────
def get_hf_user(token):
    """HF OAuth ν† ν°μœΌλ‘œ μ‚¬μš©μž 정보 쑰회"""
    try:
        r = requests.get("https://huggingface.co/api/whoami-v2", headers={"Authorization": f"Bearer {token}"})
        if r.status_code == 200:
            data = r.json()
            return data.get("name") or data.get("email") or "unknown_user"
    except Exception:
        pass
    return "guest"
import re, json


'''
def chat(message, history, hf_token):
    username = get_hf_user(hf_token) if hf_token else "guest"
    try:
        response = agent.invoke(message)
        if isinstance(response, dict):
            if "action_input" in response:
                response = response["action_input"]
            elif "output" in response:
                response = response["output"]
            elif "text" in response:
                response = response["text"]
            else:
                response = str(response)
        elif isinstance(response, str):
            # "Final Answer"κ°€ ν¬ν•¨λœ λ¬Έμžμ—΄μ΄λ©΄ κ·Έ λΆ€λΆ„λ§Œ μΆ”μΆœ
            if '"action_input":' in response:
                import re, json
                match = re.search(r'["\']action_input["\']\s*:\s*["\'](.*?)["\']', response)
                if match:
                    response = match.group(1)
            elif "Final Answer" in response:
                # {"action": "Final Answer", "action_input": "..."} ν˜•μ‹μΌ λ•Œ
                try:
                    data = json.loads(response)
                    if isinstance(data, dict) and "action_input" in data:
                        response = data["action_input"]
                except Exception:
                    response = response.replace("Final Answer", "").strip()
    except Exception as e:
        response = f"⚠️ 였λ₯˜: {e}"
    history = history + [(message, response)]
    if username:
        save_conversation(username, history)
    return history, history, ""  # μž…λ ₯ μ΄ˆκΈ°ν™”
'''
# 예: hf_token (ν˜Ήμ€ username) 을 μž…λ ₯으둜 받도둝 λ³€κ²½
def refresh_conversation_list(username="guest"):
    """계정별 λŒ€ν™” λͺ©λ‘μ„ μƒˆλ‘œκ³ μΉ¨ (Gradio Dropdown μ—…λ°μ΄νŠΈμš©)"""
    base_dir = os.path.join("user_logs", username)
    os.makedirs(base_dir, exist_ok=True)

    files = sorted(
        [f for f in os.listdir(base_dir) if f.endswith(".pkl")],
        reverse=True
    )

    # 제λͺ© λͺ©λ‘ λ§Œλ“€κΈ°
    titles = []
    for f in files:
        try:
            with open(os.path.join(base_dir, f), "rb") as fp:
                data = pickle.load(fp)
                title = data.get("title", f.replace(".pkl", ""))
        except Exception:
            title = f.replace(".pkl", "")
        titles.append(title)

    # Dropdown μ—…λ°μ΄νŠΈ
    if titles:
        return gr.update(choices=titles, value=titles[0])
    else:
        return gr.update(choices=[], value=None)




# ──────────────────────────────
# βœ… Gradio UI with HF Auth
# ──────────────────────────────
with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant (HF Auth)") as demo:
    gr.Markdown("## πŸ€– PIXAL Assistant β€” Hugging Face 계정 기반 λŒ€ν™” μ €μž₯")

    hf_login = gr.LoginButton()
    hf_token = gr.State()

    @hf_login.click(inputs=None, outputs=hf_token)
    def login(token):  # 둜그인 ν›„ token λ°˜ν™˜
        return token

    with gr.Row():
        with gr.Column(scale=2):
            chatbot = gr.Chatbot(label="PIXAL λŒ€ν™”", height=600, render_markdown=True)
            msg = gr.Textbox(label="λ©”μ‹œμ§€", placeholder="μž…λ ₯ ν›„ Enter λ˜λŠ” 전솑 클릭")
            send = gr.Button("전솑")
            clear = gr.Button("μ΄ˆκΈ°ν™”")

            msg.submit(chat, [msg, chatbot, hf_token], [chatbot, chatbot, msg])
            send.click(chat, [msg, chatbot, hf_token], [chatbot, chatbot, msg])
            clear.click(lambda: None, None, chatbot, queue=False)

        with gr.Column(scale=1):
            gr.Markdown("### πŸ’Ύ μ €μž₯된 λŒ€ν™” 기둝")
            convo_files = gr.Dropdown(label="λŒ€ν™” 선택", choices=[])
            refresh_btn = gr.Button("πŸ”„ λͺ©λ‘ μƒˆλ‘œκ³ μΉ¨")
            load_btn = gr.Button("뢈러였기")

            refresh_btn.click(refresh_conversation_list, None, convo_files)
            load_btn.click(load_selected, [convo_files], chatbot)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)
'''
def chat(message, history):
    try:
        response = agent.run(message)

        # JSON ν˜•νƒœλ‘œ 좜λ ₯될 κ°€λŠ₯성이 μžˆλŠ” 경우 처리
        if isinstance(response, dict):
            if "action_input" in response:
                response = response["action_input"]
            elif "output" in response:
                response = response["output"]
            elif "text" in response:
                response = response["text"]
            else:
                response = str(response)
        elif isinstance(response, str):
            # "Final Answer"κ°€ ν¬ν•¨λœ λ¬Έμžμ—΄μ΄λ©΄ κ·Έ λΆ€λΆ„λ§Œ μΆ”μΆœ
            if '"action_input":' in response:
                import re, json
                match = re.search(r'["\']action_input["\']\s*:\s*["\'](.*?)["\']', response)
                if match:
                    response = match.group(1)
            elif "Final Answer" in response:
                # {"action": "Final Answer", "action_input": "..."} ν˜•μ‹μΌ λ•Œ
                try:
                    data = json.loads(response)
                    if isinstance(data, dict) and "action_input" in data:
                        response = data["action_input"]
                except Exception:
                    response = response.replace("Final Answer", "").strip()

    except Exception as e:
        response = f"⚠️ 였λ₯˜: {e}"

    history = history + [(message, response)]
    return history, history,""

# ──────────────────────────────
# βœ… Gradio UI
# ──────────────────────────────
def load_selected(file):
    return load_conversation(file)

# ──────────────────────────────
# βœ… Gradio UI
# ──────────────────────────────
with gr.Blocks(theme=gr.themes.Soft(), title="PIXAL Assistant") as demo:
    gr.Markdown("## πŸ€– PIXAL Assistant β€” LangChain 기반 λ©€ν‹°νˆ΄ μ—μ΄μ „νŠΈ")

    with gr.Row():
        with gr.Column(scale=2):
            chatbot = gr.Chatbot(label="PIXAL λŒ€ν™”", height=600)
            msg = gr.Textbox(label="λ©”μ‹œμ§€", placeholder="μž…λ ₯ ν›„ Enter λ˜λŠ” 전솑 클릭")
            send = gr.Button("전솑")
            clear = gr.Button("μ΄ˆκΈ°ν™”")

            username = gr.Textbox(label="Hugging Face μ‚¬μš©μžλͺ…", placeholder="둜그인 λŒ€μ‹  이름 μž…λ ₯", value=os.getenv("HF_USER", "guest"))
            msg.submit(chat, [msg, chatbot, username], [chatbot, chatbot, msg])
            send.click(chat, [msg, chatbot, username], [chatbot, chatbot, msg])
            clear.click(lambda: None, None, chatbot, queue=False)

        with gr.Column(scale=1):
            gr.Markdown("### πŸ’Ύ μ €μž₯된 λŒ€ν™” 기둝")
            convo_files = gr.Dropdown(label="λŒ€ν™” 선택", choices=[])
            refresh_btn = gr.Button("πŸ”„ λͺ©λ‘ μƒˆλ‘œκ³ μΉ¨")
            load_btn = gr.Button("뢈러였기")

            def refresh_list(user):
                if not user: return gr.Dropdown.update(choices=[])
                return gr.Dropdown.update(choices=[x[1] for x in list_conversations(user)])

            refresh_btn.click(refresh_list, [username], convo_files)
            load_btn.click(lambda f: load_conversation(f), [convo_files], chatbot)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)

'''