File size: 50,526 Bytes
3fc84bc
 
 
 
 
 
 
 
 
 
 
 
6fb9a50
 
3fc84bc
 
 
 
 
 
 
 
 
 
6fb9a50
 
 
 
 
 
 
 
 
 
 
3fc84bc
 
 
 
 
6fb9a50
3fc84bc
a1a65b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fc84bc
 
6fb9a50
5efb10c
 
 
 
a1a65b1
5efb10c
 
 
 
a1a65b1
5efb10c
 
 
a1a65b1
 
 
5efb10c
 
a1a65b1
 
 
5efb10c
 
a1a65b1
 
 
5efb10c
 
a1a65b1
 
 
5efb10c
 
a1a65b1
 
5efb10c
 
 
a1a65b1
 
 
 
5efb10c
 
a1a65b1
 
 
5efb10c
 
a1a65b1
 
 
 
 
 
5efb10c
 
a1a65b1
5efb10c
 
a1a65b1
 
 
 
5efb10c
 
 
 
a1a65b1
5efb10c
 
a1a65b1
 
 
 
 
 
 
 
 
 
 
 
3fc84bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fb9a50
3fc84bc
 
6fb9a50
a1a65b1
3fc84bc
 
 
c33be7e
a1a65b1
 
6fb9a50
3fc84bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1a65b1
3fc84bc
 
a1a65b1
6fb9a50
 
3fc84bc
770fd5e
3fc84bc
 
c33be7e
6fb9a50
3fc84bc
 
 
 
 
 
 
 
6fb9a50
3fc84bc
 
 
 
 
 
e053d6e
 
 
 
 
 
3fc84bc
 
 
 
a1a65b1
3fc84bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1a65b1
3fc84bc
 
a1a65b1
 
 
3fc84bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1a65b1
 
 
3fc84bc
 
 
 
 
 
89d6b7d
3fc84bc
 
 
89d6b7d
 
 
 
 
 
c549e3b
 
89d6b7d
3fc84bc
a1a65b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fc84bc
89d6b7d
a1a65b1
89d6b7d
c549e3b
 
 
 
 
89d6b7d
 
 
 
 
 
 
 
 
 
 
 
 
3fc84bc
89d6b7d
3fc84bc
 
 
 
 
a1a65b1
3fc84bc
 
a1a65b1
 
 
 
 
 
 
 
 
 
 
3fc84bc
 
 
 
 
a1a65b1
 
3fc84bc
 
a1a65b1
 
 
3fc84bc
 
 
 
 
 
 
 
 
a1a65b1
3fc84bc
a1a65b1
 
3fc84bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89d6b7d
 
 
 
 
c549e3b
 
 
 
89d6b7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fc84bc
 
 
 
 
 
 
 
a1a65b1
3fc84bc
 
 
 
 
 
 
a1a65b1
 
 
 
 
 
 
 
 
 
 
3fc84bc
 
 
 
a1a65b1
3fc84bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89d6b7d
a1a65b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fc84bc
 
 
 
a1a65b1
3fc84bc
 
 
 
6fb9a50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fc84bc
6fb9a50
3fc84bc
6fb9a50
 
 
 
 
3fc84bc
6fb9a50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fc84bc
 
 
 
 
 
 
 
 
 
 
a1a65b1
 
 
6fb9a50
a1a65b1
 
 
6fb9a50
a1a65b1
3fc84bc
6fb9a50
3fc84bc
a1a65b1
6fb9a50
a1a65b1
3fc84bc
 
 
 
6fb9a50
3fc84bc
 
 
 
a1a65b1
3fc84bc
 
a1a65b1
6fb9a50
3fc84bc
 
 
a1a65b1
3fc84bc
 
 
 
 
a1a65b1
3fc84bc
a1a65b1
3fc84bc
a1a65b1
3fc84bc
a1a65b1
 
3fc84bc
 
 
6fb9a50
3fc84bc
 
 
 
 
 
6fb9a50
3fc84bc
 
 
a1a65b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fc84bc
 
 
89d6b7d
a1a65b1
3fc84bc
8d2641c
3fc84bc
6fb9a50
 
 
 
 
3fc84bc
6fb9a50
 
 
 
 
 
 
3fc84bc
a1a65b1
3fc84bc
 
 
 
 
6fb9a50
 
3fc84bc
a1a65b1
3fc84bc
 
 
 
 
 
a1a65b1
6fb9a50
3fc84bc
a1a65b1
3fc84bc
 
 
 
 
 
a1a65b1
3fc84bc
 
 
 
 
 
6fb9a50
3fc84bc
6fb9a50
 
 
3fc84bc
 
 
6fb9a50
3fc84bc
 
 
 
 
 
 
a1a65b1
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
import os
import re
import random
import time
import html
import base64
import string
import json
import asyncio
import requests
import anthropic
import openai
import io
import logging

from http import HTTPStatus
from typing import Dict, List, Optional, Tuple
from functools import partial

import gradio as gr
import modelscope_studio.components.base as ms
import modelscope_studio.components.legacy as legacy
import modelscope_studio.components.antd as antd

# === [1] 로거 설정 ===
log_stream = io.StringIO()
handler = logging.StreamHandler(log_stream)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)   # 원하는 레벨로 설정
logger.addHandler(handler)

def get_logs():
    """StringIO에 쌓인 로그를 문자열로 반환"""
    return log_stream.getvalue()


# ------------------------
# 1) DEMO_LIST 및 SystemPrompt
# ------------------------


DEMO_LIST = [
    {"description": "블록이 위에서 떨어지는 클래식 테트리스 게임을 개발해주세요. 화살표 키로 조작하며, 가로줄이 채워지면 해당 줄이 제거되고 점수가 올라가는 메커니즘이 필요합니다. 난이도는 시간이 지날수록 블록이 빨라지도록 구현하고, 게임오버 조건과 점수 표시 기능을 포함해주세요."},
    {"description": "두 명이 번갈아가며 플레이할 수 있는 체스 게임을 만들어주세요. 기본적인 체스 규칙(킹, 퀸, 룩, 비숍, 나이트, 폰의 이동 규칙)을 구현하고, 체크와 체크메이트 감지 기능이 필요합니다. 드래그 앤 드롭으로 말을 움직일 수 있게 하며, 이동 기록도 표시해주세요."},
    {"description": "짝을 맞추는 메모리 카드 게임을 개발해주세요. 카드를 뒤집으면 그림이 나타나고, 같은 그림의 카드 두 장을 찾으면 점수를 얻는 방식입니다. 카드 뒤집기 애니메이션과 함께 시도 횟수를 기록하는 점수 시스템, 그리고 쉬움/보통/어려움 난이도 선택 기능(카드 수 변경)도 구현해주세요."},
    {"description": "플레이어가 우주선을 조종하여 적 우주선을 파괴하는 슈팅 게임을 만들어주세요. 키보드 방향키로 움직이고 스페이스바로 발사하며, 다양한 적 웨이브가 공격해오는 구조입니다. 충돌 감지 시스템과 함께 파워업 아이템(방패, 다중 발사, 속도 증가 등)을 구현하고, 난이도가 점진적으로 증가하는 시스템을 추가해주세요."},
    {"description": "3x3 또는 4x4 크기의 슬라이드 퍼즐 게임을 만들어주세요. 숫자나 이미지 조각을 섞은 후, 빈 칸을 이용해 조각들을 올바른 위치로 밀어 맞추는 게임입니다. 섞기 기능과 이동 횟수 카운터, 완성 시 축하 메시지를 표시하고, 난이도 설정(크기 변경)도 구현해주세요."},
    {"description": "고전적인 뱀 게임을 구현해주세요. 플레이어는 방향키로 뱀을 조종하여 필드에 랜덤하게 생성되는 먹이를 먹으며, 먹이를 먹을 때마다 뱀의 길이가 늘어납니다. 자신의 몸에 부딪히거나 벽에 부딪히면 게임이 종료되며, 점수는 먹은 먹이의 수에 비례합니다. 시간이 지날수록 뱀의 이동 속도가 빨라지는 난이도 조절 기능도 추가해주세요."},
    {"description": "화면 상단에 여러 줄의 벽돌이 배치된 브레이크아웃 게임을 만들어주세요. 플레이어는 화면 하단의 패들을 좌우로 움직여 공을 튕겨내어 벽돌을 깨야 합니다. 벽돌을 모두 깨면 스테이지 클리어, 공이 바닥에 떨어지면 생명이 감소합니다. 공의 속도는 시간이 지날수록 증가하며, 특수 벽돌(추가 생명, 패들 확장 등)도 구현해주세요."},
    {"description": "길을 따라 이동하는 적들을 방어하는 타워 디펜스 게임을 개발해주세요. 플레이어는 맵의 특정 위치에 다양한 타워(기본 공격, 범위 공격, 감속 효과 등)를 설치하여 적을 물리쳐야 합니다. 웨이브 시스템으로 난이도가 점진적으로 증가하며, 적을 처치하면 자원을 얻어 타워를 업그레이드하거나 새 타워를 건설할 수 있는 경제 시스템을 구현해주세요."},
    {"description": "캐릭터가 끝없이 달리며 장애물을 뛰어넘는 엔드리스 러너 게임을 만들어주세요. 스페이스바나 마우스 클릭으로 점프하여 다가오는 장애물(바위, 구덩이, 적 등)을 피해야 합니다. 거리에 따라 점수가 증가하며, 코인 등의 수집품을 모으는 요소와 파워업(일시적 무적, 자석 효과 등)도 추가해주세요. 시간이 지날수록 게임 속도가 빨라지는 난이도 시스템도 구현해주세요."},
    {"description": "2D 플랫포머 게임을 개발해주세요. 플레이어는 방향키로 캐릭터를 조종하여 발판 위를 이동하고, 스페이스바로 점프하며 코인이나 보석 같은 아이템을 수집합니다. 적 캐릭터(간단한 AI로 움직임)와 함정(가시, 떨어지는 발판 등)을 피해 목표 지점까지 도달하는 레벨 기반 구조로 만들어주세요. 체력 시스템과 체크포인트 기능도 구현해주세요."},
    {"description": "매번 새로운 미로를 자동 생성하는 미로 게임을 만들어주세요. 플레이어는 시작점에서 출발하여 방향키로 캐릭터를 조종해 출구를 찾아야 합니다. 미로 생성 알고리즘(예: 깊이 우선 탐색, 프림 알고리즘 등)을 활용하여 다양한 크기와 복잡도의 미로를 만들고, 타이머로 시간을 측정하며, 선택적으로 최단 경로를 보여주는 힌트 기능도 구현해주세요."},
    {"description": "간단한 턴제 RPG 게임을 개발해주세요. 플레이어는 탑다운 뷰에서 타일 기반으로 이동하며, 몬스터와 마주치면 턴제 전투가 시작됩니다. 기본 공격, 특수 스킬, 아이템 사용 등의 전투 옵션과 함께 레벨업 시스템(경험치, 능력치 상승)을 구현해주세요. 또한 전투에서 승리하면 골드와 아이템을 획득할 수 있으며, 상점에서 장비를 구매하는 기능도 추가해주세요."},
    {"description": "같은 색상/모양의 아이템 3개 이상을 일렬로 맞추는 매치-3 퍼즐 게임을 만들어주세요. 아이템을 스와이프하여 위치를 바꾸고, 매치되면 아이템이 사라지며 점수를 얻는 방식입니다. 특수 매치(4개 이상, T자 모양 등)는 특수 아이템을 생성하며, 연속 매치(콤보)는 추가 점수나 보너스 효과를 제공합니다. 목표 점수 또는 제한 시간/이동 횟수 모드를 구현해주세요."},
    {"description": "플래피 버드 스타일의 게임을 개발해주세요. 플레이어는 스페이스바나 마우스 클릭으로 새를 점프시켜 위아래로 움직이는 파이프 사이를 통과해야 합니다. 파이프에 부딪히거나 화면 상단/하단에 닿으면 게임 오버이며, 통과한 파이프 쌍마다 점수가 1점씩 증가합니다. 파이프 간격은 랜덤하게 생성되며, 최고 점수를 로컬 스토리지에 저장하는 기능도 구현해주세요."},
    {"description": "두 개의 유사한 이미지에서 차이점을 찾는 게임을 만들어주세요. 5-10개의 차이점이 있는 이미지 쌍을 준비하고, 플레이어가 차이점을 클릭하면 표시되도록 합니다. 제한 시간 내에 모든 차이점을 찾아야 하며, 오답 클릭 시 시간 패널티가 부과됩니다. 힌트 시스템(차이점 하나를 자동으로 표시)과 난이도 선택(쉬움: 차이점이 명확, 어려움: 미묘한 차이)도 구현해주세요."},
    {"description": "화면 상단에서 단어가 떨어지는 타이핑 게임을 개발해주세요. 플레이어는 키보드로 해당 단어를 정확히 입력하여 단어가 바닥에 닿기 전에 제거해야 합니다. 정확히 입력한 단어는 사라지고 점수를 얻으며, 난이도에 따라 단어의 길이와 떨어지는 속도가 조절됩니다. 특수 단어(빨간색 등)는 보너스 점수나 시간 추가 등의 효과를 제공하며, 일정 시간/점수마다 난이도가 상승하는 시스템도 구현해주세요."},
    {"description": "물리 엔진 기반의 미니 골프 게임을 만들어주세요. 플레이어는 마우스 드래그로 공을 치는 방향과 세기를 조절하여 홀에 공을 넣어야 합니다. 다양한 장애물(모래 함정, 물웅덩이, 경사로 등)이 있는 여러 개의 코스를 구현하고, 각 홀마다 타수를 기록하여 최종 점수를 계산합니다. 바람 방향/세기 같은 환경 요소와 함께 궤적 미리보기 기능도 추가해주세요."},
    {"description": "플레이어가 낚시를 즐기는 시뮬레이션 게임을 개발해주세요. 마우스 클릭으로 낚싯줄을 던지고, 물고기가 물면 타이밍 맞추기 미니게임으로 물고기를 낚아야 합니다. 다양한 종류의 물고기(희귀도별 점수 차등)를 구현하고, 낚은 물고기에 따라 골드를 획득하여 더 좋은 낚싯대, 미끼 등을 구매할 수 있는 업그레이드 시스템을 추가해주세요. 시간대나 날씨에 따라 출현하는 물고기가 달라지는 기능도 구현해주세요."},
    {"description": "1인용 또는 AI 대전 빙고 게임을 만들어주세요. 5x5 그리드에 1-25 숫자를 무작위로 배치하고, 번갈아가며 숫자를 선택하여 해당 칸을 마킹합니다. 가로, 세로, 대각선으로 5개의 연속된 마킹이 완성되면 빙고가 되며, 먼저 3빙고를 달성하는 쪽이 승리합니다. 컴퓨터 AI는 랜덤하게 또는 전략적으로(빙고에 가까운 라인 우선) 숫자를 선택하도록 구현하고, 타이머와 승/패 기록 시스템도 추가해주세요."},
    {"description": "화면 하단에서 상단으로 노트가 올라오면 정확한 타이밍에 키를 눌러 점수를 얻는 리듬 게임을 개발해주세요. 4개의 레인(D, F, J, K 키)에 노트가 등장하며, 타이밍 정확도에 따라 Perfect, Good, Miss 등급이 표시됩니다. 배경 음악에 맞춰 노트가 생성되며, 연속 성공 시 콤보 시스템으로 추가 점수를 제공합니다. 난이도 선택(노트 속도와 밀도 조절)과 함께 최종 결과 화면(정확도, 콤보, 등급)도 구현해주세요."},
    {"description": "탑다운 뷰의 2D 레이싱 게임을 만들어주세요. 플레이어는 방향키로 자동차를 조종하여 트랙을 따라 주행하며, 트랙 이탈 시 감속되는 메커니즘을 구현합니다. 여러 AI 경쟁자들과 경쟁하며 3바퀴를 가장 빨리 완주하는 게임 모드와 함께, 시간 제한 내에 체크포인트를 통과하는 타임 어택 모드도 구현해주세요. 다양한 차량 선택지(속도와 핸들링 특성 차등)와 부스트 아이템, 장애물 등도 추가해주세요."},
    {"description": "다양한 카테고리의 퀴즈를 풀어나가는 게임을 개발해주세요. 주어진 질문에 4개의 보기 중 정답을 선택하는 방식으로, 정답 시 점수를 획득하고 오답 시 생명이 감소합니다. 30초 제한 시간 내에 답을 선택해야 하며, 난이도에 따라 질문의 복잡도와 제한 시간이 조절됩니다. 50:50 힌트(오답 2개 제거), 시간 추가 등의 도움 아이템과 함께 최종 결과 요약(정답률, 카테고리별 성적)도 구현해주세요."},
    {"description": "움직이는 표적을 맞추는 사격 갤러리 게임을 만들어주세요. 마우스 클릭으로 발사하며, 다양한 속도와 패턴으로 움직이는 표적(오리, 병, 풍선 등)을 맞추면 점수를 획득합니다. 제한된 시간과 총알 수 안에 최대한 많은 점수를 얻는 것이 목표이며, 특수 표적(황금 표적 등)은 보너스 점수나 추가 시간/총알을 제공합니다. 연속 명중 시 점수 배율이 증가하는 콤보 시스템과 함께 다양한 난이도 레벨(표적 속도/수 증가)도 구현해주세요."},
    {"description": "가상 주사위를 굴려 보드판을 돌아다니는 보드 게임을 개발해주세요. 플레이어는 차례대로 1-6 주사위를 굴려 말을 이동시키며, 도착한 칸에 따라 다양한 이벤트(앞으로/뒤로 이동, 한 턴 쉬기, 미니게임 등)가 발생합니다. 특수 아이템(추가 주사위, 이벤트 회피 등)을 수집하고 사용할 수 있으며, 먼저 결승점에 도달하거나 가장 많은 포인트를 모은 플레이어가 승리합니다. 1-4명의 로컬 멀티플레이어를 지원하며, AI 플레이어도 구현해주세요."},
    {"description": "탑다운 뷰의 좀비 서바이벌 게임을 만들어주세요. WASD로 이동하고 마우스로 조준/발사하며, 끊임없이 몰려오는 좀비 웨이브를 최대한 오래 생존하는 것이 목표입니다. 다양한 무기(권총, 샷건, 기관총 등)와 제한된 탄약, 그리고 체력 회복 아이템과 폭탄 같은 특수 아이템을 맵에서 획득할 수 있습니다. 시간이 지날수록 좀비의 수와 속도가 증가하며, 특수 좀비(탱커, 러너 등)도 등장하는 난이도 시스템을 구현해주세요."},
    {"description": "축구 페널티킥 게임을 개발해주세요. 공격 시에는 방향과 파워를 조절하여 슛을 날리고, 수비 시에는 골키퍼를 좌/중앙/우 중 한 방향으로 다이빙시켜 공을 막아야 합니다. 5번의 키커-골키퍼 대결 후 더 많은 골을 넣은 쪽이 승리하며, 동점일 경우 서든데스로 승부를 가립니다. 슛의 정확도와 파워에 따라 결과가 달라지며, 골키퍼 AI는 패턴 학습을 통해 플레이어의 경향성을 파악하도록 구현해주세요. 1인 플레이와 2인 로컬 대전 모드를 모두 지원해주세요."},
    {"description": "클래식한 지뢰찾기 게임을 구현해주세요. NxN 크기의 그리드에 M개의 지뢰가 무작위로 배치되며, 플레이어는 좌클릭으로 칸을 열고 우클릭으로 지뢰 위치에 깃발을 표시합니다. 열린 칸에는 주변 8칸의 지뢰 수가 표시되며, 주변에 지뢰가 없는 칸을 열면 연쇄적으로 주변 칸들이 열립니다. 지뢰가 있는 칸을 열면 게임 오버, 지뢰가 아닌 모든 칸을 열면 승리입니다. 난이도 설정(쉬움: 9x9/10개, 중간: 16x16/40개, 어려움: 30x16/99개)과 함께 첫 클릭은 항상 안전하도록 구현해주세요."},
    {"description": "두 플레이어가 번갈아가며 7x6 그리드에 색깔 디스크를 떨어뜨려 가로, 세로, 대각선으로 4개의 연속된 디스크를 만드는 Connect Four 게임을 개발해주세요. 플레이어는 열을 클릭하여 디스크를 해당 열의 가장 아래 빈 칸에 배치합니다. 4개의 연속된 디스크를 먼저 만드는 플레이어가 승리하며, 모든 칸이 차면 무승부입니다. 1인 플레이(AI 대전)과 2인 로컬 대전 모드를 구현하고, AI는 최소한 1단계 앞을 내다보는 논리로 작동하도록 해주세요."},
    {"description": "글자 타일을 배치하여 단어를 만드는 스크래블 스타일의 단어 게임을 만들어주세요. 각 플레이어는 7개의 글자 타일을 받고, 이를 보드에 배치하여 가로나 세로로 단어를 형성합니다. 새 단어는 기존 단어와 반드시 연결되어야 하며, 각 타일에는 점수가 있어 단어의 총점이 계산됩니다. 특수 칸(2배 글자 점수, 3배 단어 점수 등)을 활용한 전략적 배치가 가능하며, 사전 검증 기능으로 유효한 단어만 허용합니다. 1-4인 로컬 멀티플레이어와 AI 대전을 지원해주세요."},
    {"description": "2D 환경에서 진행되는 탱크 전투 게임을 개발해주세요. 플레이어는 WASD로 탱크를 조종하고, 마우스로 포탑을 조준하여 클릭으로 발사합니다. 파괴 가능한 지형(벽돌, 나무 등)과 파괴 불가능한 장애물(강철, 물 등)이 있는 맵에서 적 탱크들과 전투를 벌입니다. 다양한 무기(기본 포탄, 확산탄, 레이저 등)와 아이템(속도 증가, 방어력 강화, 추가 생명 등)을 구현하고, 스테이지별로 증가하는 적 AI 난이도와 보스 전투도 추가해주세요."},
    {"description": "3개 이상의 같은 보석을 맞추어 제거하는 퍼즐 게임을 만들어주세요. 인접한 두 보석을 스왑하여 매치를 만들며, 매치된 보석이 사라지면 위의 보석들이 떨어지고 새 보석이 채워집니다. 4개 이상 매치 시 특수 보석(가로/세로 폭발, 주변 9칸 폭발 등)이 생성되며, 연쇄 매치가 발생하면 콤보 점수가 추가됩니다. 제한 시간 또는 제한 이동 횟수 내에 목표 점수를 달성하는 레벨 기반 진행 구조와 함께, 특수 미션(특정 색상 N개 제거, 장애물 파괴 등)도 구현해주세요."},
    {"description": "단일 타워가 끊임없이 몰려오는 적들을 격퇴하는 타워 디펜스 게임을 개발해주세요. 화면 중앙의 타워는 자동으로 가장 가까운 적을 향해 발사하며, 플레이어는 웨이브 사이에 획득한 자원으로 타워를 업그레이드(공격력, 공격 속도, 범위 등)할 수 있습니다. 시간이 지날수록 더 강력하고 다양한 적(빠른 적, 방어력 높은 적, 분열하는 적 등)이 등장하며, 타워의 체력이 0이 되면 게임 오버입니다. 특수 능력(범위 공격, 일시 정지, 즉시 회복 등)과 함께 생존한 웨이브 수에 따른 랭킹 시스템도 구현해주세요."},
    {"description": "캐릭터가 끝없이 달리며 좀비와 장애물을 피하는 사이드 스크롤링 러너 게임을 만들어주세요. 스페이스바로 점프, S키로 슬라이딩하여 다양한 장애물(웅덩이, 장벽, 좀비 무리 등)을 피해야 합니다. 코인과 파워업(일시적 무적, 자석 효과, 속도 감소 등)을 수집하며, 특정 구간마다 미니 보스 좀비와의 간단한 전투도 포함됩니다. 거리에 따라 점수가 증가하고, 코인으로 캐릭터 업그레이드(더블 점프, 체력 증가 등)를 구매할 수 있는 시스템도 구현해주세요."},
    {"description": "탑다운 뷰의 간단한 액션 RPG 게임을 개발해주세요. WASD로 이동하고, 마우스 클릭으로 기본 공격, 1-4 키로 특수 스킬을 사용합니다. 플레이어는 몬스터를 처치하며 경험치와 아이템을 획득하고, 레벨업 시 능력치(공격력, 체력, 속도 등)를 향상시킵니다. 다양한 무기와 방어구를 착용할 수 있으며, 스킬 트리 시스템으로 캐릭터를 특화시킬 수 있습니다. 여러 지역과 보스 몬스터, 간단한 퀘스트 시스템도 구현해주세요."},
]


SystemPrompt = """
# GameCraft 시스템 프롬프트

## 1. 기본 정보 및 역할
당신의 이름은 'GameCraft'입니다. 당신은 게임플레이 메커니즘, 인터랙티브 디자인, 성능 최적화에 뛰어난 웹 게임 개발 전문가입니다. HTML, JavaScript, CSS를 활용하여 간결하고 효율적인 웹 기반 게임을 제작하는 것이 당신의 임무입니다.

## 2. 핵심 기술 스택
- **프론트엔드**: HTML5, CSS3, JavaScript(ES6+)
- **렌더링 방식**: 브라우저에서 직접 렌더링 가능한 코드 생성
- **코드 스타일**: 바닐라 JavaScript 우선, 외부 라이브러리 최소화

## 3. 게임 유형별 특화 지침
### 3.1 아케이드/액션 게임
- 간결한 충돌 감지 시스템 구현
- 키보드/터치 입력 최적화
- 기본적인 점수 시스템

### 3.2 퍼즐 게임
- 명확한 게임 규칙 및 승리 조건
- 기본 난이도 구현
- 핵심 게임 메커니즘에 집중

### 3.3 카드/보드 게임
- 간소화된 턴 기반 시스템
- 기본 게임 규칙 자동화
- 핵심 게임 로직 중심

### 3.4 시뮬레이션 게임
- 효율적인 상태 관리
- 가장 중요한 상호작용 구현
- 핵심 요소만 포함

## 4. 이모지 활용 지침 🎮
- 게임 UI 요소에 이모지 활용 (예: 생명력 ❤️, 코인 💰, 시간 ⏱️)
- 이모지 사용은 핵심 요소에만 집중

## 5. 기술적 구현 가이드라인
### 5.1 코드 구조
- **간결성 중시**: 코드는 최대한 간결하게 작성하고, 주석은 최소화
- **모듈화**: 코드 기능별로 분리하되 불필요한 추상화 지양
- **최적화**: 게임 루프와 렌더링 최적화에 집중
- **코드 크기 제한**: 전체 코드는 200줄을 넘지 않도록 함

### 5.2 성능 최적화
- DOM 조작 최소화
- 불필요한 변수와 함수 제거
- 메모리 관리에 주의

### 5.3 반응형 디자인
- 기본적인 반응형 지원
- 핵심 기능에 집중한 심플한 UI

## 6. 외부 라이브러리
- 라이브러리 사용은 최소화하고, 필요한 경우에만 사용
- 라이브러리 사용 시 CDN으로 가져올 것

## 7. 접근성 및 포용성
- 핵심 접근성 기능에만 집중

## 8. 제약사항 및 유의사항
- 외부 API 호출 금지
- 코드 크기 최소화에 우선순위 (200줄 이내)
- 주석 최소화 - 필수적인 설명만 포함
- 불필요한 기능 구현 지양

## 9. 출력 형식
- HTML 코드 블록으로만 코드 반환
- 추가 설명 없이 즉시 실행 가능한 코드만 제공
- 모든 코드는 단일 HTML 파일에 인라인으로 포함

## 10. 코드 품질 기준
- 효율성과 간결함이 최우선
- 핵심 게임플레이 메커니즘에만 집중
- 복잡한 기능보다 작동하는 기본 기능 우선
- 불필요한 주석이나 장황한 코드 지양
- 단일 파일에 모든 코드 포함
- 코드 길이 제한: 완성된 게임 코드는 200줄 이내로 작성

## 11. 중요: 코드 생성 제한
- 게임 코드는 반드시 200줄 이내로 제한
- 불필요한 설명이나 주석 제외
- 핵심 기능만 구현하고 부가 기능은 생략
- 코드 크기가 커질 경우 기능을 간소화하거나 생략할 것
"""


# ------------------------
# 2) 공통 상수, 함수, 클래스
# ------------------------

class Role:
    SYSTEM = "system"
    USER = "user"
    ASSISTANT = "assistant"

History = List[Tuple[str, str]]
Messages = List[Dict[str, str]]

IMAGE_CACHE = {}

def get_image_base64(image_path):
    """
    이미지 파일을 base64로 읽어서 캐싱
    """
    if image_path in IMAGE_CACHE:
        return IMAGE_CACHE[image_path]
    try:
        with open(image_path, "rb") as image_file:
            encoded_string = base64.b64encode(image_file.read()).decode()
            IMAGE_CACHE[image_path] = encoded_string
            return encoded_string
    except:
        return IMAGE_CACHE.get('default.png', '')

def history_to_messages(history: History, system: str) -> Messages:
    messages = [{'role': Role.SYSTEM, 'content': system}]
    for h in history:
        messages.append({'role': Role.USER, 'content': h[0]})
        messages.append({'role': Role.ASSISTANT, 'content': h[1]})
    return messages

def messages_to_history(messages: Messages) -> History:
    assert messages[0]['role'] == Role.SYSTEM
    history = []
    for q, r in zip(messages[1::2], messages[2::2]):
        history.append([q['content'], r['content']])
    return history


# ------------------------
# 3) API 연동 설정
# ------------------------

YOUR_ANTHROPIC_TOKEN = os.getenv('ANTHROPIC_API_KEY', '').strip()
YOUR_OPENAI_TOKEN    = os.getenv('OPENAI_API_KEY',     '').strip()

claude_client = anthropic.Anthropic(api_key=YOUR_ANTHROPIC_TOKEN)
openai_client = openai.OpenAI(api_key=YOUR_OPENAI_TOKEN)

async def try_claude_api(system_message, claude_messages, timeout=15):
    """
    Claude API 호출 (스트리밍)
    """
    try:
        system_message_with_limit = system_message + "\n\n추가 중요 지침: 생성하는 코드는 절대로 200줄을 넘지 마세요. 코드 간결성이 최우선입니다. 주석을 최소화하고, 핵심 기능만 구현하세요."
        
        start_time = time.time()
        with claude_client.messages.stream(
            model="claude-3-7-sonnet-20250219",
            max_tokens=19800,
            system=system_message_with_limit,
            messages=claude_messages,
            temperature=0.3,
        ) as stream:
            collected_content = ""
            for chunk in stream:
                current_time = time.time()
                if current_time - start_time > timeout:
                    raise TimeoutError("Claude API timeout")
                if chunk.type == "content_block_delta":
                    collected_content += chunk.delta.text
                    yield collected_content
                    await asyncio.sleep(0)
                start_time = current_time
    except Exception as e:
        raise e

async def try_openai_api(openai_messages):
    """
    OpenAI API 호출 (스트리밍) - 코드 길이 제한 강화
    """
    try:
        if openai_messages and openai_messages[0]["role"] == "system":
            openai_messages[0]["content"] += "\n\n추가 중요 지침: 생성하는 코드는 절대로 200줄을 넘지 마세요. 코드 간결성이 최우선입니다. 주석은 최소화하고, 핵심 기능만 구현하세요."

        stream = openai_client.chat.completions.create(
            model="o3",
            messages=openai_messages,
            stream=True,
            max_tokens=19800,
            temperature=0.2
        )
        collected_content = ""
        for chunk in stream:
            if chunk.choices[0].delta.content is not None:
                collected_content += chunk.choices[0].delta.content
                yield collected_content
    except Exception as e:
        raise e


# ------------------------
# 4) 템플릿(하나로 통합)
# ------------------------

def load_json_data():
    data_list = []
    for item in DEMO_LIST:
        data_list.append({
            "name": f"[게임] {item['description'][:20]}...",
            "prompt": item['description']
        })
    return data_list

def create_template_html(title, items):
    """
    이미지 없이 템플릿 HTML 생성
    """
    html_content = r"""
<style>
.prompt-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 16px;
    padding: 12px;
}
.prompt-card {
    background: white;
    border: 1px solid #eee;
    border-radius: 12px;
    padding: 12px;
    cursor: pointer;
    box-shadow: 0 4px 8px rgba(0,0,0,0.05);
    transition: all 0.3s ease;
}
.prompt-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 6px 12px rgba(0,0,0,0.1);
}
.card-name {
    font-weight: bold;
    margin-bottom: 8px;
    font-size: 13px;
    color: #444;
}
.card-prompt {
    font-size: 11px;
    line-height: 1.4;
    color: #666;
    display: -webkit-box;
    -webkit-line-clamp: 7;
    -webkit-box-orient: vertical;
    overflow: hidden;
    height: 84px;
    background-color: #f8f9fa;
    padding: 8px;
    border-radius: 6px;
}
</style>
<div class="prompt-grid">
"""
    import html as html_lib
    for item in items:
        card_html = f"""
<div class="prompt-card" onclick="copyToInput(this)" data-prompt="{html_lib.escape(item.get('prompt', ''))}">
    <div class="card-name">{html_lib.escape(item.get('name', ''))}</div>
    <div class="card-prompt">{html_lib.escape(item.get('prompt', ''))}</div>
</div>
"""
        html_content += card_html
    html_content += r"""
<script>
function copyToInput(card) {
    const prompt = card.dataset.prompt;
    const textarea = document.querySelector('.ant-input-textarea-large textarea');
    if (textarea) {
        textarea.value = prompt;
        textarea.dispatchEvent(new Event('input', { bubbles: true }));
        // 템플릿 Drawer 닫기
        document.querySelector('.session-drawer .close-btn').click();
    }
}
</script>
</div>
"""
    return gr.HTML(value=html_content)

def load_all_templates():
    return create_template_html("🎮 모든 게임 템플릿", load_json_data())


# ------------------------
# 5) 배포/부스트/기타 유틸
# ------------------------

def remove_code_block(text):
    pattern = r'```html\s*([\s\S]+?)\s*```'
    match = re.search(pattern, text, re.DOTALL)
    if match:
        return match.group(1).strip()
    
    pattern = r'```(?:\w+)?\s*([\s\S]+?)\s*```'
    match = re.search(pattern, text, re.DOTALL)
    if match:
        return match.group(1).strip()
    
    text = re.sub(r'```html\s*', '', text)
    text = re.sub(r'\s*```', '', text)
    return text.strip()

def optimize_code(code: str) -> str:
    if not code or len(code.strip()) == 0:
        return code
    
    lines = code.split('\n')
    if len(lines) <= 200:
        return code
    
    comment_patterns = [
        r'/\*[\s\S]*?\*/',
        r'//.*?$',
        r'<!--[\s\S]*?-->'
    ]
    cleaned_code = code
    for pattern in comment_patterns:
        cleaned_code = re.sub(pattern, '', cleaned_code, flags=re.MULTILINE)
    
    cleaned_lines = []
    empty_line_count = 0
    for line in cleaned_code.split('\n'):
        if line.strip() == '':
            empty_line_count += 1
            if empty_line_count <= 1:
                cleaned_lines.append('')
        else:
            empty_line_count = 0
            cleaned_lines.append(line)
    
    cleaned_code = '\n'.join(cleaned_lines)
    cleaned_code = re.sub(r'console\.log\(.*?\);', '', cleaned_code, flags=re.MULTILINE)
    cleaned_code = re.sub(r' {2,}', ' ', cleaned_code)
    return cleaned_code

def send_to_sandbox(code):
    clean_code = remove_code_block(code)
    clean_code = optimize_code(clean_code)
    
    if clean_code.startswith('```html'):
        clean_code = clean_code[7:].strip()
    if clean_code.endswith('```'):
        clean_code = clean_code[:-3].strip()
    
    if not clean_code.strip().startswith('<!DOCTYPE') and not clean_code.strip().startswith('<html'):
        clean_code = f"""<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Game Preview</title>
</head>
<body>
{clean_code}
</body>
</html>"""
    encoded_html = base64.b64encode(clean_code.encode('utf-8')).decode('utf-8')
    data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
    return f'<iframe src="{data_uri}" width="100%" height="920px" style="border:none;"></iframe>'

def boost_prompt(prompt: str) -> str:
    if not prompt:
        return ""
    boost_system_prompt = """당신은 웹 게임 개발 프롬프트 전문가입니다.
주어진 프롬프트를 분석하여 더 명확하고 간결한 요구사항으로 변환하되,
원래 의도와 목적은 그대로 유지하면서 다음 관점들을 고려하여 증강하십시오:

1. 게임플레이 핵심 메커니즘 명확히 정의
2. 필수적인 상호작용 요소만 포함
3. 핵심 UI 요소 간략히 기술
4. 코드 간결성 유지를 위한 우선순위 설정
5. 기본적인 게임 규칙과 승리/패배 조건 명시

다음 중요 지침을 반드시 준수하세요:
- 불필요한 세부 사항이나 부가 기능은 제외
- 생성될 코드가 600줄을 넘지 않도록 기능을 제한
- 명확하고 간결한 언어로 요구사항 작성
- 최소한의 필수 게임 요소만 포함
"""
    try:
        try:
            response = claude_client.messages.create(
                model="claude-3-7-sonnet-20250219",
                max_tokens=10000,
                temperature=0.3,
                messages=[{
                    "role": "user",
                    "content": f"다음 게임 프롬프트를 분석하고 증강하되, 간결함을 유지하세요: {prompt}"
                }],
                system=boost_system_prompt
            )
            if hasattr(response, 'content') and len(response.content) > 0:
                return response.content[0].text
            raise Exception("Claude API 응답 형식 오류")
        except Exception:
            completion = openai_client.chat.completions.create(
                model="gpt-4",
                messages=[
                    {"role": "system", "content": boost_system_prompt},
                    {"role": "user", "content": f"다음 게임 프롬프트를 분석하고 증강하되, 간결함을 유지하세요: {prompt}"}
                ],
                max_tokens=10000,
                temperature=0.3
            )
            if completion.choices and len(completion.choices) > 0:
                return completion.choices[0].message.content
            raise Exception("OpenAI API 응답 형식 오류")
    except Exception:
        return prompt

def handle_boost(prompt: str):
    try:
        boosted_prompt = boost_prompt(prompt)
        return boosted_prompt, gr.update(active_key="empty")
    except Exception:
        return prompt, gr.update(active_key="empty")

def history_render(history: History):
    return gr.update(open=True), history

def execute_code(query: str):
    if not query or query.strip() == '':
        return None, gr.update(active_key="empty")
    try:
        clean_code = remove_code_block(query)
        if clean_code.startswith('```html'):
            clean_code = clean_code[7:].strip()
        if clean_code.endswith('```'):
            clean_code = clean_code[:-3].strip()
        if not clean_code.strip().startswith('<!DOCTYPE') and not clean_code.strip().startswith('<html'):
            if not ('<body' in clean_code and '</body>' in clean_code):
                clean_code = f"""<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Game Preview</title>
</head>
<body>
{clean_code}
</body>
</html>"""
        return send_to_sandbox(clean_code), gr.update(active_key="render")
    except Exception as e:
        print(f"Execute code error: {str(e)}")
        return None, gr.update(active_key="empty")


# ------------------------
# 6) 데모 클래스
# ------------------------

class Demo:
    def __init__(self):
        pass
        
    async def generation_code(self, query: Optional[str], _setting: Dict[str, str], _history: Optional[History]):
        if not query or query.strip() == '':
            query = random.choice(DEMO_LIST)['description']
        
        if _history is None:
            _history = []
        
        query = f"""
다음 게임을 제작해주세요. 
중요 요구사항: 
1. 코드는 가능한 한 간결하게 작성할 것
2. 불필요한 주석이나 설명은 제외할 것
3. 코드는 600줄을 넘지 않을 것
4. 모든 코드는 하나의 HTML 파일에 통합할 것
5. 핵심 기능만 구현하고 부가 기능은 생략할 것
게임 요청: {query}
"""
        
        messages = history_to_messages(_history, _setting['system'])
        system_message = messages[0]['content']
        
        claude_messages = [
            {"role": msg["role"] if msg["role"] != "system" else "user", "content": msg["content"]}
            for msg in messages[1:] + [{'role': Role.USER, 'content': query}]
            if msg["content"].strip() != ''
        ]
        
        openai_messages = [{"role": "system", "content": system_message}]
        for msg in messages[1:]:
            openai_messages.append({
                "role": msg["role"],
                "content": msg["content"]
            })
        openai_messages.append({"role": "user", "content": query})
        
        try:
            yield [
                "Generating code...",
                _history,
                None,
                gr.update(active_key="loading"),
                gr.update(open=True)
            ]
            await asyncio.sleep(0)
            collected_content = None
            try:
                async for content in try_claude_api(system_message, claude_messages):
                    yield [
                        content,
                        _history,
                        None,
                        gr.update(active_key="loading"),
                        gr.update(open=True)
                    ]
                    await asyncio.sleep(0)
                collected_content = content
            except Exception:
                async for content in try_openai_api(openai_messages):
                    yield [
                        content,
                        _history,
                        None,
                        gr.update(active_key="loading"),
                        gr.update(open=True)
                    ]
                    await asyncio.sleep(0)
                collected_content = content

            if collected_content:
                clean_code = remove_code_block(collected_content)
                code_lines = clean_code.count('\n') + 1
                if code_lines > 700:
                    warning_msg = f"""
⚠️ **경고: 생성된 코드가 너무 깁니다 ({code_lines}줄)**
이로 인해 실행 시 오류가 발생할 수 있습니다. 다음과 같이 시도해 보세요:
1. 더 간단한 게임을 요청하세요
2. 특정 기능만 명시하여 요청하세요 (예: "간단한 Snake 게임, 점수 시스템 없이")
3. "코드" 버튼을 사용하여 직접 실행해 보세요
```html
{clean_code[:2000]}
... (코드가 너무 깁니다) ... """
                    collected_content = warning_msg
                    yield [
                        collected_content,
                        _history,
                        None,
                        gr.update(active_key="empty"),
                        gr.update(open=True)
                    ]
                else:
                    _history = messages_to_history([
                        {'role': Role.SYSTEM, 'content': system_message}
                    ] + claude_messages + [{
                        'role': Role.ASSISTANT,
                        'content': collected_content
                    }])
                    yield [
                        collected_content,
                        _history,
                        send_to_sandbox(clean_code),
                        gr.update(active_key="render"),
                        gr.update(open=True)
                    ]
            else:
                raise ValueError("No content was generated from either API")
        except Exception as e:
            raise ValueError(f'Error calling APIs: {str(e)}')
            
    def clear_history(self):
        return []


####################################################
# 1) deploy_to_vercel 함수
####################################################
def deploy_to_vercel(code: str):
    print(f"[DEBUG] deploy_to_vercel() 시작. code 길이: {len(code) if code else 0}")
    try:
        if not code or len(code.strip()) < 10:
            print("[DEBUG] 배포 불가: code가 짧음")
            return "No code to deploy."

        token = "A8IFZmgW2cqA4yUNlLPnci0N"
        if not token:
            print("[DEBUG] Vercel 토큰이 없음.")
            return "Vercel token is not set."

        project_name = ''.join(random.choice(string.ascii_lowercase) for _ in range(6))
        print(f"[DEBUG] 생성된 project_name: {project_name}")

        deploy_url = "https://api.vercel.com/v13/deployments"
        headers = {
            "Authorization": f"Bearer {token}",
            "Content-Type": "application/json"
        }

        package_json = {
            "name": project_name,
            "version": "1.0.0",
            "private": True,
            "dependencies": {"vite": "^5.0.0"},
            "scripts": {
                "dev": "vite",
                "build": "echo 'No build needed' && mkdir -p dist && cp index.html dist/",
                "preview": "vite preview"
            }
        }

        files = [
            {"file": "index.html", "data": code},
            {"file": "package.json", "data": json.dumps(package_json, indent=2)}
        ]
        project_settings = {
            "buildCommand": "npm run build",
            "outputDirectory": "dist",
            "installCommand": "npm install",
            "framework": None
        }

        deploy_data = {
            "name": project_name,
            "files": files,
            "target": "production",
            "projectSettings": project_settings
        }

        print("[DEBUG] Vercel API 요청 전송중...")
        deploy_response = requests.post(deploy_url, headers=headers, json=deploy_data)
        print("[DEBUG] 응답 status_code:", deploy_response.status_code)

        if deploy_response.status_code != 200:
            print("[DEBUG] 배포 실패:", deploy_response.text)
            return f"Deployment failed: {deploy_response.text}"

        deployment_url = f"https://{project_name}.vercel.app"
        print(f"[DEBUG] 배포 성공 -> URL: {deployment_url}")
        time.sleep(5)


    # Markdown 링크로 반환
        return f"""
        ✅ **Deployment complete!**  
        Your app is live at:  
        [**{deployment_url}**]({deployment_url})
        """    


    
    except Exception as e:
        print("[ERROR] deploy_to_vercel() 예외:", e)
        return f"Error during deployment: {str(e)}"



# ------------------------
# (3) handle_deploy_legacy
# ------------------------
def handle_deploy_legacy(code):
    logger.debug(f"[handle_deploy_legacy] code 길이: {len(code) if code else 0}")
    if not code or len(code.strip()) < 10:
        logger.info("[handle_deploy_legacy] 코드가 짧음.")
        return "<div style='color:red;'>배포할 코드가 없습니다.</div>"

    clean_code = remove_code_block(code)
    logger.debug(f"[handle_deploy_legacy] remove_code_block 후 길이: {len(clean_code)}")

    result_html = deploy_to_vercel(clean_code)
    logger.debug(f"[handle_deploy_legacy] 배포 결과 HTML 길이: {len(result_html)}")

    encoded_html = base64.b64encode(result_html.encode('utf-8')).decode()
    data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"

    iframe_html = f"""
    <iframe src="{data_uri}"
            style="width:100%; height:600px; border:none;"
            sandbox="allow-scripts allow-same-origin allow-popups">
    </iframe>
    """
    logger.debug("[handle_deploy_legacy] iframe_html 반환")
    return iframe_html


# ------------------------
# 8) Gradio / Modelscope UI 빌드
# ------------------------
demo_instance = Demo()
theme = gr.themes.Soft(
    primary_hue="blue",
    secondary_hue="purple",
    neutral_hue="slate",
    spacing_size=gr.themes.sizes.spacing_md,
    radius_size=gr.themes.sizes.radius_md,
    text_size=gr.themes.sizes.text_md,
)

with gr.Blocks(css_paths=["app.css"], theme=theme) as demo:
    header_html = gr.HTML("""
    <div class="app-header">
        <h1>🎮 Vibe Game Craft</h1>
        <p>설명을 입력하면 웹 기반 HTML5, JavaScript, CSS 게임을 생성합니다. 실시간 미리보기와 배포 기능도 지원됩니다.</p>
    </div>
    <!-- 배포 결과 박스 - 헤더 바로 아래 위치 -->
    <div id="deploy-banner" style="display:none;" class="deploy-banner">
        <!-- (생략) ... 배너 스타일/스크립트 ... -->
    </div>
    <style>
    /* (생략) ... CSS ... */
    </style>
    <script>
    /* (생략) ... JS copyBannerUrl / showDeployBanner ... */
    </script>
    """)

    history = gr.State([])
    setting = gr.State({"system": SystemPrompt})
    deploy_status = gr.State({"is_deployed": False,"status": "","url": "","message": ""})

    with ms.Application() as app:
        with antd.ConfigProvider():

            with antd.Drawer(open=False, title="코드 보기", placement="left", width="750px") as code_drawer:
                code_output = legacy.Markdown()

            with antd.Drawer(open=False, title="히스토리", placement="left", width="900px") as history_drawer:
                history_output = legacy.Chatbot(show_label=False, flushing=False, height=960, elem_classes="history_chatbot")

            with antd.Drawer(
                open=False,
                title="게임 템플릿",
                placement="right",
                width="900px",
                elem_classes="session-drawer"
            ) as session_drawer:
                with antd.Flex(vertical=True, gap="middle"):
                    gr.Markdown("### 사용 가능한 게임 템플릿")
                    session_history = gr.HTML(elem_classes="session-history")
                    close_btn = antd.Button("닫기", type="default", elem_classes="close-btn")

            with antd.Row(gutter=[32, 12], align="top", elem_classes="equal-height-container") as layout:

                # 왼쪽 Col
                with antd.Col(span=24, md=16, elem_classes="equal-height-col"):
                    with ms.Div(elem_classes="right_panel panel"):
                        gr.HTML(r"""
<div class="render_header">
    <span class="header_btn"></span><span class="header_btn"></span><span class="header_btn"></span>
</div>
""")
                        with antd.Tabs(active_key="empty", render_tab_bar="() => null") as state_tab:
                            with antd.Tabs.Item(key="empty"):
                                empty = antd.Empty(description="게임을 만들려면 설명을 입력하세요", elem_classes="right_content")
                            with antd.Tabs.Item(key="loading"):
                                loading = antd.Spin(True, tip="게임 코드 생성 중...", size="large", elem_classes="right_content")
                            with antd.Tabs.Item(key="render"):
                                sandbox = gr.HTML(elem_classes="html_content")

                # 오른쪽 Col
                with antd.Col(span=24, md=8, elem_classes="equal-height-col"):
                    with antd.Flex(vertical=True, gap="small", elem_classes="right-top-buttons"):
                        with antd.Flex(gap="small", elem_classes="setting-buttons", justify="space-between"):
                            codeBtn = antd.Button("🧑‍💻 코드 보기", type="default", elem_classes="code-btn")
                            historyBtn = antd.Button("📜 히스토리", type="default", elem_classes="history-btn")
                            template_btn = antd.Button("🎮 템플릿", type="default", elem_classes="template-btn")
                        
                        with antd.Flex(gap="small", justify="space-between", elem_classes="action-buttons"):
                            btn = antd.Button("전송", type="primary", size="large", elem_classes="send-btn")
                            boost_btn = antd.Button("증강", type="default", size="large", elem_classes="boost-btn")
                            execute_btn = antd.Button("코드", type="default", size="large", elem_classes="execute-btn")
                            deploy_btn = antd.Button("배포", type="default", size="large", elem_classes="deploy-btn")
                            clear_btn = antd.Button("클리어", type="default", size="large", elem_classes="clear-btn")

                    with antd.Flex(vertical=True, gap="middle", wrap=True, elem_classes="input-panel"):
                        input_text = antd.InputTextarea(
                            size="large",
                            allow_clear=True,
                            placeholder=random.choice(DEMO_LIST)['description'],
                            max_length=100000
                        )
                        gr.HTML('<div class="help-text">💡 원하는 게임의 설명을 입력하세요. 예: "테트리스 게임 제작해줘."</div>')

                        # Markdown으로 배포 결과 표시
                        deploy_result_container = gr.Markdown(
                            value="아직 배포된 게임이 없습니다.",
                            label="Deployment Result"
                        )

            # Code Drawer 열기/닫기
            codeBtn.click(lambda: gr.update(open=True), inputs=[], outputs=[code_drawer])
            code_drawer.close(lambda: gr.update(open=False), inputs=[], outputs=[code_drawer])

            # History Drawer 열기/닫기
            historyBtn.click(history_render, inputs=[history], outputs=[history_drawer, history_output])
            history_drawer.close(lambda: gr.update(open=False), inputs=[], outputs=[history_drawer])

            # Template Drawer
            template_btn.click(
                fn=lambda: (gr.update(open=True), load_all_templates()),
                outputs=[session_drawer, session_history],
                queue=False
            )
            session_drawer.close(lambda: (gr.update(open=False), gr.HTML("")), outputs=[session_drawer, session_history])
            close_btn.click(lambda: (gr.update(open=False), gr.HTML("")), outputs=[session_drawer, session_history])

            # 전송 버튼
            btn.click(
                demo_instance.generation_code,
                inputs=[input_text, setting, history],
                outputs=[code_output, history, sandbox, state_tab, code_drawer]
            )

            # 클리어 버튼
            clear_btn.click(demo_instance.clear_history, inputs=[], outputs=[history])

            # 증강 버튼
            boost_btn.click(
                fn=handle_boost,
                inputs=[input_text],
                outputs=[input_text, state_tab]
            )

            # 코드 실행 버튼
            execute_btn.click(
                fn=execute_code,
                inputs=[input_text],
                outputs=[sandbox, state_tab]
            )

            # 배포 버튼 → deploy_result_container (Markdown)
            deploy_btn.click(
                fn=lambda code: deploy_to_vercel(remove_code_block(code)) if code else "No code generated.",
                inputs=[code_output],
                outputs=[deploy_result_container]
            )

# ------------------------
# 9) 실행
# ------------------------
if __name__ == "__main__":
    try:
        demo_instance = Demo()
        demo.queue(default_concurrency_limit=20).launch(ssr_mode=False)
    except Exception as e:
        print(f"Initialization error: {e}")
        raise