File size: 60,927 Bytes
9e65b56
 
 
 
 
 
 
1635ec4
9e65b56
 
 
 
 
0b04246
9e65b56
 
 
 
c5fd9c1
9e65b56
c5fd9c1
9e65b56
 
0b04246
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1635ec4
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5e31d0
 
 
 
 
 
 
 
 
 
 
9e65b56
 
 
 
 
 
c5fd9c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e65b56
 
 
 
 
 
 
 
 
 
 
c5fd9c1
 
 
 
 
9e65b56
 
c5fd9c1
 
9e65b56
 
 
 
 
 
 
 
79159da
9e65b56
 
 
 
 
 
c5fd9c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f5e9ae
 
 
 
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1635ec4
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1635ec4
9e65b56
1635ec4
 
 
9e65b56
 
 
 
 
 
 
 
 
 
1635ec4
9e65b56
 
 
671d971
 
 
 
 
1635ec4
671d971
1635ec4
671d971
 
 
 
1f74f5a
 
1635ec4
671d971
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f74f5a
671d971
 
 
 
1635ec4
671d971
1f74f5a
 
 
 
671d971
 
 
 
 
 
 
 
 
 
1635ec4
671d971
 
 
 
 
 
1f74f5a
671d971
1f74f5a
 
671d971
 
1f74f5a
671d971
 
1f74f5a
671d971
 
 
1f74f5a
671d971
 
 
 
 
 
 
 
 
 
 
 
 
1f74f5a
671d971
 
1f74f5a
 
671d971
 
 
 
 
 
9e65b56
 
 
 
 
 
 
c5fd9c1
 
9e65b56
 
c5fd9c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e65b56
 
 
 
 
 
1635ec4
 
9e65b56
 
 
 
 
 
 
 
1635ec4
9e65b56
1635ec4
9e65b56
 
 
 
 
 
 
 
1635ec4
9e65b56
c5fd9c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1635ec4
9e65b56
 
 
 
 
 
1635ec4
0b04246
 
1635ec4
0b04246
 
9e65b56
 
 
b15e72a
9e65b56
 
 
671d971
 
 
9e65b56
671d971
 
 
 
 
 
 
 
 
 
9e65b56
 
671d971
9e65b56
671d971
9e65b56
671d971
 
 
9e65b56
 
 
 
 
 
1635ec4
9e65b56
 
 
1635ec4
 
9e65b56
1635ec4
 
9e65b56
1635ec4
9e65b56
1635ec4
 
 
 
9e65b56
 
 
 
 
 
1635ec4
9e65b56
1635ec4
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1635ec4
 
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1635ec4
9e65b56
1635ec4
9e65b56
1635ec4
 
 
88a5069
9e65b56
 
 
88a5069
 
 
9e65b56
88a5069
9e65b56
88a5069
1635ec4
 
 
 
 
 
 
 
 
 
 
9e65b56
 
 
 
 
 
 
 
 
 
 
 
1635ec4
 
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79159da
 
 
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1635ec4
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1635ec4
9e65b56
 
 
 
 
 
 
 
 
 
 
 
5bafa93
88a5069
5bafa93
 
 
 
88a5069
5bafa93
c5fd9c1
 
 
5bafa93
88a5069
 
 
 
 
5bafa93
88a5069
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bafa93
 
 
 
 
 
 
 
 
 
 
 
 
88a5069
 
5bafa93
 
 
c5fd9c1
 
5bafa93
 
0b04246
 
 
5bafa93
 
 
 
 
 
 
 
 
88a5069
 
5bafa93
88a5069
5bafa93
 
88a5069
 
5bafa93
88a5069
 
 
5bafa93
 
 
 
88a5069
5bafa93
 
 
 
 
88a5069
5bafa93
 
 
 
 
 
 
 
 
 
9e65b56
 
 
 
 
 
 
 
 
 
 
1635ec4
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1635ec4
 
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b04246
9e65b56
 
0b04246
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b04246
 
 
 
 
 
 
 
 
 
 
 
 
9e65b56
 
 
 
 
 
 
 
 
1635ec4
9e65b56
 
1635ec4
9e65b56
1635ec4
9e65b56
 
 
 
0b04246
 
ee978c4
9e65b56
 
 
 
 
 
 
 
 
 
 
 
ee978c4
0b04246
 
 
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b04246
9e65b56
 
0b04246
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b04246
 
 
 
 
 
 
 
 
 
 
 
 
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b04246
 
 
 
 
 
 
 
 
 
 
9e65b56
 
 
0b04246
9e65b56
 
 
0b04246
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b04246
 
 
 
 
 
 
 
 
 
 
9e65b56
 
 
0b04246
9e65b56
 
0b04246
9e65b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b04246
 
 
 
 
 
 
 
 
9e65b56
 
 
 
 
 
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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
import asyncio
import json
import os
import re
import time
import uuid
from contextlib import asynccontextmanager
from dataclasses import dataclass
from pathlib import Path
from typing import Any, AsyncGenerator, Dict, List, Optional

from fastapi import Depends, FastAPI, HTTPException, Request, Security
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, StreamingResponse
from fastapi.security import APIKeyHeader
from loguru import logger

try:
    import httpx as _httpx
except ImportError:
    _httpx = None


from .adapter_registry import AdapterRegistry
from .agent_loop import AgentLoop, AgentTrace
from .agent_manager import AgentManager
from .feature_flags import FeatureFlags
from .runtime_config import RuntimeConfig
from .schemas import (
    AgentRunRequest,
    AgentRunResponse,
    AgentTraceSchema,
    GenerateCivilResponseRequest,
    GenerateCivilResponseResponse,
    GenerateRequest,
    GenerateResponse,
    ToolResultSchema,
)
from .session_context import SessionContext, SessionStore
from .tool_router import ToolType, tool_name

SKIP_MODEL_LOAD = os.getenv("SKIP_MODEL_LOAD", "false").lower() in ("true", "1", "yes")


async def _noop_tool(query: str, context: dict, session: Any) -> dict:
    """build_all_tools fallback์šฉ no-op tool."""
    return {"success": False, "error": "tool์ด ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค"}


try:
    from slowapi import Limiter
    from slowapi.middleware import SlowAPIMiddleware
    from slowapi.util import get_remote_address

    limiter = Limiter(key_func=get_remote_address)
    _RATE_LIMIT_AVAILABLE = True
except ImportError:
    limiter = None
    _RATE_LIMIT_AVAILABLE = False

_API_KEY = os.getenv("API_KEY")
_api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)


async def verify_api_key(api_key: str = Security(_api_key_header)):
    if _API_KEY is None:
        return
    if api_key != _API_KEY:
        raise HTTPException(status_code=401, detail="์œ ํšจํ•˜์ง€ ์•Š์€ API ํ‚ค์ž…๋‹ˆ๋‹ค.")


runtime_config = RuntimeConfig.from_env()
runtime_config.log_summary()

MODEL_PATH = runtime_config.model.model_path
DATA_PATH = runtime_config.paths.data_path
INDEX_PATH = runtime_config.paths.index_path
GPU_UTILIZATION = runtime_config.gpu_utilization
MAX_MODEL_LEN = runtime_config.max_model_len
TRUST_REMOTE_CODE = runtime_config.model.trust_remote_code
_PROJECT_ROOT = str(Path(__file__).resolve().parent.parent.parent)
AGENTS_DIR = runtime_config.paths.agents_dir


@dataclass
class SamplingParams:
    """vLLM HTTP API์šฉ ์ƒ˜ํ”Œ๋ง ํŒŒ๋ผ๋ฏธํ„ฐ. vLLM ์ง์ ‘ import ์—†์ด ๋™์ž‘."""

    max_tokens: int = 512
    temperature: float = 0.7
    top_p: float = 1.0
    stop: Optional[list] = None
    repetition_penalty: float = 1.0


@dataclass
class PreparedGeneration:
    prompt: str
    sampling_params: SamplingParams


class _VLLMOutputItem:
    """vLLM HTTP ์‘๋‹ต์˜ ๋‹จ์ผ choice๋ฅผ ๊ธฐ์กด ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋ž˜ํ•‘."""

    def __init__(self, text: str, finish_reason: str, token_ids: list):
        self.text = text
        self.finish_reason = finish_reason
        self.token_ids = token_ids


class _VLLMHttpResult:
    """vLLM HTTP ์‘๋‹ต์„ ๊ธฐ์กด AsyncLLM ๊ฒฐ๊ณผ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋ž˜ํ•‘.

    ๊ธฐ์กด ์ฝ”๋“œ๊ฐ€ ``output.outputs[0].text``, ``output.prompt_token_ids`` ๋“ฑ์—
    ์ ‘๊ทผํ•˜๋ฏ€๋กœ ๋™์ผํ•œ ์†์„ฑ์„ ์ œ๊ณตํ•œ๋‹ค.
    """

    def __init__(self, data: dict):
        self._data = data
        choices = data.get("choices", [])
        usage = data.get("usage", {})
        self.outputs = []
        for choice in choices:
            msg = choice.get("message", {})
            text = msg.get("content", "")
            finish = choice.get("finish_reason", "stop")
            self.outputs.append(
                _VLLMOutputItem(
                    text=text,
                    finish_reason=finish,
                    token_ids=list(range(usage.get("completion_tokens", 0))),
                )
            )
        self.prompt_token_ids = list(range(usage.get("prompt_tokens", 0)))


def _extract_approval_request(graph_state: Any) -> Any:
    """LangGraph interrupt state์—์„œ approval payload๋ฅผ ์ถ”์ถœํ•œ๋‹ค."""
    if not graph_state or not getattr(graph_state, "tasks", None):
        return None
    task = graph_state.tasks[0]
    if not getattr(task, "interrupts", None):
        return None
    return task.interrupts[0].value


class vLLMEngineManager:
    """GovOn Shell MVP์šฉ ๋กœ์ปฌ ๋Ÿฐํƒ€์ž„ ๋งค๋‹ˆ์ €.

    vLLM์€ ๋ณ„๋„ ํ”„๋กœ์„ธ์Šค(entrypoint.sh)์—์„œ OpenAI-compatible ์„œ๋ฒ„๋กœ ์‹คํ–‰๋œ๋‹ค.
    ์ด ํด๋ž˜์Šค๋Š” httpx๋กœ vLLM HTTP API๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
    """

    def __init__(self):
        self._vllm_base_url = f"http://localhost:{os.getenv('VLLM_PORT', '8000')}"
        self._http_client: Optional[Any] = None
        self.feature_flags = FeatureFlags.from_env()
        self.session_store = SessionStore()
        self.agent_manager = AgentManager(AGENTS_DIR)
        self.agent_loop: Optional[AgentLoop] = None
        self.graph = None  # LangGraph CompiledGraph (v2 ์—”๋“œํฌ์ธํŠธ์šฉ)
        self._checkpointer_ctx = None  # AsyncSqliteSaver ์ปจํ…์ŠคํŠธ ๋งค๋‹ˆ์ € (lifespan์—์„œ ๊ด€๋ฆฌ)
        self._sync_checkpointer_conn = None  # SqliteSaver์šฉ sqlite3 connection (leak ๋ฐฉ์ง€)
        self._init_agent_loop()
        # _init_graph()๋Š” lifespan()์—์„œ ํ˜ธ์ถœ โ€” ๋ชจ๋“ˆ ๋กœ๋“œ ์‹œ์  ์‹คํ–‰ ๋ฐฉ์ง€

    async def initialize(self):
        if SKIP_MODEL_LOAD:
            logger.info("SKIP_MODEL_LOAD=true: ๋ชจ๋ธ ๋ฐ ์ธ๋ฑ์Šค ๋กœ๋”ฉ์„ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
            return

        # vLLM ์„œ๋ฒ„๋Š” entrypoint.sh์—์„œ ์ด๋ฏธ ๊ธฐ๋™๋จ โ€” health check๋งŒ ์ˆ˜ํ–‰
        logger.info(f"vLLM ์„œ๋ฒ„ ์—ฐ๊ฒฐ ํ™•์ธ: {self._vllm_base_url}")
        if _httpx is None:
            raise RuntimeError("httpx๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. pip install httpx")

        self._http_client = _httpx.AsyncClient(
            base_url=self._vllm_base_url,
            timeout=_httpx.Timeout(300.0, connect=30.0),
        )

        # vLLM ์„œ๋ฒ„ health check (entrypoint.sh์—์„œ ์ด๋ฏธ ํ™•์ธํ–ˆ์ง€๋งŒ ์ด์ค‘ ๊ฒ€์ฆ)
        for attempt in range(10):
            try:
                resp = await self._http_client.get("/health")
                if resp.status_code == 200:
                    logger.info("vLLM ์„œ๋ฒ„ ์—ฐ๊ฒฐ ์„ฑ๊ณต")
                    return
            except Exception:
                pass
            logger.debug(f"vLLM ์„œ๋ฒ„ ๋Œ€๊ธฐ ์ค‘... ({attempt + 1}/10)")
            await asyncio.sleep(3)

        raise RuntimeError(f"vLLM ์„œ๋ฒ„์— ์—ฐ๊ฒฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: {self._vllm_base_url}")

    def _escape_special_tokens(self, text: str) -> str:
        tokens = [
            "[|user|]",
            "[|assistant|]",
            "[|system|]",
            "[|endofturn|]",
            "<thought>",
            "</thought>",
        ]
        for token in tokens:
            text = text.replace(
                token,
                token.replace("[", "\\[")
                .replace("]", "\\]")
                .replace("<", "\\<")
                .replace(">", "\\>"),
            )
        return text

    @staticmethod
    def _strip_thought_blocks(text: str) -> str:
        # <thought>...</thought> (๊ตฌํ˜•) ๋ฐ <think>...</think> (EXAONE-4.0 ์ถ”๋ก  ๋ชจ๋“œ) ๋ชจ๋‘ ์ œ๊ฑฐ
        text = re.sub(r"<thought>.*?</thought>\s*", "", text, flags=re.DOTALL)
        text = re.sub(r"<think>.*?</think>\s*", "", text, flags=re.DOTALL)
        return text.strip()

    def _build_persona_prompt(self, agent_name: str, user_message: str) -> str:
        if self.agent_manager and self.agent_manager.get_agent(agent_name):
            return self.agent_manager.build_prompt(agent_name, user_message)
        return user_message

    def _extract_query(self, prompt: str) -> str:
        user_match = re.search(r"\[\|user\|\](.*?)\[\|endofturn\|\]", prompt, re.DOTALL)
        if user_match:
            user_block = user_match.group(1)
            complaint_match = re.search(r"๋ฏผ์›\s*๋‚ด์šฉ\s*:\s*(.+)", user_block, re.DOTALL)
            if complaint_match:
                return complaint_match.group(1).strip()
            return user_block.strip()
        return prompt

    @staticmethod
    def _is_evidence_request(query: str) -> bool:
        return any(token in query for token in ("๊ทผ๊ฑฐ", "์ถœ์ฒ˜", "์™œ", "์ด์œ ", "๋งํฌ"))

    @staticmethod
    def _is_revision_request(query: str) -> bool:
        return any(token in query for token in ("๋‹ค์‹œ", "์ˆ˜์ •", "๊ณ ์ณ", "์ •์ค‘", "๊ณต์†", "๋ณด๊ฐ•"))

    def _latest_prior_turns(
        self,
        session: SessionContext,
        current_query: str,
    ) -> tuple[Optional[str], Optional[str]]:
        turns = list(session.recent_history)
        if turns and turns[-1].role == "user" and turns[-1].content == current_query:
            turns = turns[:-1]

        previous_user = next(
            (turn.content for turn in reversed(turns) if turn.role == "user"), None
        )
        previous_assistant = next(
            (turn.content for turn in reversed(turns) if turn.role == "assistant"),
            None,
        )
        return previous_user, previous_assistant

    def _build_working_query(self, query: str, session: SessionContext) -> str:
        query = query.strip()
        if not query:
            return query

        if not (self._is_evidence_request(query) or self._is_revision_request(query)):
            return query

        previous_user, previous_assistant = self._latest_prior_turns(session, query)
        parts: List[str] = []
        if previous_user:
            parts.append(f"์›๋ž˜ ์š”์ฒญ: {previous_user}")
        if previous_assistant:
            parts.append(f"์ด์ „ ๋‹ต๋ณ€: {previous_assistant[:600]}")

        if self._is_revision_request(query):
            parts.append(f"์ˆ˜์ • ์š”์ฒญ: {query}")

        return "\n\n".join(parts) if parts else query

    @staticmethod
    def _format_evidence_items(evidence_dict: Dict[str, Any]) -> str:
        """EvidenceEnvelope dict๋ฅผ ์†Œ๋น„ํ•˜์—ฌ ์ถœ์ฒ˜ ๋ชฉ๋ก ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

        EvidenceItem์ด ์žˆ์œผ๋ฉด source-specific branching ์—†์ด ๋‹จ์ผ ํฌ๋งคํ„ฐ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.
        """
        items = evidence_dict.get("items", [])
        if not items:
            return ""

        lines: list[str] = []
        for idx, item in enumerate(items[:10], start=1):
            source_type = item.get("source_type", "")
            title = item.get("title", "")
            link = item.get("link_or_path", "")

            if source_type == "api":
                label = title or "์™ธ๋ถ€ API ๊ฒฐ๊ณผ"
                if link:
                    lines.append(f"[{idx}] {label} - {link}")
                else:
                    lines.append(f"[{idx}] {label}")
            else:
                label = title or "์ƒ์„ฑ ์ฐธ์กฐ"
                if link:
                    lines.append(f"[{idx}] {label} - {link}")
                else:
                    lines.append(f"[{idx}] {label}")

        return "\n".join(lines)

    def _summarize_evidence(
        self,
        api_lookup_data: Dict[str, Any],
    ) -> str:
        # EvidenceEnvelope๊ฐ€ ์žˆ์œผ๋ฉด ์šฐ์„  ์‚ฌ์šฉ
        evidence = api_lookup_data.get("evidence")
        if isinstance(evidence, dict) and evidence.get("items"):
            lines = ["๊ทผ๊ฑฐ ์š”์•ฝ"]
            api_items = [i for i in evidence["items"] if i.get("source_type") == "api"]
            if api_items:
                titles = ", ".join(i["title"] for i in api_items[:3] if i.get("title"))
                lines.append(
                    f"- ์™ธ๋ถ€ ๋ฏผ์›๋ถ„์„ API์—์„œ ์œ ์‚ฌ ์‚ฌ๋ก€ {len(api_items)}๊ฑด์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค."
                    + (f" ๋Œ€ํ‘œ ์‚ฌ๋ก€: {titles}" if titles else "")
                )
            if len(lines) == 1:
                lines.append(
                    "- ๋‚ด๋ถ€ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ์ถฉ๋ถ„ํžˆ ํ™•๋ณดํ•˜์ง€ ๋ชปํ•ด ์ผ๋ฐ˜ ํ–‰์ • ์‘๋Œ€ ์›์น™ ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค."
                )
            return "\n".join(lines)

        # Legacy ํฌ๋งคํ„ฐ (EvidenceItem ์—†์„ ๋•Œ)
        lines = ["๊ทผ๊ฑฐ ์š”์•ฝ"]

        api_results = api_lookup_data.get("results", [])
        if api_results:
            titles = []
            for item in api_results[:3]:
                title = item.get("title") or item.get("qnaTitle") or item.get("question")
                if title:
                    titles.append(title)
            lines.append(
                f"- ์™ธ๋ถ€ ๋ฏผ์›๋ถ„์„ API์—์„œ ์œ ์‚ฌ ์‚ฌ๋ก€ {len(api_results)}๊ฑด์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค."
                + (f" ๋Œ€ํ‘œ ์‚ฌ๋ก€: {', '.join(titles)}" if titles else "")
            )

        if len(lines) == 1:
            lines.append(
                "- ๋‚ด๋ถ€ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ์ถฉ๋ถ„ํžˆ ํ™•๋ณดํ•˜์ง€ ๋ชปํ•ด ์ผ๋ฐ˜ ํ–‰์ • ์‘๋Œ€ ์›์น™ ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค."
            )

        return "\n".join(lines)

    @staticmethod
    def _api_source_line(index: int, item: Dict[str, Any]) -> str:
        title = item.get("title") or item.get("qnaTitle") or item.get("question") or "์™ธ๋ถ€ API ๊ฒฐ๊ณผ"
        url = item.get("url") or item.get("detailUrl") or ""
        if url:
            return f"[{index}] {title} - {url}"
        return f"[{index}] {title}"

    def _build_evidence_section(
        self,
        session: SessionContext,
        current_query: str,
        api_data: Dict[str, Any],
    ) -> str:
        _, previous_answer = self._latest_prior_turns(session, current_query)
        lines = ["๊ทผ๊ฑฐ/์ถœ์ฒ˜"]
        cursor = 1

        # EvidenceEnvelope๊ฐ€ ์žˆ์œผ๋ฉด ๋‹จ์ผ ํฌ๋งคํ„ฐ๋กœ ์šฐ์„  ์ฒ˜๋ฆฌ
        api_evidence = api_data.get("evidence")

        if api_evidence and isinstance(api_evidence, dict) and api_evidence.get("items"):
            for item in api_evidence["items"][:5]:
                title = item.get("title", "") or "์™ธ๋ถ€ API ๊ฒฐ๊ณผ"
                link = item.get("link_or_path", "")
                if link:
                    lines.append(f"[{cursor}] {title} - {link}")
                else:
                    lines.append(f"[{cursor}] {title}")
                cursor += 1
        else:
            # Legacy API ํฌ๋งคํ„ฐ
            api_items = api_data.get("citations") or api_data.get("results") or []
            for item in api_items[:5]:
                lines.append(self._api_source_line(cursor, item))
                cursor += 1

        if cursor == 1:
            lines.append("- ๊ฒ€์ƒ‰ ๊ฐ€๋Šฅํ•œ ๊ทผ๊ฑฐ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.")

        section = "\n".join(lines)
        if previous_answer:
            return f"{previous_answer}\n\n{section}"
        return section

    async def _prepare_civil_response_generation(
        self,
        request: GenerateCivilResponseRequest,
        flags: Optional[FeatureFlags] = None,
        external_cases: Optional[List[dict]] = None,
    ) -> PreparedGeneration:
        gen_defaults = runtime_config.generation

        safe_message = self._escape_special_tokens(self._extract_query(request.prompt))
        user_content = f"๋‹ค์Œ ๋ฏผ์›์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.\n\n{safe_message}"
        prompt = self._build_persona_prompt("draft_response", user_content)

        sampling_params = SamplingParams(
            temperature=request.temperature,
            top_p=request.top_p,
            max_tokens=request.max_tokens,
            stop=request.stop or gen_defaults.stop_sequences,
            repetition_penalty=gen_defaults.repetition_penalty,
        )

        return PreparedGeneration(
            prompt=prompt,
            sampling_params=sampling_params,
        )

    async def _prepare_draft_only(
        self,
        request: GenerateCivilResponseRequest,
        flags: Optional[FeatureFlags] = None,
    ) -> PreparedGeneration:
        """LoRA ์ดˆ์•ˆ ์ƒ์„ฑ์šฉ: ์ฟผ๋ฆฌ๋งŒ์œผ๋กœ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ.

        ์‚ฌ์šฉ์ž ์ฟผ๋ฆฌ๋ฅผ persona ํ”„๋กฌํ”„ํŠธ๋กœ ๊ฐ์‹ธ์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
        """
        gen_defaults = runtime_config.generation

        safe_message = self._escape_special_tokens(self._extract_query(request.prompt))
        # ํ•™์Šต ๋ฐ์ดํ„ฐ ํ˜•์‹: user = instruction + "\n\n" + input
        user_content = f"๋‹ค์Œ ๋ฏผ์›์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.\n\n{safe_message}"
        prompt = self._build_persona_prompt("draft_response", user_content)

        sampling_params = SamplingParams(
            temperature=(
                request.temperature if request.temperature is not None else gen_defaults.temperature
            ),
            top_p=request.top_p if request.top_p is not None else gen_defaults.top_p,
            max_tokens=request.max_tokens or gen_defaults.max_tokens,
            stop=request.stop or gen_defaults.stop_sequences,
            repetition_penalty=gen_defaults.repetition_penalty,
        )

        return PreparedGeneration(
            prompt=prompt,
            sampling_params=sampling_params,
        )

    async def synthesize_final(
        self,
        draft_text: str,
        evidence_items: list,
        query: str,
        adapter_name: str = "public_admin",
    ) -> str:
        """์ดˆ์•ˆ + ๋„๊ตฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฒ ์ด์Šค ๋ชจ๋ธ๋กœ ํ†ตํ•ฉํ•˜์—ฌ ์ตœ์ข… ๋‹ต๋ณ€ ์ƒ์„ฑ.

        LoRA ์–ด๋Œ‘ํ„ฐ๋Š” ํ•™์Šต ํ˜•์‹(์งˆ๋ฌธโ†’๋‹ต๋ณ€)์— ํŠนํ™”๋˜์–ด ์žˆ์–ด
        ์ดˆ์•ˆ+๊ทผ๊ฑฐ ํ†ตํ•ฉ ๊ฐ™์€ ๋ฒ”์šฉ ํƒœ์Šคํฌ์—๋Š” ๋ฒ ์ด์Šค ๋ชจ๋ธ์ด ์ ํ•ฉํ•˜๋‹ค.
        """
        safe_query = self._escape_special_tokens(query[:400])
        safe_draft = self._escape_special_tokens(draft_text[:800])

        # ๊ทผ๊ฑฐ ํ…์ŠคํŠธ ์กฐ๋ฆฝ
        evidence_text = ""
        for item in evidence_items[:5]:
            source_type = item.get("source_type", "")
            title = item.get("title", "")
            excerpt = item.get("excerpt", "")[:200]
            label = "[์™ธ๋ถ€]" if source_type == "api" else "[์ƒ์„ฑ]"
            if title or excerpt:
                evidence_text += f"- {label} {title}: {excerpt}\n"

        if not evidence_text.strip():
            evidence_text = "(๊ฒ€์ƒ‰ ๊ทผ๊ฑฐ ์—†์Œ)"

        # ๋ฒ ์ด์Šค ๋ชจ๋ธ ๋ฒ”์šฉ ํ•ฉ์„ฑ ํ”„๋กฌํ”„ํŠธ
        synthesis_prompt = (
            "[|system|]๋‹น์‹ ์€ ๋ฏผ์› ๋‹ต๋ณ€์„ ๋ณด๊ฐ•ํ•˜๋Š” ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. "
            "์ดˆ์•ˆ๊ณผ ์ฐธ๊ณ  ๊ทผ๊ฑฐ๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ์ •ํ™•ํ•˜๊ณ  ๊ณต๊ฐ์ ์ธ ์ตœ์ข… ๋‹ต๋ณ€์„ ์ž‘์„ฑํ•˜์„ธ์š”. "
            "๋ฒ•์  ๊ทผ๊ฑฐ๊ฐ€ ์žˆ์œผ๋ฉด ์ธ์šฉํ•˜๊ณ , ์ ˆ์ฐจ์™€ ์กฐ์น˜์‚ฌํ•ญ์„ ๋ช…ํ™•ํžˆ ํฌํ•จํ•˜์„ธ์š”."
            "[|endofturn|]\n"
            "[|user|]๋‹ค์Œ ์ดˆ์•ˆ๊ณผ ๊ทผ๊ฑฐ๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ์ตœ์ข… ๋ฏผ์› ๋‹ต๋ณ€์„ ์ž‘์„ฑํ•˜์„ธ์š”.\n\n"
            f"[๋ฏผ์› ์งˆ์˜]\n{safe_query}\n\n"
            f"[์ดˆ์•ˆ]\n{safe_draft}\n\n"
            f"[์ฐธ๊ณ  ๊ทผ๊ฑฐ]\n{evidence_text}"
            "[|endofturn|]\n[|assistant|]"
        )

        # ๋ฒ ์ด์Šค ๋ชจ๋ธ ์‚ฌ์šฉ (LoRA ์—†์Œ) โ€” ํ•ฉ์„ฑ์€ ๋ฒ”์šฉ ํƒœ์Šคํฌ
        sampling_params = SamplingParams(
            max_tokens=768,
            temperature=0.6,
            top_p=0.9,
            stop=["[|endofturn|]"],
        )

        import uuid as _uuid

        request_id = str(_uuid.uuid4())

        try:
            output = await self._run_engine(
                synthesis_prompt, sampling_params, request_id, lora_request=None
            )
        except Exception as exc:
            logger.warning(f"[synthesize_final] ํ•ฉ์„ฑ ์‹คํŒจ: {exc}")
            return draft_text

        if output is None or not output.outputs:
            return draft_text

        return self._strip_thought_blocks(output.outputs[0].text)

    async def _run_engine(
        self,
        prompt: str,
        sampling_params: SamplingParams,
        request_id: str,
        lora_request=None,
    ):
        """vLLM OpenAI-compatible HTTP API๋ฅผ ํ†ตํ•ด ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค."""
        if self._http_client is None:
            return None

        # EXAONE chat template ํ˜•์‹์˜ prompt๋ฅผ messages๋กœ ๋ณ€ํ™˜
        messages = self._prompt_to_messages(prompt)

        body: Dict[str, Any] = {
            "model": MODEL_PATH,
            "messages": messages,
            "max_tokens": sampling_params.max_tokens,
            "temperature": sampling_params.temperature,
            "stream": False,
        }
        if sampling_params.top_p is not None and sampling_params.top_p < 1.0:
            body["top_p"] = sampling_params.top_p
        if sampling_params.stop:
            body["stop"] = list(sampling_params.stop)
        if sampling_params.repetition_penalty and sampling_params.repetition_penalty != 1.0:
            body["repetition_penalty"] = sampling_params.repetition_penalty

        # LoRA ์–ด๋Œ‘ํ„ฐ ์ง€์ •
        if lora_request is not None:
            body["model"] = lora_request.lora_name

        try:
            resp = await self._http_client.post("/v1/chat/completions", json=body)
            resp.raise_for_status()
            data = resp.json()
        except Exception as exc:
            logger.error(f"vLLM HTTP ํ˜ธ์ถœ ์‹คํŒจ: {exc}")
            return None

        # OpenAI ์‘๋‹ต์„ ๊ธฐ์กด ์ธํ„ฐํŽ˜์ด์Šค์™€ ํ˜ธํ™˜๋˜๋Š” ๊ฐ์ฒด๋กœ ๋ž˜ํ•‘
        return _VLLMHttpResult(data)

    @staticmethod
    def _prompt_to_messages(prompt: str) -> list:
        """EXAONE chat template ํ˜•์‹ ํ”„๋กฌํ”„ํŠธ๋ฅผ OpenAI messages๋กœ ๋ณ€ํ™˜."""
        messages = []
        # [|system|]...[|endofturn|], [|user|]...[|endofturn|], [|assistant|]... ํŒŒ์‹ฑ
        import re as _re

        parts = _re.split(r"\[\\?\|(\w+)\\?\|]", prompt)
        role = None
        for part in parts:
            if part in ("system", "user", "assistant"):
                role = part
            elif role and part.strip():
                content = part.replace("[|endofturn|]", "").strip()
                if content:
                    messages.append({"role": role, "content": content})
                role = None

        if not messages:
            messages = [{"role": "user", "content": prompt}]
        return messages

    async def generate(
        self,
        request: GenerateRequest,
        request_id: str,
        flags: Optional[FeatureFlags] = None,
    ) -> Any:
        return await self.generate_civil_response(request, request_id, flags)

    async def generate_civil_response(
        self,
        request: GenerateCivilResponseRequest,
        request_id: str,
        flags: Optional[FeatureFlags] = None,
        external_cases: Optional[List[dict]] = None,
        lora_request=None,
    ) -> Any:
        prepared = await self._prepare_civil_response_generation(request, flags, external_cases)
        return await self._run_engine(
            prepared.prompt, prepared.sampling_params, request_id, lora_request=lora_request
        )

    async def generate_stream(
        self,
        request: GenerateRequest,
        request_id: str,
        flags: Optional[FeatureFlags] = None,
    ) -> Any:
        prepared = await self._prepare_civil_response_generation(request, flags)
        if self._http_client is None:
            raise RuntimeError("vLLM ์„œ๋ฒ„์— ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")

        messages = self._prompt_to_messages(prepared.prompt)
        body = {
            "model": MODEL_PATH,
            "messages": messages,
            "max_tokens": prepared.sampling_params.max_tokens,
            "temperature": prepared.sampling_params.temperature,
            "stream": True,
        }
        if prepared.sampling_params.stop:
            body["stop"] = list(prepared.sampling_params.stop)

        return self._http_client.stream("POST", "/v1/chat/completions", json=body)

    def _init_agent_loop(self) -> None:
        from src.inference.actions.data_go_kr import MinwonAnalysisAction

        engine_ref = self
        minwon_action = MinwonAnalysisAction()

        async def _api_lookup_tool(query: str, context: dict, session: SessionContext) -> dict:
            working_query = query.strip()
            payload = await minwon_action.fetch_similar_cases(
                working_query,
                {
                    **context,
                    "session_context": session.build_context_summary(),
                },
            )
            results = payload["results"] or []
            return {
                "query": payload["query"],
                "count": len(results),
                "results": results,
                "context_text": payload["context_text"],
                "citations": [citation.to_dict() for citation in payload["citations"]],
                "source": "data.go.kr",
            }

        async def _draft_response_tool(
            query: str,
            context: dict,
            session: SessionContext,
        ) -> dict:
            working_query = engine_ref._build_working_query(query, session)

            # LoRA-First: ์ฟผ๋ฆฌ๋งŒ์œผ๋กœ ์ดˆ์•ˆ ์ƒ์„ฑ
            adapter_name = context.get("adapter") if context else None
            if not adapter_name:
                adapter_name = "public_admin"
            _adapter_reg = AdapterRegistry.get_instance()
            lora_req = _adapter_reg.get_lora_request(adapter_name)

            gen_request = GenerateCivilResponseRequest(
                prompt=working_query,
                max_tokens=2048,
                temperature=0.7,
            )
            request_id = str(uuid.uuid4())
            prepared = await engine_ref._prepare_draft_only(gen_request)
            final_output = await engine_ref._run_engine(
                prepared.prompt, prepared.sampling_params, request_id, lora_request=lora_req
            )

            if final_output is None or not final_output.outputs:
                return {
                    "text": "",
                    "draft_text": "",
                    "success": False,
                    "error": "๋ฏผ์› ๋‹ต๋ณ€ ์ดˆ์•ˆ ์ƒ์„ฑ ์‹คํŒจ",
                    "results": [],
                    "context_text": "",
                }

            draft_text = engine_ref._strip_thought_blocks(final_output.outputs[0].text)

            return {
                "text": draft_text,
                "draft_text": draft_text,
                "success": True,
                "results": [],
                "context_text": draft_text,
                "prompt_tokens": len(final_output.prompt_token_ids),
                "completion_tokens": len(final_output.outputs[0].token_ids),
            }

        tool_registry = {
            ToolType.API_LOOKUP: _api_lookup_tool,
            "draft_response": _draft_response_tool,
        }
        self.agent_loop = AgentLoop(tool_registry=tool_registry)

    def _build_langgraph_tools(self) -> list:
        """LangGraph ToolNode์šฉ ๋„๊ตฌ ๋ชฉ๋ก์„ ์ƒ์„ฑํ•œ๋‹ค.

        build_all_tools()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ StructuredTool ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
        AgentLoop์˜ tool_registry์—์„œ ๊ธฐ์กด closure๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์ „๋‹ฌํ•œ๋‹ค.
        """
        from src.inference.graph.tools import build_all_tools

        if self.agent_loop is None:
            return build_all_tools(
                api_lookup_action=self._get_api_lookup_action(),
            )

        # AgentLoop์˜ tool_registry์—์„œ ๊ธฐ์กด closure๋ฅผ ์ถ”์ถœ
        raw_tools = {
            str(k.value if hasattr(k, "value") else k): v for k, v in self.agent_loop._tools.items()
        }

        return build_all_tools(
            api_lookup_action=self._get_api_lookup_action(),
            draft_response_fn=raw_tools.get("draft_response"),
        )

    def _get_api_lookup_action(self) -> Any:
        """AgentLoop์— ๋“ฑ๋ก๋œ api_lookup์˜ MinwonAnalysisAction์„ ์ถ”์ถœํ•œ๋‹ค."""
        if self.agent_loop is None:
            return None
        tool_fn = self.agent_loop._tools.get(ToolType.API_LOOKUP)
        # ApiLookupCapability์ธ ๊ฒฝ์šฐ action์„ ์ง์ ‘ ์ถ”์ถœ
        if hasattr(tool_fn, "_action"):
            return tool_fn._action
        # closure์ธ ๊ฒฝ์šฐ action์„ ์ถ”์ถœํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ None ๋ฐ˜ํ™˜
        # (MinwonAnalysisAction์€ _init_agent_loop์—์„œ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค)
        try:
            from src.inference.actions.data_go_kr import MinwonAnalysisAction

            return MinwonAnalysisAction()
        except Exception:
            return None

    def _init_graph_with_async_checkpointer(self, checkpointer: object) -> None:
        """lifespan์—์„œ AsyncSqliteSaver๊ฐ€ ์ค€๋น„๋œ ํ›„ graph๋ฅผ ์žฌ๊ตฌ์„ฑํ•œ๋‹ค."""
        self._init_graph(checkpointer=checkpointer)

    def _init_graph(self, checkpointer: Optional[object] = None) -> None:
        """LangGraph StateGraph๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

        v4 ์•„ํ‚คํ…์ฒ˜: ReAct + ToolNode ๊ธฐ๋ฐ˜.
        LLM์ด ์ž์œจ์ ์œผ๋กœ ๋„๊ตฌ ํ˜ธ์ถœ์„ ๊ฒฐ์ •ํ•˜๋ฉฐ, ์ •์  planner/executor๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

        Parameters
        ----------
        checkpointer : optional
            ์™ธ๋ถ€์—์„œ ์ฃผ์ž…ํ•  LangGraph checkpointer.
            None์ด๋ฉด SqliteSaver(๋™๊ธฐ sqlite3)๋ฅผ ์‹œ๋„ํ•˜๊ณ ,
            import ์‹คํŒจ ์‹œ MemorySaver๋กœ fallbackํ•œ๋‹ค.
            SqliteSaver DB ๊ฒฝ๋กœ๋Š” SessionStore DB์™€ ๊ฐ™์€ ๋””๋ ‰ํ„ฐ๋ฆฌ์—
            ``langgraph_checkpoints.db``๋กœ ์ƒ์„ฑ๋œ๋‹ค (๊ด€์‹ฌ์‚ฌ ๋ถ„๋ฆฌ).
        """
        try:
            from src.inference.graph.builder import build_govon_graph
        except ImportError as exc:
            logger.warning(f"LangGraph graph ์ดˆ๊ธฐํ™” ์‹คํŒจ (import ์˜ค๋ฅ˜): {exc}")
            return

        tools = self._build_langgraph_tools()

        # LLM ์ธ์Šคํ„ด์Šค ๊ตฌ์„ฑ
        if SKIP_MODEL_LOAD:
            # CI/ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ: LLM์ด ์—†์œผ๋ฏ€๋กœ graph ์ดˆ๊ธฐํ™” ์Šคํ‚ต
            logger.info("SKIP_MODEL_LOAD=true: LangGraph graph ์ดˆ๊ธฐํ™” ์Šคํ‚ต")
            return
        elif os.getenv("LANGGRAPH_MODEL_BASE_URL"):
            from langchain_openai import ChatOpenAI

            llm = ChatOpenAI(
                base_url=os.environ["LANGGRAPH_MODEL_BASE_URL"],
                api_key=os.getenv("LANGGRAPH_MODEL_API_KEY", "EMPTY"),
                model=os.getenv("LANGGRAPH_PLANNER_MODEL", runtime_config.model.model_path),
                temperature=0.0,
                max_tokens=1024,
            )
        else:
            # ์šด์˜ ํ™˜๊ฒฝ: vLLM OpenAI-compatible endpoint ์‚ฌ์šฉ
            from langchain_openai import ChatOpenAI

            vllm_port = os.getenv("VLLM_PORT", "8000")
            llm = ChatOpenAI(
                base_url=f"http://localhost:{vllm_port}/v1",
                api_key="EMPTY",
                model=runtime_config.model.model_path,
                temperature=0.0,
                max_tokens=1024,
            )

        # checkpointer๊ฐ€ ์™ธ๋ถ€์—์„œ ์ฃผ์ž…๋˜์ง€ ์•Š์œผ๋ฉด SqliteSaver๋ฅผ ์‹œ๋„ํ•œ๋‹ค.
        if checkpointer is None:
            checkpointer, conn = _build_sync_sqlite_checkpointer(self.session_store.db_path)
            if self._sync_checkpointer_conn is not None:
                try:
                    self._sync_checkpointer_conn.close()
                except Exception:
                    pass
            self._sync_checkpointer_conn = conn

        self.graph = build_govon_graph(
            llm=llm,
            tools=tools,
            session_store=self.session_store,
            checkpointer=checkpointer,
        )
        logger.info("LangGraph graph ์ดˆ๊ธฐํ™” ์™„๋ฃŒ")


def _build_sync_sqlite_checkpointer(
    session_db_path: str,
) -> tuple:
    """SqliteSaver(๋™๊ธฐ) ๋˜๋Š” MemorySaver(fallback)๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    LangGraph checkpointer์šฉ SQLite DB๋Š” SessionStore์˜ sessions.sqlite3์™€
    ๊ฐ™์€ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋ณ„๋„ ํŒŒ์ผ ``langgraph_checkpoints.db``๋กœ ์ƒ์„ฑํ•œ๋‹ค.
    ๋‘ DB๋ฅผ ๋ถ„๋ฆฌํ•จ์œผ๋กœ์จ ๊ด€์‹ฌ์‚ฌ(์„ธ์…˜ ๋ฉ”ํƒ€ vs. graph ์ฒดํฌํฌ์ธํŠธ)๋ฅผ ๋ช…ํ™•ํžˆ ๊ตฌ๋ถ„ํ•œ๋‹ค.

    SqliteSaver๋Š” ํ”„๋กœ์„ธ์Šค ์žฌ์‹œ์ž‘ ํ›„์—๋„ interrupt ์ƒํƒœ๋ฅผ SQLite์—์„œ ๋ณต์›ํ•˜๋ฏ€๋กœ
    MemorySaver์™€ ๋‹ฌ๋ฆฌ ์žฌ์‹œ์ž‘-์•ˆ์ „(restart-safe)ํ•˜๋‹ค.

    Parameters
    ----------
    session_db_path : str
        SessionStore๊ฐ€ ์‚ฌ์šฉ ์ค‘์ธ sessions.sqlite3 ํŒŒ์ผ ๊ฒฝ๋กœ.
        ์ด ๊ฒฝ๋กœ์˜ ๋ถ€๋ชจ ๋””๋ ‰ํ„ฐ๋ฆฌ์— langgraph_checkpoints.db๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

    Returns
    -------
    tuple[SqliteSaver | MemorySaver, sqlite3.Connection | None]
        (checkpointer, conn) ํŠœํ”Œ.
        SqliteSaver ์‚ฌ์šฉ ์‹œ conn์€ ์—ด๋ฆฐ sqlite3.Connection์ด๋ฉฐ,
        ํ˜ธ์ถœ์ž๊ฐ€ ์ ์ ˆํ•œ ์‹œ์ ์— closeํ•ด์•ผ ํ•œ๋‹ค.
        MemorySaver fallback ์‹œ conn์€ None์ด๋‹ค.
    """
    cp_db_path = str(Path(session_db_path).parent / "langgraph_checkpoints.db")
    try:
        from langgraph.checkpoint.sqlite import SqliteSaver

        conn = __import__("sqlite3").connect(cp_db_path, check_same_thread=False)
        saver = SqliteSaver(conn)
        logger.info(f"LangGraph checkpointer: SqliteSaver ({cp_db_path})")
        return saver, conn
    except ImportError:
        logger.warning(
            "langgraph-checkpoint-sqlite ๋ฏธ์„ค์น˜ โ€” MemorySaver๋กœ fallbackํ•ฉ๋‹ˆ๋‹ค. "
            "ํ”„๋กœ์„ธ์Šค ์žฌ์‹œ์ž‘ ์‹œ interrupt ์ƒํƒœ๊ฐ€ ์†Œ๋ฉธ๋ฉ๋‹ˆ๋‹ค."
        )
        from langgraph.checkpoint.memory import MemorySaver

        return MemorySaver(), None


manager = vLLMEngineManager()


@asynccontextmanager
async def lifespan(app: FastAPI):
    """FastAPI lifespan: ๋ชจ๋ธ/์ธ๋ฑ์Šค ์ดˆ๊ธฐํ™” ๋ฐ AsyncSqliteSaver ์—…๊ทธ๋ ˆ์ด๋“œ.

    startup ๋‹จ๊ณ„์—์„œ AsyncSqliteSaver๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉด graph๋ฅผ ์žฌ๊ตฌ์„ฑํ•œ๋‹ค.
    AsyncSqliteSaver๋Š” async ์ปจํ…์ŠคํŠธ ๋งค๋‹ˆ์ €๋กœ ๊ด€๋ฆฌํ•˜๋ฉฐ, shutdown ์‹œ ์ •๋ฆฌํ•œ๋‹ค.
    AsyncSqliteSaver import ์‹คํŒจ ์‹œ _init_graph์—์„œ ์ด๋ฏธ ์„ค์ •๋œ
    SqliteSaver(๋˜๋Š” MemorySaver fallback)๋ฅผ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•œ๋‹ค.
    """
    await manager.initialize()

    # vLLM ์„œ๋ฒ„ ์—ฐ๊ฒฐ ํ›„ graph ์ดˆ๊ธฐํ™” (๋ชจ๋“ˆ ๋กœ๋“œ ์‹œ์ ์ด ์•„๋‹Œ lifespan์—์„œ ์‹คํ–‰)
    manager._init_graph()

    # AsyncSqliteSaver๋กœ graph ์žฌ๊ตฌ์„ฑ ์‹œ๋„ (๋” ๋†’์€ async ์„ฑ๋Šฅ)
    async_cp_db = str(Path(manager.session_store.db_path).parent / "langgraph_checkpoints.db")
    try:
        from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver

        async with AsyncSqliteSaver.from_conn_string(async_cp_db) as async_saver:
            # ๋™๊ธฐ SqliteSaver๊ฐ€ ๋ณด์œ ํ•˜๋˜ connection์„ ๋‹ซ์•„ leak์„ ๋ฐฉ์ง€ํ•œ๋‹ค.
            if manager._sync_checkpointer_conn is not None:
                try:
                    manager._sync_checkpointer_conn.close()
                except Exception:
                    pass
                manager._sync_checkpointer_conn = None
            manager._checkpointer_ctx = async_saver
            manager._init_graph_with_async_checkpointer(async_saver)
            logger.info(f"LangGraph checkpointer: AsyncSqliteSaver ({async_cp_db})")
            yield
        manager._checkpointer_ctx = None
    except ImportError:
        logger.info("AsyncSqliteSaver ๋ฏธ์„ค์น˜ โ€” SqliteSaver(๋™๊ธฐ) ๋˜๋Š” MemorySaver๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.")
        yield


app = FastAPI(
    title="GovOn Local Runtime",
    description="Local FastAPI daemon for the GovOn Agentic Shell MVP.",
    lifespan=lifespan,
)

ALLOWED_ORIGINS = os.getenv("CORS_ORIGINS", "").split(",")
if ALLOWED_ORIGINS and ALLOWED_ORIGINS[0]:
    app.add_middleware(
        CORSMiddleware,
        allow_origins=ALLOWED_ORIGINS,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

if _RATE_LIMIT_AVAILABLE and limiter is not None:
    app.state.limiter = limiter
    app.add_middleware(SlowAPIMiddleware)


@app.get("/health")
async def health():
    return {
        "status": "healthy",
        "profile": runtime_config.profile.value,
        "model": runtime_config.model.model_path,
        "agents_loaded": manager.agent_manager.list_agents() if manager.agent_manager else [],
        "feature_flags": {
            "model_version": manager.feature_flags.model_version,
        },
        "session_store": {
            "driver": "sqlite",
            "path": manager.session_store.db_path,
        },
    }


def _rate_limit(limit_string: str):
    if _RATE_LIMIT_AVAILABLE and limiter is not None:
        return limiter.limit(limit_string)

    def _noop(func):
        return func

    return _noop


def get_feature_flags(request: Request) -> FeatureFlags:
    header = request.headers.get("X-Feature-Flag")
    return manager.feature_flags.override_from_header(header)


@app.post("/v1/generate-civil-response", response_model=GenerateCivilResponseResponse)
@_rate_limit("30/minute")
async def generate_civil_response(
    request: GenerateCivilResponseRequest,
    _: None = Depends(verify_api_key),
    flags: FeatureFlags = Depends(get_feature_flags),
):
    if request.stream:
        raise HTTPException(status_code=400, detail="๋ฏผ์› ๋‹ต๋ณ€ ์ŠคํŠธ๋ฆฌ๋ฐ์€ /v1/stream์„ ์‚ฌ์šฉํ•˜์„ธ์š”.")

    request_id = str(uuid.uuid4())
    final_output = await manager.generate_civil_response(
        request,
        request_id,
        flags,
    )
    if final_output is None:
        raise HTTPException(status_code=500, detail="๋ฏผ์› ๋‹ต๋ณ€ ์ƒ์„ฑ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.")

    return GenerateCivilResponseResponse(
        request_id=request_id,
        complaint_id=request.complaint_id,
        text=manager._strip_thought_blocks(final_output.outputs[0].text),
        prompt_tokens=len(final_output.prompt_token_ids),
        completion_tokens=len(final_output.outputs[0].token_ids),
    )


@app.post("/v1/generate", response_model=GenerateResponse)
@_rate_limit("30/minute")
async def generate(
    request: GenerateRequest,
    _: None = Depends(verify_api_key),
    flags: FeatureFlags = Depends(get_feature_flags),
):
    if request.stream:
        raise HTTPException(status_code=400, detail="Use /v1/stream for streaming.")

    request_id = str(uuid.uuid4())
    final_output = await manager.generate(request, request_id, flags)
    if final_output is None:
        raise HTTPException(status_code=500, detail="Generation failed.")

    return GenerateResponse(
        request_id=request_id,
        complaint_id=request.complaint_id,
        text=manager._strip_thought_blocks(final_output.outputs[0].text),
        prompt_tokens=len(final_output.prompt_token_ids),
        completion_tokens=len(final_output.outputs[0].token_ids),
    )


@app.post("/v1/chat/completions")
@_rate_limit("30/minute")
async def chat_completions(
    request: Request,
    _: None = Depends(verify_api_key),
):
    """OpenAI-compatible /v1/chat/completions.

    vLLM HTTP API๋ฅผ ๊ฒฝ์œ ํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    v2 ReAct graph๋Š” ChatOpenAI๊ฐ€ vLLM OpenAI ์„œ๋ฒ„์— ์ง์ ‘ ์—ฐ๊ฒฐํ•˜๋ฏ€๋กœ
    ์ด ์—”๋“œํฌ์ธํŠธ๋Š” v1 ํ˜ธํ™˜ ์œ ์ง€์šฉ์ด๋‹ค.
    """
    try:
        body = await request.json()
    except Exception:
        raise HTTPException(status_code=400, detail="Invalid JSON body.")

    messages: list[dict] = body.get("messages", [])
    if not messages:
        raise HTTPException(status_code=422, detail="messages must not be empty.")

    try:
        max_tokens = int(body.get("max_tokens", 512))
        temperature = float(body.get("temperature", 0.7))
    except (ValueError, TypeError):
        raise HTTPException(status_code=400, detail="Invalid max_tokens or temperature value.")

    if not (1 <= max_tokens <= runtime_config.max_model_len):
        raise HTTPException(
            status_code=400,
            detail=f"max_tokens must be between 1 and {runtime_config.max_model_len}.",
        )
    if not (0.0 <= temperature <= 2.0):
        raise HTTPException(status_code=400, detail="temperature must be between 0.0 and 2.0.")

    model: str = body.get("model", runtime_config.model.model_path)

    # ๋ฉ”์‹œ์ง€ โ†’ ํ”„๋กฌํ”„ํŠธ ๋ณ€ํ™˜ (EXAONE chat template ํ˜•์‹)
    prompt_parts: list[str] = []
    for msg in messages:
        role = msg.get("role", "user")
        content = msg.get("content", "")
        if role == "system":
            prompt_parts.append(f"[|system|]{content}[|endofturn|]")
        elif role == "user":
            prompt_parts.append(f"[|user|]{content}[|endofturn|]")
        elif role == "assistant":
            prompt_parts.append(f"[|assistant|]{content}[|endofturn|]")
        else:
            logger.warning(f"chat_completions: ์ง€์›ํ•˜์ง€ ์•Š๋Š” role ๋ฌด์‹œ: {role!r}")
    prompt_parts.append("[|assistant|]")
    prompt = "\n".join(prompt_parts)

    if manager._http_client is None:
        raise HTTPException(status_code=503, detail="vLLM server not connected.")

    request_id = str(uuid.uuid4())
    logger.info(
        f"chat_completions request_id={request_id} messages={len(messages)} max_tokens={max_tokens}"
    )
    sampling_params = SamplingParams(
        max_tokens=max_tokens,
        temperature=temperature,
        stop=["[|endofturn|]"],
    )

    try:
        final_output = await manager._run_engine(prompt, sampling_params, request_id)
    except Exception as exc:
        logger.error(f"chat_completions generation failed: {exc}")
        raise HTTPException(status_code=500, detail="Generation failed due to internal error.")

    if final_output is None or not final_output.outputs:
        raise HTTPException(status_code=500, detail="Generation failed.")

    output = final_output.outputs[0]
    text = manager._strip_thought_blocks(output.text)
    prompt_tokens = len(final_output.prompt_token_ids)
    completion_tokens = len(output.token_ids)
    vllm_reason = getattr(output, "finish_reason", None)
    finish_reason = "length" if vllm_reason == "length" else "stop"

    return {
        "id": f"chatcmpl-{request_id}",
        "object": "chat.completion",
        "created": int(time.time()),
        "model": model,
        "choices": [
            {
                "index": 0,
                "message": {"role": "assistant", "content": text},
                "finish_reason": finish_reason,
            }
        ],
        "usage": {
            "prompt_tokens": prompt_tokens,
            "completion_tokens": completion_tokens,
            "total_tokens": prompt_tokens + completion_tokens,
        },
    }


@app.post("/v1/stream")
@_rate_limit("30/minute")
async def stream_generate(
    request: GenerateRequest,
    _: None = Depends(verify_api_key),
    flags: FeatureFlags = Depends(get_feature_flags),
):
    if not request.stream:
        request.stream = True

    request_id = str(uuid.uuid4())
    results_stream = await manager.generate_stream(
        request,
        request_id,
        flags,
    )

    async def stream_results() -> AsyncGenerator[str, None]:
        async for request_output in results_stream:
            text = request_output.outputs[0].text
            finished = request_output.finished
            if finished:
                text = manager._strip_thought_blocks(text)

            response_obj = {"request_id": request_id, "text": text, "finished": finished}
            yield f"data: {json.dumps(response_obj, ensure_ascii=False)}\n\n"

    return StreamingResponse(stream_results(), media_type="text/event-stream")


def _trace_to_schema(trace: AgentTrace) -> AgentTraceSchema:
    return AgentTraceSchema(
        request_id=trace.request_id,
        session_id=trace.session_id,
        plan=trace.plan_tools,
        plan_reason=trace.plan_reason,
        tool_results=[
            ToolResultSchema(
                tool=tool_name(result.tool),
                success=result.success,
                latency_ms=round(result.latency_ms, 2),
                data=result.data,
                error=result.error,
            )
            for result in trace.tool_results
        ],
        total_latency_ms=round(trace.total_latency_ms, 2),
        error=trace.error,
    )


@app.post("/v1/agent/run", response_model=AgentRunResponse)
@_rate_limit("30/minute")
async def agent_run(
    request: AgentRunRequest,
    _: None = Depends(verify_api_key),
):
    if not manager.agent_loop:
        raise HTTPException(status_code=503, detail="์—์ด์ „ํŠธ ๋ฃจํ”„๊ฐ€ ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
    if request.stream:
        raise HTTPException(status_code=400, detail="์ŠคํŠธ๋ฆฌ๋ฐ์€ /v1/agent/stream์„ ์‚ฌ์šฉํ•˜์„ธ์š”.")

    session = manager.session_store.get_or_create(session_id=request.session_id)
    request_id = str(uuid.uuid4())
    trace = await manager.agent_loop.run(
        query=request.query,
        session=session,
        request_id=request_id,
        force_tools=request.force_tools,
    )

    return AgentRunResponse(
        request_id=request_id,
        session_id=session.session_id,
        text=trace.final_text,
        trace=_trace_to_schema(trace),
    )


@app.post("/v1/agent/stream")
@_rate_limit("30/minute")
async def agent_stream(
    request: AgentRunRequest,
    _: None = Depends(verify_api_key),
):
    if not manager.agent_loop:
        raise HTTPException(status_code=503, detail="์—์ด์ „ํŠธ ๋ฃจํ”„๊ฐ€ ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")

    session = manager.session_store.get_or_create(session_id=request.session_id)
    request_id = str(uuid.uuid4())

    async def stream_events() -> AsyncGenerator[str, None]:
        async for event in manager.agent_loop.run_stream(
            query=request.query,
            session=session,
            request_id=request_id,
            force_tools=request.force_tools,
        ):
            yield f"data: {json.dumps(event, ensure_ascii=False)}\n\n"

    return StreamingResponse(stream_events(), media_type="text/event-stream")


# ---------------------------------------------------------------------------
# v2 ์—”๋“œํฌ์ธํŠธ: LangGraph ๊ธฐ๋ฐ˜ agent ์‹คํ–‰ (interrupt/approve ํŒจํ„ด)
# ---------------------------------------------------------------------------


@app.post("/v2/agent/stream")
@_rate_limit("30/minute")
async def v2_agent_stream(
    request: AgentRunRequest,
    _http_request: Request,
    _: None = Depends(verify_api_key),
):
    """LangGraph ๊ธฐ๋ฐ˜ agent SSE ์ŠคํŠธ๋ฆฌ๋ฐ ์‹คํ–‰.

    graph.astream()์„ ์‚ฌ์šฉํ•ด ๋…ธ๋“œ๋ณ„ ์™„๋ฃŒ ์ด๋ฒคํŠธ๋ฅผ SSE๋กœ ์ „์†กํ•œ๋‹ค.

    ์ด๋ฒคํŠธ ํ˜•์‹ (๊ฐ ์ค„: ``data: <JSON>\\n\\n``):
      - ๋…ธ๋“œ ์ง„ํ–‰: ``{"node": "<name>", "status": "completed", ...}``
      - approval_wait ๋„๋‹ฌ:
        ``{"node": "approval_wait", "status": "awaiting_approval",
           "approval_request": {...}, "thread_id": "..."}``
      - ์˜ค๋ฅ˜: ``{"node": "error", "status": "error", "error": "..."}``

    ์Šน์ธ ํ๋ฆ„:
    - ํด๋ผ์ด์–ธํŠธ๋Š” ``awaiting_approval`` ์ด๋ฒคํŠธ ์ˆ˜์‹  ํ›„ ์ŠคํŠธ๋ฆผ์ด ์ข…๋ฃŒ๋จ์„ ์ธ์ง€ํ•˜๊ณ 
      ``/v2/agent/approve``๋กœ ์Šน์ธ/๊ฑฐ์ ˆ์„ ์ „๋‹ฌํ•œ๋‹ค.
    """
    if not manager.graph:

        async def _no_graph():
            yield 'data: {"node": "error", "status": "error", "error": "LangGraph graph๊ฐ€ ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค."}\n\n'

        return StreamingResponse(_no_graph(), media_type="text/event-stream")

    from langchain_core.messages import HumanMessage

    thread_id = request.session_id or str(uuid.uuid4())
    session_id = thread_id
    request_id = str(uuid.uuid4())
    config = {"configurable": {"thread_id": thread_id}}
    initial_state = {
        "session_id": session_id,
        "request_id": request_id,
        "messages": [HumanMessage(content=request.query)],
    }

    # ๊ธฐ์กด interrupt ์ƒํƒœ๊ฐ€ ๋‚จ์•„์žˆ์œผ๋ฉด ๊ฑฐ์ ˆ(cancel)๋กœ ํ•ด์†Œ
    try:
        from langgraph.types import Command

        existing_state = await manager.graph.aget_state(config)
        if existing_state and existing_state.next:
            await manager.graph.ainvoke(
                Command(resume={"approved": False, "cancel": True}),
                config,
            )
    except Exception as clear_exc:
        logger.warning(f"[v2] interrupt ์ƒํƒœ ํ™•์ธ/ํ•ด์†Œ ์‹คํŒจ (๋ฌด์‹œ): {type(clear_exc).__name__}")

    async def _generate() -> AsyncGenerator[str, None]:
        try:
            async for chunk in manager.graph.astream(initial_state, config, stream_mode="updates"):
                # chunk: {node_name: state_delta}
                for node_name, state_delta in chunk.items():
                    event: dict = {
                        "node": node_name,
                        "status": "completed",
                    }
                    # persist ์™„๋ฃŒ ์‹œ evidence_items๋ฅผ ์ด๋ฒคํŠธ์— ํฌํ•จ.
                    # ์ „์ œ: stream_mode="updates"์—์„œ state_delta๋Š” ๋…ธ๋“œ์˜ raw return dict๋‹ค.
                    # evidence_items ์Šคํ‚ค๋งˆ: EvidenceItem.to_dict() ํ•„๋“œ๋ฅผ ๋”ฐ๋ฅธ๋‹ค.
                    #   source_type: "api" | "llm_generated"
                    #   title, excerpt, link_or_path, page, score, provider_meta
                    if node_name == "persist" and isinstance(state_delta, dict):
                        if state_delta.get("final_text"):
                            event["final_text"] = state_delta["final_text"]
                        if state_delta.get("evidence_items"):
                            event["evidence_items"] = state_delta["evidence_items"]
                    # approval_wait: ๋ช…์‹œ์  ๋…ธ๋“œ๋ช… ๋˜๋Š” LangGraph interrupt() ํ˜ธ์ถœ ์‹œ
                    # stream_mode="updates"์—์„œ emit๋˜๋Š” "__interrupt__" ์ฒญํฌ ๋ชจ๋‘ ์ฒ˜๋ฆฌ
                    if node_name in ("approval_wait", "__interrupt__"):
                        try:
                            graph_state = await manager.graph.aget_state(config)
                            if graph_state.next:
                                event = {
                                    "node": "approval_wait",
                                    "status": "awaiting_approval",
                                    "approval_request": _extract_approval_request(graph_state),
                                    "thread_id": thread_id,
                                    "session_id": session_id,
                                }
                        except Exception as exc:
                            logger.warning(f"[v2/agent/stream] aget_state ์‹คํŒจ: {exc}")
                            event["node"] = "approval_wait"
                            event["status"] = "awaiting_approval"
                            event["thread_id"] = thread_id
                            event["session_id"] = session_id
                            event["approval_request"] = {
                                "prompt": "์Šน์ธ ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. /v2/agent/approve๋กœ ์ง„ํ–‰ํ•˜์„ธ์š”."
                            }

                    yield f"data: {json.dumps(event, ensure_ascii=False)}\n\n"

                    # Stop streaming after awaiting_approval (client must call /v2/agent/approve)
                    if event.get("status") == "awaiting_approval":
                        return
        except Exception as exc:
            logger.error(f"[v2/agent/stream] ์ŠคํŠธ๋ฆผ ์˜ˆ์™ธ: {exc}")
            error_event = {"node": "error", "status": "error", "error": str(exc)}
            yield f"data: {json.dumps(error_event, ensure_ascii=False)}\n\n"

    return StreamingResponse(_generate(), media_type="text/event-stream")


@app.post("/v2/agent/run")
@_rate_limit("30/minute")
async def v2_agent_run(
    request: AgentRunRequest,
    _http_request: Request,
    _: None = Depends(verify_api_key),
):
    """LangGraph ๊ธฐ๋ฐ˜ agent ์‹คํ–‰ (1๋‹จ๊ณ„: interrupt๊นŒ์ง€).

    graph๋ฅผ ์‹คํ–‰ํ•˜์—ฌ `approval_wait` ๋…ธ๋“œ์—์„œ interrupt๋˜๋ฉด
    `status: awaiting_approval`๊ณผ ํ•จ๊ป˜ ์Šน์ธ ์š”์ฒญ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    ํด๋ผ์ด์–ธํŠธ๋Š” ๋ฐ˜ํ™˜๋œ `thread_id`๋ฅผ ์ €์žฅํ•ด๋‘๊ณ 
    `/v2/agent/approve`๋กœ ์Šน์ธ/๊ฑฐ์ ˆ์„ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค.

    Session Resume Contract
    -----------------------
    ๋™์ผ session_id๋กœ ์žฌ์š”์ฒญํ•˜๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ ๊ทœ์น™์„ ๋”ฐ๋ฅธ๋‹ค:

    1. **interrupt ๋Œ€๊ธฐ ์ค‘**: graph๊ฐ€ approval_wait์—์„œ interrupt ์ƒํƒœ์ด๋ฉด
       ํ˜„์žฌ checkpoint์—์„œ resumeํ•˜์ง€ ์•Š๊ณ  ์ƒˆ ๋ฉ”์‹œ์ง€๋ฅผ *์ถ”๊ฐ€ํ•˜์—ฌ* ์ด์–ด์„œ ์‹คํ–‰ํ•œ๋‹ค.
       (์žฌ์š”์ฒญ์€ ์ƒˆ graph_run์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.)
       ์Šน์ธ/๊ฑฐ์ ˆ์€ ๋ฐ˜๋“œ์‹œ `/v2/agent/approve`๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.

    2. **์™„๋ฃŒ๋œ graph**: graph๊ฐ€ END์— ๋„๋‹ฌํ•œ ์ƒํƒœ(state.next == [])์ด๋ฉด
       ๋™์ผ thread_id์— ์ƒˆ graph_run์„ ์‹œ์ž‘ํ•œ๋‹ค. LangGraph checkpointer๊ฐ€
       ๋™์ผ thread_id์—์„œ ์ด์ „ ์ƒํƒœ๋ฅผ ๋ˆ„์ ํ•˜๋ฏ€๋กœ ๋Œ€ํ™” ํžˆ์Šคํ† ๋ฆฌ๊ฐ€ ๋ณด์กด๋œ๋‹ค.

    3. **ํ”„๋กœ์„ธ์Šค ์žฌ์‹œ์ž‘ ํ›„**: SqliteSaver ์‚ฌ์šฉ ์‹œ DB์—์„œ checkpoint๊ฐ€ ๋ณต์›๋˜๋ฏ€๋กœ
       interrupt ์ƒํƒœ๊ฐ€ ์œ ์ง€๋œ๋‹ค. ํด๋ผ์ด์–ธํŠธ๋Š” ๊ธฐ์กด thread_id๋กœ `/v2/agent/approve`
       ๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•˜๋ฉด ์ค‘๋‹จ๋œ ์ง€์ ์—์„œ resumeํ•  ์ˆ˜ ์žˆ๋‹ค.

    Note: session_id == thread_id. ๋‘ ๊ฐ’์€ ํ•ญ์ƒ ๋™์ผํ•˜๊ฒŒ ์œ ์ง€๋œ๋‹ค.
    """
    if not manager.graph:
        raise HTTPException(status_code=503, detail="LangGraph graph๊ฐ€ ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")

    from langchain_core.messages import HumanMessage

    thread_id = request.session_id or str(uuid.uuid4())
    session_id = thread_id  # thread_id๋ฅผ session_id๋กœ ํ™•์ • (session_id == thread_id ๋ถˆ๋ณ€)
    request_id = str(uuid.uuid4())
    config = {"configurable": {"thread_id": thread_id}}
    initial_state = {
        "session_id": session_id,
        "request_id": request_id,
        "messages": [HumanMessage(content=request.query)],
    }

    # ๊ธฐ์กด interrupt ์ƒํƒœ๊ฐ€ ๋‚จ์•„์žˆ์œผ๋ฉด ๊ฑฐ์ ˆ(cancel)๋กœ ํ•ด์†Œ
    try:
        existing_state = await manager.graph.aget_state(config)
        if existing_state and existing_state.next:
            from langgraph.types import Command

            await manager.graph.ainvoke(
                Command(resume={"approved": False, "cancel": True}),
                config,
            )
    except Exception as clear_exc:
        logger.warning(f"[v2] interrupt ์ƒํƒœ ํ™•์ธ/ํ•ด์†Œ ์‹คํŒจ (๋ฌด์‹œ): {type(clear_exc).__name__}")

    try:
        await manager.graph.ainvoke(initial_state, config)

        # interrupt ์ƒํƒœ ํ™•์ธ
        graph_state = await manager.graph.aget_state(config)
        if graph_state.next:
            # interrupt ๋Œ€๊ธฐ ์ค‘: approval_request ์ •๋ณด๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ˜ํ™˜
            return {
                "status": "awaiting_approval",
                "thread_id": thread_id,
                "session_id": session_id,
                "graph_run_id": request_id,
                "approval_request": _extract_approval_request(graph_state),
            }

        # interrupt ์—†์ด ์™„๋ฃŒ๋œ ๊ฒฝ์šฐ (rejected ๋˜๋Š” ์˜ค๋ฅ˜)
        final_state = graph_state.values
        return {
            "status": "completed",
            "thread_id": thread_id,
            "session_id": session_id,
            "graph_run_id": request_id,
            "text": final_state.get("final_text", ""),
            "evidence_items": final_state.get("evidence_items", []),
        }
    except Exception as exc:
        logger.error(f"[v2/agent/run] ์˜ˆ์™ธ ๋ฐœ์ƒ: {exc}")
        # graph_run์„ "error" status๋กœ ๊ธฐ๋ก ์‹œ๋„
        try:
            if manager.session_store:
                session = manager.session_store.get_or_create(session_id)
                session.add_graph_run(
                    request_id=request_id,
                    plan_summary=f"[error] {exc}",
                    approval_status="",
                    executed_capabilities=[],
                    status="error",
                    total_latency_ms=0.0,
                )
        except Exception as persist_exc:
            logger.warning(f"[v2/agent/run] error persist ์‹คํŒจ: {persist_exc}")
        logger.exception(f"[v2/agent/run] ์š”์ฒญ ์ฒ˜๋ฆฌ ์‹คํŒจ: {exc}")
        return JSONResponse(
            status_code=500,
            content={
                "status": "error",
                "thread_id": thread_id,
                "session_id": session_id,
                "graph_run_id": request_id,
                "error": "์š”์ฒญ ์ฒ˜๋ฆฌ ์ค‘ ๋‚ด๋ถ€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.",
            },
        )


@app.post("/v2/agent/approve")
@_rate_limit("30/minute")
async def v2_agent_approve(
    thread_id: str,
    approved: bool,
    _http_request: Request,
    _: None = Depends(verify_api_key),
):
    """interrupt๋œ graph๋ฅผ resumeํ•œ๋‹ค (2๋‹จ๊ณ„: ์Šน์ธ/๊ฑฐ์ ˆ).

    Parameters
    ----------
    thread_id : str
        `/v2/agent/run`์—์„œ ๋ฐ˜ํ™˜๋œ thread_id.
    approved : bool
        True๋ฉด tool_execute๋กœ ์ง„ํ–‰, False๋ฉด graph๊ฐ€ END๋กœ ์ข…๋ฃŒ.
    """
    if not manager.graph:
        raise HTTPException(status_code=503, detail="LangGraph graph๊ฐ€ ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")

    from langgraph.types import Command

    config = {"configurable": {"thread_id": thread_id}}

    try:
        result = await manager.graph.ainvoke(
            Command(resume={"approved": approved}),
            config,
        )

        # ๊ฑฐ์ ˆ์ด๋ฉด "rejected", ์Šน์ธ ์™„๋ฃŒ๋ฉด "completed"
        approval_status = result.get("approval_status", "")
        if not approved:
            response_status = "rejected"
        else:
            response_status = "completed"

        return {
            "status": response_status,
            "thread_id": thread_id,
            "session_id": result.get("session_id", ""),
            "graph_run_id": result.get("request_id", ""),
            "text": result.get("final_text", ""),
            "evidence_items": result.get("evidence_items", []),
            "approval_status": approval_status,
        }
    except Exception as exc:
        logger.error(f"[v2/agent/approve] ์˜ˆ์™ธ ๋ฐœ์ƒ: {exc}")
        # graph_run์„ "error" status๋กœ ๊ธฐ๋ก ์‹œ๋„
        session_id = ""
        request_id = ""
        try:
            if manager.session_store:
                graph_state = await manager.graph.aget_state(config)
                state_values = graph_state.values if graph_state else {}
                session_id = state_values.get("session_id", "")
                request_id = state_values.get("request_id", "")
                if session_id:
                    session = manager.session_store.get_or_create(session_id)
                    session.add_graph_run(
                        request_id=request_id,
                        plan_summary=f"[error] {exc}",
                        approval_status="",
                        executed_capabilities=[],
                        status="error",
                        total_latency_ms=0.0,
                    )
        except Exception as persist_exc:
            logger.warning(f"[v2/agent/approve] error persist ์‹คํŒจ: {persist_exc}")
        logger.exception(f"[v2/agent/approve] ์Šน์ธ ์ฒ˜๋ฆฌ ์‹คํŒจ: {exc}")
        return JSONResponse(
            status_code=500,
            content={
                "status": "error",
                "thread_id": thread_id,
                "session_id": session_id,
                "graph_run_id": request_id,
                "error": "์Šน์ธ ์ฒ˜๋ฆฌ ์ค‘ ๋‚ด๋ถ€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.",
            },
        )


@app.post("/v2/agent/cancel")
@_rate_limit("30/minute")
async def v2_agent_cancel(
    thread_id: str,
    _http_request: Request,
    _: None = Depends(verify_api_key),
):
    """interrupt ๋Œ€๊ธฐ ์ค‘์ธ graph๋ฅผ ๊ฐ•์ œ ์ทจ์†Œํ•œ๋‹ค.

    interrupt ์ƒํƒœ์—์„œ ๊ฑฐ์ ˆ ์ฒ˜๋ฆฌ(Command(resume={"approved": False}))๋ฅผ ์ˆ˜ํ–‰ํ•˜๋˜,
    state์— interrupt_reason="user_cancel"์„ ์ „๋‹ฌํ•˜์—ฌ
    persist ๋…ธ๋“œ๊ฐ€ graph_run status๋ฅผ "interrupted"๋กœ ๊ธฐ๋กํ•˜๊ฒŒ ํ•œ๋‹ค.

    Parameters
    ----------
    thread_id : str
        `/v2/agent/run`์—์„œ ๋ฐ˜ํ™˜๋œ thread_id.
    """
    if not manager.graph:
        raise HTTPException(status_code=503, detail="LangGraph graph๊ฐ€ ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")

    from langgraph.types import Command

    config = {"configurable": {"thread_id": thread_id}}

    try:
        # interrupt ์ƒํƒœ ํ™•์ธ
        graph_state = await manager.graph.aget_state(config)
        if not graph_state or not graph_state.next:
            raise HTTPException(
                status_code=409,
                detail="ํ•ด๋‹น thread๋Š” ํ˜„์žฌ interrupt ๋Œ€๊ธฐ ์ƒํƒœ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.",
            )

        session_id = graph_state.values.get("session_id", "")

        # ๊ฐ•์ œ ๊ฑฐ์ ˆ + interrupt_reason ์ „๋‹ฌ๋กœ resume
        result = await manager.graph.ainvoke(
            Command(resume={"approved": False, "cancel": True}),
            config,
        )

        # persist ๋…ธ๋“œ์—์„œ "interrupted" ๊ธฐ๋ก์„ ์œ„ํ•ด state update
        # (approval_wait_node๊ฐ€ cancel ์‹ ํ˜ธ๋ฅผ interrupt_reason์œผ๋กœ ๋ณ€ํ™˜)
        return {
            "status": "cancelled",
            "thread_id": thread_id,
            "session_id": session_id,
            "graph_run_id": result.get("request_id", ""),
        }
    except HTTPException:
        raise
    except Exception as exc:
        logger.exception(f"[v2/agent/cancel] ์ทจ์†Œ ์ฒ˜๋ฆฌ ์‹คํŒจ: {exc}")
        return JSONResponse(
            status_code=500,
            content={
                "status": "error",
                "thread_id": thread_id,
                "error": "์ทจ์†Œ ์ฒ˜๋ฆฌ ์ค‘ ๋‚ด๋ถ€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.",
            },
        )


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, **runtime_config.to_uvicorn_kwargs())