File size: 65,151 Bytes
7e184bf
2e1d581
7cf5031
4f3da4f
7e184bf
7cf5031
bed229a
7e184bf
bed229a
 
7cf5031
 
bf8f953
 
 
4f3da4f
 
4a2c0eb
7cf5031
5386e26
 
 
 
 
 
 
 
 
 
 
1160b9b
71621f7
158215f
 
36dcbf4
 
5386e26
7cf5031
2e1d581
 
5386e26
 
 
 
 
 
 
 
 
bed229a
5386e26
5a47613
5386e26
4f3da4f
7e184bf
85c9819
 
 
 
bef09db
ea37944
7cf5031
1ba6ce2
7cf5031
bed229a
7cf5031
158215f
1ae9a08
ea37944
1ae9a08
775fb04
85c9819
4a2c0eb
7904f75
1160b9b
158215f
 
bed229a
71621f7
7cf5031
bed229a
97d9a2e
bed229a
7cf5031
 
97d9a2e
 
7cf5031
 
97d9a2e
29a29d5
 
5386e26
 
4a2c0eb
 
158215f
 
 
 
 
 
 
29a29d5
7cf5031
 
f4b3c44
7cf5031
 
 
 
 
 
 
 
5386e26
7cf5031
 
 
 
 
 
 
 
4a325e1
7cf5031
 
 
 
 
 
 
4a325e1
bf8f953
7cf5031
 
7904f75
1ba6ce2
8e7137e
7e184bf
7904f75
 
7e184bf
 
7138767
f4b3c44
7e184bf
 
 
7cf5031
 
85c9819
 
 
 
 
7cf5031
 
 
 
bf8f953
85c9819
 
 
 
 
29a29d5
5a47613
 
 
 
 
 
 
 
 
 
 
 
 
85c9819
 
 
 
 
4f3da4f
 
 
4a2c0eb
4f3da4f
5386e26
 
 
 
 
1160b9b
 
 
 
 
7cf5031
 
 
 
 
 
7e184bf
 
7cf5031
 
 
bf8f953
29a29d5
5a47613
 
 
7cf5031
 
7e184bf
7cf5031
 
4a325e1
4a2c0eb
4a325e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e184bf
7cf5031
 
1e2aaef
 
dfcb128
bed229a
4a325e1
 
 
dfcb128
4a325e1
 
7cf5031
7e184bf
4a325e1
 
 
 
 
 
 
 
 
dfcb128
4a325e1
 
 
7cf5031
4a325e1
 
7cf5031
 
080a6ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfcb128
080a6ff
 
 
 
 
 
 
7cf5031
 
 
dfcb128
7cf5031
4a325e1
 
 
 
 
 
7cf5031
 
 
 
942e3f2
5386e26
 
 
 
 
 
 
 
 
 
 
 
 
 
942e3f2
 
7cf5031
 
 
4a325e1
 
 
 
 
 
942e3f2
dfcb128
4a325e1
7e184bf
7cf5031
64d4d94
7cf5031
942e3f2
64d4d94
 
 
 
 
 
97d9a2e
70be945
7cf5031
942e3f2
 
 
64d4d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
942e3f2
97d9a2e
 
 
 
 
942e3f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5386e26
942e3f2
 
 
 
 
 
 
 
 
 
 
97d9a2e
942e3f2
 
97d9a2e
942e3f2
 
97d9a2e
64d4d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dcbf4
 
 
 
64d4d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
942e3f2
64d4d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cf5031
 
 
 
 
 
85c9819
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed229a
7cf5031
 
dfcb128
bed229a
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f4e297
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed229a
0f4e297
dfcb128
 
 
 
 
 
 
 
 
 
 
0f4e297
 
 
 
 
 
dfcb128
0f4e297
 
 
 
dfcb128
 
0f4e297
 
dfcb128
0f4e297
 
dfcb128
0f4e297
 
 
 
 
7cf5031
5a47613
 
dfcb128
29a29d5
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a47613
 
 
dfcb128
5a47613
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a47613
 
 
dfcb128
5a47613
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29a29d5
85c9819
 
dfcb128
85c9819
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85c9819
4f3da4f
 
dfcb128
4f3da4f
dfcb128
 
 
 
 
 
158215f
dfcb128
 
 
158215f
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f3da4f
5386e26
 
dfcb128
5386e26
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
5386e26
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5386e26
1160b9b
 
dfcb128
1160b9b
dfcb128
 
 
 
 
 
158215f
dfcb128
 
 
158215f
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1160b9b
158215f
 
dfcb128
158215f
dfcb128
 
 
 
 
 
 
158215f
dfcb128
 
 
 
 
 
158215f
dfcb128
 
 
 
158215f
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158215f
 
 
dfcb128
158215f
dfcb128
 
 
 
 
 
 
158215f
dfcb128
 
 
 
 
 
158215f
dfcb128
 
 
 
158215f
dfcb128
158215f
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158215f
36dcbf4
 
dfcb128
36dcbf4
dfcb128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dcbf4
7cf5031
 
 
 
 
 
 
 
85c9819
7cf5031
bf8f953
 
5a47613
 
 
 
 
 
85c9819
 
4f3da4f
 
5386e26
 
1160b9b
 
158215f
 
 
 
36dcbf4
 
7cf5031
 
bed229a
7e184bf
 
7cf5031
7e184bf
 
 
 
7cf5031
bf8f953
5a47613
 
 
85c9819
4f3da4f
5386e26
1160b9b
158215f
 
36dcbf4
7e184bf
 
 
bed229a
7cf5031
 
 
 
 
 
 
 
bed229a
7cf5031
 
7e184bf
7cf5031
 
bed229a
7cf5031
bed229a
7cf5031
 
bed229a
7cf5031
bf8f953
5a47613
 
 
85c9819
4f3da4f
5386e26
1160b9b
158215f
 
36dcbf4
7cf5031
bed229a
 
7cf5031
 
 
bf8f953
5a47613
 
 
85c9819
4f3da4f
5386e26
1160b9b
158215f
 
36dcbf4
bed229a
eae50b4
7e184bf
 
 
bef09db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e184bf
7cf5031
4a325e1
97d9a2e
bef09db
 
 
 
 
 
 
 
7cf5031
 
 
 
bef09db
 
7e184bf
4a2c0eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e184bf
4a2c0eb
7cf5031
4a325e1
4a2c0eb
 
7e184bf
 
4a325e1
bed229a
4a2c0eb
 
7e184bf
bed229a
4a325e1
 
bed229a
 
 
 
 
7cf5031
 
 
158215f
7cf5031
 
4a325e1
7cf5031
bed229a
7cf5031
158215f
7cf5031
 
bed229a
 
7e184bf
 
 
bef09db
32b8e9c
bed229a
bf8f953
 
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
import os
from dotenv import load_dotenv
from typing import TypedDict, Annotated, Dict, Any, Optional, Union, List
from pathlib import Path
from langgraph.graph.message import add_messages
from langchain_core.messages import AnyMessage, HumanMessage, AIMessage, SystemMessage, ToolMessage
from langgraph.graph import START, END, StateGraph
from langchain_openai import ChatOpenAI
import tempfile
import random
import json
import requests
from urllib.parse import quote, urlparse
from bs4 import BeautifulSoup
import html2text
import pandas as pd
from tabulate import tabulate
import base64

# Import all tool functions from tools.py
from tools import (
    tools_config,
    run_python_code,
    scrape_webpage,
    wikipedia_search,
    tavily_search,
    arxiv_search,
    supabase_operation,
    excel_to_text,
    save_attachment_to_tempfile,
    process_youtube_video,
    transcribe_audio,
    extract_python_code_from_complex_input,
    process_image,
    read_file,
    process_online_document
)

load_dotenv()

# Remove the following functions from agent.py since they're now imported from tools.py:
# - run_python_code (lines ~28-175)
# - scrape_webpage (lines ~177-310) 
# - wikipedia_search (lines ~345-405)
# - tavily_search (lines ~407-470)
# - arxiv_search (lines ~472-535)
# - supabase_operation (lines ~537-620)
# - excel_to_text (lines ~622-690)
# - save_attachment_to_tempfile (lines ~1680-1706)

# Also remove the tools_config definition (lines ~795-870) since it's imported from tools.py

# The rest of the file remains the same...

# System prompt to guide the model's behavior
#web_search: Search the google search engine when Tavily Search and Wikipedia Search do not return a result. Provide a specific search query.
#webpage_scrape: Scrape content from a specific webpage URL when Tavily Search and Wikipedia Search do not return a result. Provide a valid URL to extract information from a particular web page.
#Give preference to using Tavily Search and Wikipedia Search before using web_search or webpage_scrape. When Web_search does not return a result, use Tavily Search.

SYSTEM_PROMPT = """ You are a genuis deep reseach assistant called ScholarAI, made by Vividh Mahajan. Answer the following questions as best you can. If it is a basic question, answer it using your internal knowledge. If it is a complex question that requires facts, use the tools to answer it DO NOT rely on your internal knowledge unless the tools fail to provide a result:
For simple questions, you can use your internal knowledge and answer directly. If you do not understand the question, ask for clarification after trying to answer the question yourself.

The way you use the tools is by specifying a json blob. These are the only tools you can use:
Specifically, this json should have an `action` key (with the name of the tool to use) and an `action_input` key (with the input to the tool going here).

The only values that should be in the "action" field are:
python_code: Execute Python code. Use this tool to calculate math problems. make sure to use prints to be able to view the final result. args: {"code": {"type": "string"}}
wikipedia_search: Search Wikipedia for information about a specific topic. Optionally specify the number of results to return, args: {"query": {"type": "string"}, "num_results": {"type": "integer", "optional": true}}
tavily_search: Search the web using Tavily. Optionally specify search_depth as 'basic' or 'comprehensive'. DO NOT use Tavily Search for basic questions that you can answer using your internal knowledge especially if the question is not about factual information. DO NOT use Tavily Search for clarifiction. args: {"query": {"type": "string"}, "search_depth": {"type": "string", "optional": true}}
arxiv_search: Search ArXiv for publications,news and other resources. Optionally specify max_results to control the number of papers returned, args: {"query": {"type": "string"}, "max_results": {"type": "integer", "optional": true}}
webpage_scrape: Scrape a specific webpage, args: {"url": {"type": "string"}}
supabase_operation: Perform database operations, args: {"operation_type": {"type": "string"}, "table": {"type": "string"}, "data": {"type": "object", "optional": true}, "filters": {"type": "object", "optional": true}}
excel_to_text: Convert Excel to Markdown table with attachment, args: {"excel_path": {"type": "string"}, "file_content": {"type": "string"}, "sheet_name": {"type": "string", "optional": true}}
process_youtube_video: Process a YouTube video by extracting its transcript/captions and basic metadata by providing the video URL. Returns video metadata and transcript, args: {"url": {"type": "string"}, "summarize": {"type": "boolean", "optional": true}}
transcribe_audio: Transcribe audio files using OpenAI Whisper, args: {"audio_path": {"type": "string"}, "file_content": {"type": "string", "optional": true}, "language": {"type": "string", "optional": true}}
process_image: Process and analyze image files, args: {"image_path": {"type": "string"}, "image_url": {"type": "string", "optional": true}, "file_content": {"type": "string", "optional": true}, "analyze_content": {"type": "boolean", "optional": true}}
read_file: Read and display the contents of a text file, args: {"file_path": {"type": "string"}, "file_content": {"type": "string", "optional": true}, "line_start": {"type": "integer", "optional": true}, "line_end": {"type": "integer", "optional": true}}

If you get stuck, try using another tool. For example if you are unable to find relevant information from the tavily_search tool, try using the wikipedia_search tool and vice versa.
IMPORTANT: Make sure your JSON is properly formatted with double quotes around keys and string values.

Example use for tools:

```json
{
  "action": "tavily_search",
  "action_input": {"query": "What is the capital of France?", "search_depth": "basic"}
}
```
or
```json
{
  "action": "process_youtube_video",
  "action_input": {"url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ", "summarize": true}
}
```
or
```json
{
  "action": "process_image",
  "action_input": {"image_path": "example.jpg", "analyze_content": true}
}
```

ALWAYS follow this specific format for your responses. Your entire response will follow this pattern:
Question: [the user's question]
Thought: [your reasoning about what to do next, break it down into smaller steps and clearly state your thoughts]
Action: 
```json
{
  "action": "[tool_name]",
  "action_input": {"[parameter_name]": "[parameter_value]"}
}
```
Observation: [the result from the tool will appear here]
Thought: [your reasoning after seeing the observation, break it down into smaller steps]
Action: 
```json
{
  "action": "[tool_name]",
  "action_input": {"[parameter_name]": "[parameter_value]"}
}
```
Observation: [another tool result will appear here]

IMPORTANT: You MUST strictly follow the ReAct pattern (Reasoning, Action, Observation):
1. First reason about the problem in the "Thought" section
2. Then decide what action to take in the "Action" section (using the tools)
3. Wait for an observation from the tool
4. Based on the observation, continue with another thought
5. This cycle repeats until you have enough information to provide a final answer

NEVER fake or simulate tool output yourself. If you are unable to make progreess in a certain way, try a different tool or a different approach.

... (this Thought/Action/Observation cycle can repeat as needed) ...
Thought: I now know the final answer
Final Answer: Make sure to follow any formatting instructions given by the user. Do not give too long of an answer.
If you are unable arrive at a final answer, summarize the information you have gathered such as links and any relevent information and provide a final answer. Also state a reason for you not being able to answer the question.
Now begin! Reminder to ALWAYS use the exact characters `Final Answer:` when you provide a definitive answer. DO NOT USE TAVILY SEARCH FOR CLARIFICATION OR BASIC QUESTIONS THAT YOU CAN ANSWER USING YOUR INTERNAL KNOWLEDGE"""

#YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string. For one word answers, start with the word with a capital letter.

# Generate the chat interface, including the tools
llm = ChatOpenAI(
    model="gpt-4o-mini",
    temperature=0.1
)

chat = llm
# Tools are defined but not bound to the LLM here
tools_config = [
    # {
    #     "name": "web_search",
    #     "description": "Search the web for current information. Provide a specific search query in the format: {\"query\": \"your search query here\"}",
    #     "func": safe_web_search
    # },
    {
        "name": "python_code", 
        "description": "Execute Python code. Provide the complete Python code as a string in the format: {\"code\": \"your python code here\"}",
        "func": run_python_code
    },
    # {
    #     "name": "webpage_scrape",
    #     "description": "Scrape content from a specific webpage URL. Provide a valid URL in the format: {\"url\": \"https://example.com\"}",
    #     "func": scrape_webpage
    # },
    {
        "name": "wikipedia_search",
        "description": "Search Wikipedia for information about a specific topic. Provide a query in the format: {\"query\": \"your topic\", \"num_results\": 3}",
        "func": wikipedia_search
    },
    {
        "name": "tavily_search",
        "description": "Search the web using Tavily for more comprehensive results. Provide a query in the format: {\"query\": \"your search query\", \"search_depth\": \"basic\"}",
        "func": tavily_search
    },
    {
        "name": "arxiv_search",
        "description": "Search ArXiv for scientific papers. Provide a query in the format: {\"query\": \"your research topic\", \"max_results\": 5}",
        "func": arxiv_search
    },
    {
        "name": "supabase_operation",
        "description": "Perform database operations on Supabase (insert, select, update, delete). Provide operation_type, table name, and optional data/filters. ",
        "func": supabase_operation
    },
    {
        "name": "excel_to_text",
        "description": "Read an Excel file and return a Markdown table. You can provide either the path to an Excel file or use a file attachment. For attachments, provide a base64-encoded string of the file content and a filename.",
        "func": excel_to_text
    },
    {
        "name": "process_youtube_video",
        "description": "Extract and analyze YouTube video content by providing the video URL. Returns video metadata and transcript.",
        "func": process_youtube_video
    },
    {
        "name": "transcribe_audio",
        "description": "Transcribe audio files using OpenAI Whisper. You can provide either a file path or use a file attachment. For attachments, provide base64-encoded content. Optionally specify language for better accuracy.",
        "func": transcribe_audio
    }
]

# Instead of binding tools, we'll handle the JSON parsing ourselves
# chat_with_tools = chat.bind_tools([Tool(**tool) for tool in tools_config])
chat_with_tools = chat

# Generate the AgentState and Agent graph
class ActionInput(TypedDict, total=False):
    query: Optional[str]
    code: Optional[str]
    url: Optional[str]
    image_url: Optional[str]
    num_results: Optional[int]
    search_depth: Optional[str]
    max_results: Optional[int]

class AgentState(TypedDict, total=False):
    messages: Annotated[list[AnyMessage], add_messages]
    current_tool: Optional[str]
    action_input: Optional[ActionInput]
    iteration_count: int  # Added to track iterations
    attachments: Dict[str, str]  # Added to store file attachments (filename -> base64 content)
    # tool_call_id: Optional[str] # Ensure this is present if used by your graph logic for tools

# Add prune_messages_for_llm function
def prune_messages_for_llm(
    full_history: List[AnyMessage], 
    num_recent_to_keep: int = 6  # Keeps roughly 2-3 ReAct turns (Thought/Action, Observation)
) -> List[AnyMessage]:
    """
    Prunes the message history for the LLM call.
    This function expects a 'core' history (messages without the initial SystemMessage).
    It keeps the first HumanMessage (original query) and the last `num_recent_to_keep` messages
    from this core history, injecting a condensation note.
    """
    if not full_history: # full_history here is actually core_history
        return []

    first_human_message: Optional[HumanMessage] = None
    for msg in full_history: # Iterate over the provided core_history
        if isinstance(msg, HumanMessage):
            first_human_message = msg
            break
    
    # If history is too short or no initial human query found in core_history,
    # return core_history as is. The calling function (assistant) will prepend SystemMessage.
    # Threshold considers: first_human (1) + condensation_note (1) + num_recent_to_keep
    if first_human_message is None or len(full_history) < (1 + 1 + num_recent_to_keep):
        return full_history

    # Pruning is needed for the core_history
    recent_messages_from_core = full_history[-num_recent_to_keep:]
    
    pruned_core_list: List[AnyMessage] = []
    
    # Add the first human message
    pruned_core_list.append(first_human_message)
    
    # Add condensation note
    pruned_core_list.append(
        AIMessage(content="[System note: To manage context length, earlier parts of the conversation have been omitted. The original query and the most recent interactions are preserved.]")
    )
    
    # Add recent messages, ensuring not to duplicate the first_human_message if it's in the recent slice
    for msg in recent_messages_from_core:
        if msg is not first_human_message: # Check object identity
            pruned_core_list.append(msg)
            
    return pruned_core_list

def assistant(state: AgentState) -> Dict[str, Any]:
    """Assistant node that processes messages and decides on next action."""
    from langchain_core.messages import AIMessage  # Add import at the start of the function
    
    print("\n=== Assistant Node ===")
    
    full_current_history = state["messages"]
    iteration_count = state.get("iteration_count", 0)
    iteration_count += 1 # Increment for the current call
    print(f"Iteration: {iteration_count}")

    # Prepare messages for the LLM
    system_msg = SystemMessage(content=SYSTEM_PROMPT)
    
    # Core history excludes any SystemMessages found in the accumulated history.
    # The pruning function operates on this core history.
    core_history = [msg for msg in full_current_history if not isinstance(msg, SystemMessage)]

    llm_input_core_messages: List[AnyMessage]

    # Prune if it's time (e.g., after every 5th completed iteration, so check for current iteration 6, 11, etc.)
    # Iteration 1-5: no pruning. Iteration 6: prune.
    if iteration_count > 5 and (iteration_count - 1) % 5 == 0:
        print(f"Pruning message history at iteration {iteration_count}")
        llm_input_core_messages = prune_messages_for_llm(core_history, num_recent_to_keep=6)
    else:
        llm_input_core_messages = core_history
    
    # Combine system message with the (potentially pruned) core messages
    messages_for_llm = [system_msg] + llm_input_core_messages
    
    # Get response from the assistant
    try:
        response = chat_with_tools.invoke(messages_for_llm, stop=["Observation:"])
        
        # Check for empty response
        if response is None or not hasattr(response, 'content') or not response.content or len(response.content.strip()) < 20:
            print("Warning: Received empty or very short response from LLM")
            
            # Create a new response object if it's None
            if response is None:
                response = AIMessage(content="")
                print("Created new AIMessage object for None response")
            
            # Check what kind of information we have in the last observation
            last_observation = None
            for msg in reversed(core_history):
                if isinstance(msg, AIMessage) and "Observation:" in msg.content:
                    last_observation = msg.content
                    break
                    
            # Create an appropriate fallback response
            if last_observation and "python_code" in state.get("current_tool", ""):
                print("Creating fallback response for empty response after Python code execution")
                fallback_content = (
                    "Thought: I've analyzed the results of the code execution. Based on the observations, "
                    "I need to determine the final answer.\n\n"
                    "Final Answer: "
                )
                
                # Look for common patterns in Python output that might indicate results
                import re
                result_match = re.search(r'(\[.+?\]|\{.+?\}|[A-Za-z,\s]+)$', last_observation)
                if result_match:
                    fallback_content += result_match.group(1).strip()
                else:
                    # Generic fallback based on the executed code
                    fallback_content += "Based on the code analysis, but the exact result was unclear from the output."
            else:
                # Generic fallback suggesting a reasonable next search
                print("Creating generic fallback response for empty LLM response")
                
                # Make sure we have a valid query
                query = ""
                if full_current_history and hasattr(full_current_history[0], 'content'):
                    query = full_current_history[0].content[:100]  # Limit to first 100 chars
                else:
                    query = "additional information for this question"
                    
                fallback_content = (
                    "Thought: I need more information to answer this question correctly. Let me search for additional details.\n\n"
                    "Action: \n```json\n"
                    "{\n"
                    '  "action": "tavily_search",\n'
                    f'  "action_input": {{"query": "{query}", "search_depth": "comprehensive"}}\n'
                    "}\n```"
                )
                
            # Use our fallback content instead of the empty response
            response.content = fallback_content
            print(f"Created fallback response: {fallback_content[:100]}...")
        else:
            content_preview = response.content[:300].replace('\n', ' ')
            print(f"Response preview: {content_preview}...")
    except Exception as e:
        print(f"Error in LLM invocation: {str(e)}")
        # Create a fallback response in case of LLM errors
        response = AIMessage(content=(
            "Thought: I encountered an error in processing. Let me try to proceed with what I know.\n\n"
            "Final Answer: Unable to complete the task due to an error in processing."
        ))
    
    # Extract the action JSON from the response text
    action_json = extract_json_from_text(response.content)
    print(f"Extracted action: {action_json.get('action') if action_json else 'None'}")
    
    assistant_response_message = AIMessage(content=response.content)
    
    state_update: Dict[str, Any] = {
        "messages": [assistant_response_message], 
        "iteration_count": iteration_count
    }
    
    if action_json and "action" in action_json and "action_input" in action_json:
        tool_name = action_json["action"]
        tool_input = action_json["action_input"]
        
        # Handle nested JSON issue - check if any value in action_input is a JSON string
        if isinstance(tool_input, dict):
            for key, value in tool_input.items():
                if isinstance(value, str) and value.strip().startswith("{"):
                    try:
                        nested_json = json.loads(value)
                        if isinstance(nested_json, dict) and "action" in nested_json and "action_input" in nested_json:
                            # This is a nested structure, use the inner one
                            tool_name = nested_json["action"]
                            tool_input = nested_json["action_input"]
                            print(f"Unwrapped nested JSON. New tool: {tool_name}")
                            break
                    except json.JSONDecodeError:
                        continue
        
        print(f"Using tool: {tool_name}")
        
        tool_call_id = f"call_{random.randint(1000000, 9999999)}"
        
        state_update["current_tool"] = tool_name
        state_update["action_input"] = tool_input
    else:
        print("No tool action found or 'Final Answer' detected in response.")
        state_update["current_tool"] = None
        state_update["action_input"] = None
        
    print("=== End Assistant Node ===\n")
    return state_update

def extract_json_from_text(text: str) -> dict:
    """Extract JSON from text, handling markdown code blocks and other formats."""
    try:
        import re
        import json
        
        # Return empty if text is None or very short (less than 10 chars)
        if not text or len(text.strip()) < 10:
            print("Warning: Empty or very short text input to JSON extraction")
            return None
        
        print(f"Attempting to extract JSON from text: {text[:200]}...")
        
        # First, clean up the text to handle specific patterns that might confuse parsing
        text = text.replace('\\n', '\n').replace('\\"', '"')
        
        # Case 1: "Final Answer:" detection - if present, return None to indicate we should end
        if "Final Answer:" in text:
            print("Detected 'Final Answer' - no tool action needed")
            return None
        
        # Case 2: Extract direct python dictionary representation without JSON formatting
        if "action_input" in text and not '{"action"' in text and not '{"action_input"' in text:
            # Try regex to extract a Python dict-like structure
            action_match = re.search(r"action:\s*(\w+)", text, re.IGNORECASE)
            input_match = re.search(r"action_input:\s*(\{.+?\})", text, re.DOTALL | re.IGNORECASE)
            
            if action_match and input_match:
                action = action_match.group(1).strip()
                try:
                    action_input = eval(input_match.group(1))  # Be careful with eval!
                    if isinstance(action_input, dict):
                        return {"action": action, "action_input": action_input}
                except:
                    pass
        
        # Pattern 1: Look for "Action:" followed by a markdown code block
        action_match = re.search(r"Action:\s*```(?:python|json)?\s*(.*?)```", text, re.DOTALL)
        if action_match:
            action_content = action_match.group(1).strip()
            print(f"Found action content from markdown block: {action_content[:100]}...")
            
            # Try to parse as JSON first
            try:
                parsed_json = json.loads(action_content)
                if "action" in parsed_json and "action_input" in parsed_json:
                    return parsed_json
            except json.JSONDecodeError:
                # If it's Python code, create action structure
                if "=" in action_content or "import" in action_content or "print" in action_content:
                    print("Detected Python code, formatting as action_input")
                    return {
                        "action": "python_code",
                        "action_input": {"code": action_content}
                    }
        
        # Pattern 2: Look for regular markdown code blocks
        code_blocks = re.findall(r"```(?:json|python)?(.+?)```", text, re.DOTALL)
        for block in code_blocks:
            block = block.strip()
            print(f"Processing code block: {block[:100]}...")
            
            # Try to parse as JSON
            try:
                parsed = json.loads(block)
                if "action" in parsed and "action_input" in parsed:
                    print(f"Successfully parsed JSON block: {parsed}")
                    return parsed
            except json.JSONDecodeError:
                # If it's Python code, create action structure
                if "=" in block or "import" in block or "print" in block or "def " in block:
                    print("Detected Python code in block, formatting as action_input")
                    return {
                        "action": "python_code",
                        "action_input": {"code": block}
                    }
        
        # Pattern 3: Direct JSON object ({...}) in the text
        json_matches = re.findall(r"\{[\s\S]*?\}", text)
        for json_str in json_matches:
            try:
                parsed = json.loads(json_str)
                if "action" in parsed and "action_input" in parsed:
                    print(f"Found valid JSON object: {parsed}")
                    return parsed
            except json.JSONDecodeError:
                    continue
        
        # Pattern 4: Look for patterns like 'action': 'tool_name', 'action_input': {...}
        action_pattern = re.search(r"['\"](action)['\"]:\s*['\"](\w+)['\"]", text)
        action_input_pattern = re.search(r"['\"](action_input)['\"]:\s*(\{.+\})", text, re.DOTALL)
        
        if action_pattern and action_input_pattern:
            action = action_pattern.group(2)
            action_input_str = action_input_pattern.group(2)
            
            try:
                action_input = json.loads(action_input_str)
                return {
                    "action": action,
                    "action_input": action_input
                }
            except json.JSONDecodeError:
                pass
        
        # Pattern 5: Look for simple text patterns like "I need to use tool X to search for Y"
        tool_patterns = [
            (r"(?:use|using|need to use|should use|will use)(?:\s+the)?\s+(\w+)(?:\s+tool)?\s+to\s+(?:search|find|look up|research)(?:\s+for)?\s+['\"](.*?)['\"]", 
             lambda m: {"action": m.group(1).lower(), "action_input": {"query": m.group(2)}}),
            (r"(?:use|using|need to use|should use|will use)(?:\s+the)?\s+(\w+)(?:\s+tool)?\s+to\s+(?:search|find|look up|research)\s+(?:for\s+)?(.+?)(?=\.|$)", 
             lambda m: {"action": m.group(1).lower(), "action_input": {"query": m.group(2).strip()}}),
            (r"(?:use|using|need to use|should use|will use)(?:\s+the)?\s+(\w+)(?:\s+tool)?\s+on\s+['\"](.*?)['\"]", 
             lambda m: {"action": m.group(1).lower(), "action_input": {"query": m.group(2)}})
        ]
        
        for pattern, formatter in tool_patterns:
            match = re.search(pattern, text, re.IGNORECASE)
            if match:
                try:
                    result = formatter(match)
                    # Map common words to actual tool names
                    tool_mapping = {
                        "tavily": "tavily_search",
                        "wikipedia": "wikipedia_search",
                        "arxiv": "arxiv_search",
                        "web": "tavily_search",
                        "python": "python_code",
                        "excel": "excel_to_text",
                        "youtube": "process_youtube_video",
                        "webpage": "webpage_scrape",
                        "scrape": "webpage_scrape",
                        "pdf": "process_online_document",
                        "document": "process_online_document",
                        "online": "process_online_document"
                    }
                    
                    if result["action"].lower() in tool_mapping:
                        result["action"] = tool_mapping[result["action"].lower()]
                        
                    print(f"Extracted tool action using pattern: {result}")
                    return result
                except Exception as e:
                    print(f"Error formatting pattern match: {e}")
        
        # Fallback: If we detect thinking about a specific topic, suggest a related tool
        fallback_patterns = [
            (r"(?:I need|I should|I will|let me|I can)(?:\s+(?:to))?\s+(?:search|find|look for)\s+(?:information|details|data)?\s+(?:about|on|regarding)?\s+(.+?)(?=\.|$)",
             lambda m: {"action": "tavily_search", "action_input": {"query": m.group(1).strip()}}),
            (r"(?:I will|I should|let me)(?:\s+now)?\s+(?:search|check)(?:\s+on)?\s+wikipedia\s+(?:for)?\s+(.+?)(?=\.|$)",
             lambda m: {"action": "wikipedia_search", "action_input": {"query": m.group(1).strip()}}),
            (r"(?:I need|I should|I will|let me)(?:\s+to)?\s+(?:execute|run|write|use)\s+(?:some|a)?\s+python\s+(?:code)?",
             lambda m: {"action": "python_code", "action_input": {"code": "# Example code\nprint('Please specify Python code to execute')"}})
        ]
        
        for pattern, formatter in fallback_patterns:
            match = re.search(pattern, text, re.IGNORECASE)
            if match:
                try:
                    result = formatter(match)
                    print(f"Used fallback pattern to suggest tool: {result}")
                    return result
                except Exception as e:
                    print(f"Error in fallback pattern: {e}")
        
        print("Could not extract valid JSON from text using any pattern")
        
        # Last resort fallback: If we've tried everything and failed, and the response is long
        # enough to suggest the model is doing work but not formatting correctly, try to 
        # extract any query-like content and suggest a tavily search
        if len(text) > 200 and "search" in text.lower():
            # Extract a potential query - look for sentences with search-related terms
            search_sentences = re.findall(r'[^.!?]*(?:search|find|look for|investigate|research)[^.!?]*[.!?]', text)
            if search_sentences:
                best_sentence = max(search_sentences, key=len)
                # Clean up the query
                query = re.sub(r'(?:I will|I should|I need to|I want to|Let me|I\'ll|I can)\s+(?:search|find|look for|investigate|research)(?:\s+for)?\s+', '', best_sentence)
                query = query.strip('.!? \t\n')
                if query and len(query) > 5:
                    print(f"Last resort fallback: suggesting Tavily search for: {query}")
                    return {"action": "tavily_search", "action_input": {"query": query}}
        
        return None
        
    except Exception as e:
        print(f"Error extracting JSON: {e}")
        return None

# Comment out the web_search_node function
# def web_search_node(state: AgentState) -> Dict[str, Any]:
#     """Node that executes the web search tool."""
#     print("Web Search Tool Called...\n\n")
#     
#     # Extract tool arguments
#     action_input = state.get("action_input", {})
#     print(f"Web search action_input: {action_input}")
#     
#     # Try different ways to extract the query
#     query = ""
#     if isinstance(action_input, dict):
#         query = action_input.get("query", "")
#     elif isinstance(action_input, str):
#         query = action_input
#     
#     print(f"Searching for: '{query}'")
#     
#     # Call the search function with retry logic
#     result = safe_web_search(query)
#     print(f"Search result: {result}")  # Print the full result for debugging
#     
#     # Check if we hit rate limits and add a helpful note
#     if "rate limit" in result.lower() or "ratelimit" in result.lower():
#         result += "\n\nNote: You can use your internal knowledge to provide a response since the search is rate limited."
#     
#     # Format the observation to continue the ReAct cycle
#     # Don't include "Observation:" as the assistant is stopped at this token
#     observation = result
#     
#     # Create a tool message with the result
#     tool_message = AIMessage(
#         content=f"Observation: {observation}"
#     )
#     
#     # Print the observation that will be sent back to the assistant
#     print("\n=== TOOL OBSERVATION ===")
#     print(tool_message.content[:500] + "..." if len(tool_message.content) > 500 else tool_message.content)
#     print("=== END OBSERVATION ===\n")
#     
#     # Return the updated state
#     return {
#         "messages": state["messages"] + [tool_message],
#         "current_tool": None,  # Reset the current tool
#         "action_input": None   # Clear the action input
#     }

def python_code_node(state: AgentState) -> Dict[str, Any]:
    """Node that executes Python code."""
    print("\n=== Python Code Node ===")
    
    try:
        # Extract tool arguments
        action_input = state.get("action_input", {})
        print(f"Input: {action_input.get('code', '')[:100]}...")
        
        # Get the code string
        code = ""
        if isinstance(action_input, dict):
            code = action_input.get("code", "")
        elif isinstance(action_input, str):
            code = action_input
        
        print(f"Original code field (first 100 chars): {code[:100]}")
        
        def extract_code_from_json(json_str):
            """Recursively extract code from nested JSON structures."""
            try:
                parsed = json.loads(json_str)
                if isinstance(parsed, dict):
                    # Check for direct code field
                    if "code" in parsed:
                        return parsed["code"]
                    # Check for nested action_input structure
                    if "action_input" in parsed:
                        inner_input = parsed["action_input"]
                        if isinstance(inner_input, dict):
                            if "code" in inner_input:
                                return inner_input["code"]
                            # If inner_input is also JSON string, recurse
                            if isinstance(inner_input.get("code", ""), str) and inner_input["code"].strip().startswith("{"):
                                return extract_code_from_json(inner_input["code"])
                return json_str
            except:
                return json_str
        
        # Handle nested JSON structures
        if isinstance(code, str) and code.strip().startswith("{"):
            code = extract_code_from_json(code)
            print("Extracted code from JSON structure")
        
        print(f"Final code to execute: {code[:100]}...")
        
        # Execute the code
        try:
            result = run_python_code(code)
            print(f"Execution successful")
            
            # Format the observation
            tool_message = AIMessage(
                content=f"Observation: {result.strip()}"
            )
            
            # Print the observation that will be sent back to the assistant
            print("=== End Python Code Node ===\n")
            
            # Return the updated state
            return {
                "messages": state["messages"] + [tool_message],
                "current_tool": None,  # Reset the current tool
                "action_input": None   # Clear the action input
            }
        except Exception as e:
            error_message = f"Error executing Python code: {str(e)}"
            print(error_message)
            tool_message = AIMessage(content=f"Observation: {error_message}")
            print("=== End Python Code Node ===\n")
            return {
                "messages": state["messages"] + [tool_message],
                "current_tool": None,
                "action_input": None
            }
    except Exception as e:
        error_message = f"Error in Python code node: {str(e)}"
        print(error_message)
        tool_message = AIMessage(content=f"Observation: {error_message}")
        print("=== End Python Code Node ===\n")
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }

def webpage_scrape_node(state: AgentState) -> Dict[str, Any]:
    """Node that scrapes content from a specific webpage URL."""
    print("\n=== Webpage Scrape Node ===")
    
    try:
        # Extract tool arguments
        action_input = state.get("action_input", {})
        url = action_input.get("url", "") if isinstance(action_input, dict) else action_input
        print(f"URL: {url}")
        
        # Safety check - don't run with empty URL
        if not url:
            result = "Error: No URL provided. Please provide a valid URL to scrape."
        else:
            # Call the webpage scraping function
            result = scrape_webpage(url)
        
        # Format the observation
        tool_message = AIMessage(
            content=f"Observation: {result.strip()}"
        )
        
        print("=== End Webpage Scrape Node ===\n")
        
        # Return the updated state
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }
    except Exception as e:
        error_message = f"Error in webpage scrape node: {str(e)}"
        print(error_message)
        tool_message = AIMessage(content=f"Observation: {error_message}")
        print("=== End Webpage Scrape Node ===\n")
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }

def wikipedia_search_node(state: AgentState) -> Dict[str, Any]:
    """Node that processes Wikipedia search requests."""
    print("\n=== Wikipedia Search Node ===")
    
    try:
        # Extract tool arguments
        action_input = state.get("action_input", {})
        query = action_input.get("query", "") if isinstance(action_input, dict) else action_input
        num_results = action_input.get("num_results", 3) if isinstance(action_input, dict) else 3
        print(f"Query: {query} (max results: {num_results})")
        
        # Safety check - don't run with empty query
        if not query:
            result = "Error: No search query provided. Please provide a valid query for Wikipedia search."
        else:
            # Call the Wikipedia search function
            result = wikipedia_search(query, num_results)
        
        # Format the observation
        tool_message = AIMessage(
            content=f"Observation: {result.strip()}"
        )
        
        print("=== End Wikipedia Search Node ===\n")
        
        # Return the updated state
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }
    except Exception as e:
        error_message = f"Error in Wikipedia search node: {str(e)}"
        print(error_message)
        tool_message = AIMessage(content=f"Observation: {error_message}")
        print("=== End Wikipedia Search Node ===\n")
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }

def tavily_search_node(state: AgentState) -> Dict[str, Any]:
    """Node that processes Tavily search requests."""
    print("\n=== Tavily Search Node ===")
    
    try:
        # Extract tool arguments
        action_input = state.get("action_input", {})
        query = action_input.get("query", "") if isinstance(action_input, dict) else action_input
        search_depth = action_input.get("search_depth", "basic") if isinstance(action_input, dict) else "basic"
        print(f"Query: {query} (depth: {search_depth})")
        
        # Safety check - don't run with empty query
        if not query:
            result = "Error: No search query provided. Please provide a valid query for Tavily search."
        else:
            # Call the Tavily search function
            result = tavily_search(query, search_depth)
        
        # Format the observation
        tool_message = AIMessage(
            content=f"Observation: {result.strip()}"
        )
        
        print("=== End Tavily Search Node ===\n")
        
        # Return the updated state
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }
    except Exception as e:
        error_message = f"Error in Tavily search node: {str(e)}"
        print(error_message)
        tool_message = AIMessage(content=f"Observation: {error_message}")
        print("=== End Tavily Search Node ===\n")
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }

def arxiv_search_node(state: AgentState) -> Dict[str, Any]:
    """Node that processes ArXiv search requests."""
    print("\n=== ArXiv Search Node ===")
    
    try:
        # Extract tool arguments
        action_input = state.get("action_input", {})
        query = action_input.get("query", "") if isinstance(action_input, dict) else action_input
        max_results = action_input.get("max_results", 5) if isinstance(action_input, dict) else 5
        print(f"Query: {query} (max results: {max_results})")
        
        # Safety check - don't run with empty query
        if not query:
            result = "Error: No search query provided. Please provide a valid query for ArXiv search."
        else:
            # Call the ArXiv search function
            result = arxiv_search(query, max_results)
        
        # Format the observation
        tool_message = AIMessage(
            content=f"Observation: {result.strip()}"
        )
        
        print("=== End ArXiv Search Node ===\n")
        
        # Return the updated state
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }
    except Exception as e:
        error_message = f"Error in ArXiv search node: {str(e)}"
        print(error_message)
        tool_message = AIMessage(content=f"Observation: {error_message}")
        print("=== End ArXiv Search Node ===\n")
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }

def supabase_operation_node(state: AgentState) -> Dict[str, Any]:
    """Node that processes Supabase database operations."""
    print("\n=== Supabase Operation Node ===")
    
    try:
        # Extract tool arguments
        action_input = state.get("action_input", {})
        operation_type = action_input.get("operation_type", "") if isinstance(action_input, dict) else ""
        table = action_input.get("table", "") if isinstance(action_input, dict) else ""
        print(f"Operation: {operation_type} on table {table}")
        
        # Safety check
        if not operation_type or not table:
            result = "Error: Both operation_type and table are required. operation_type should be one of: insert, select, update, delete"
        else:
            # Call the Supabase operation function
            result = supabase_operation(operation_type, table, action_input.get("data"), action_input.get("filters"))
        
        # Format the observation
        tool_message = AIMessage(
            content=f"Observation: {result.strip()}"
        )
        
        print("=== End Supabase Operation Node ===\n")
        
        # Return the updated state
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }
    except Exception as e:
        error_message = f"Error in Supabase operation node: {str(e)}"
        print(error_message)
        tool_message = AIMessage(content=f"Observation: {error_message}")
        print("=== End Supabase Operation Node ===\n")
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }

def excel_to_text_node(state: AgentState) -> Dict[str, Any]:
    """Node that processes Excel to Markdown table conversions."""
    print("\n=== Excel to Text Node ===")
    
    try:
        # Extract tool arguments
        action_input = state.get("action_input", {})
        excel_path = action_input.get("excel_path", "") if isinstance(action_input, dict) else ""
        sheet_name = action_input.get("sheet_name") if isinstance(action_input, dict) else None
        print(f"File: {excel_path} (sheet: {sheet_name or 'default'})")
        
        # Safety check
        if not excel_path:
            result = "Error: Excel file path is required"
        else:
            # Call the Excel to text function
            result = excel_to_text(excel_path, sheet_name, action_input.get("file_content"))
        
        # Format the observation
        tool_message = AIMessage(
            content=f"Observation: {result.strip()}"
        )
        
        print("=== End Excel to Text Node ===\n")
        
        # Return the updated state
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }
    except Exception as e:
        error_message = f"Error in Excel to text node: {str(e)}"
        print(error_message)
        tool_message = AIMessage(content=f"Observation: {error_message}")
        print("=== End Excel to Text Node ===\n")
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }

def process_youtube_video_node(state: AgentState) -> Dict[str, Any]:
    """Node that processes YouTube videos."""
    print("\n=== YouTube Video Processing Node ===")
    
    try:
        # Extract tool arguments
        action_input = state.get("action_input", {})
        url = action_input.get("url", "") if isinstance(action_input, dict) else action_input
        summarize = action_input.get("summarize", True) if isinstance(action_input, dict) else True
        print(f"URL: {url} (summarize: {summarize})")
        
        # Safety check - don't run with empty URL
        if not url:
            result = "Error: No URL provided. Please provide a valid YouTube URL."
        elif not url.startswith(("http://", "https://")) or not ("youtube.com" in url or "youtu.be" in url):
            result = f"Error: Invalid YouTube URL format: {url}. Please provide a valid URL starting with http:// or https:// and containing youtube.com or youtu.be."
        else:
            # Call the YouTube processing function
            try:
                result = process_youtube_video(url, summarize)
            except Exception as e:
                result = f"Error processing YouTube video: {str(e)}\n\nThis could be due to:\n- The video is private or has been removed\n- Network connectivity issues\n- YouTube API changes\n- Rate limiting"
        
        # Format the observation
        tool_message = AIMessage(
            content=f"Observation: {result.strip()}"
        )
        
        print("=== End YouTube Video Processing Node ===\n")
        
        # Return the updated state
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }
    except Exception as e:
        error_message = f"Error in YouTube video processing node: {str(e)}"
        print(error_message)
        tool_message = AIMessage(content=f"Observation: {error_message}")
        print("=== End YouTube Video Processing Node ===\n")
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }

def transcribe_audio_node(state: AgentState) -> Dict[str, Any]:
    """Node that processes audio transcription requests."""
    print("\n=== Audio Transcription Node ===")
    
    try:
        # Extract tool arguments
        action_input = state.get("action_input", {})
        audio_path = action_input.get("audio_path", "") if isinstance(action_input, dict) else ""
        language = action_input.get("language") if isinstance(action_input, dict) else None
        print(f"File: {audio_path} (language: {language or 'auto-detect'})")
        
        # Safety check
        if not audio_path:
            result = "Error: Audio file path is required"
        else:
            # Call the transcribe audio function
            result = transcribe_audio(audio_path, action_input.get("file_content"), language)
        
        # Format the observation
        tool_message = AIMessage(
            content=f"Observation: {result.strip()}"
        )
        
        print("=== End Audio Transcription Node ===\n")
        
        # Return the updated state
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }
    except Exception as e:
        error_message = f"Error in audio transcription node: {str(e)}"
        print(error_message)
        tool_message = AIMessage(content=f"Observation: {error_message}")
        print("=== End Audio Transcription Node ===\n")
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }

def process_image_node(state: AgentState) -> Dict[str, Any]:
    """Node that processes image analysis requests."""
    print("\n=== Image Processing Node ===")
    
    try:
        # Extract tool arguments
        action_input = state.get("action_input", {})
        image_path = action_input.get("image_path", "") if isinstance(action_input, dict) else ""
        image_url = action_input.get("image_url") if isinstance(action_input, dict) else None
        analyze_content = action_input.get("analyze_content", True) if isinstance(action_input, dict) else True
        print(f"Source: {image_url or image_path} (analyze: {analyze_content})")
        
        # Safety check
        if not image_path and not image_url:
            result = "Error: Either image path or image URL is required"
        else:
            # Call the process image function
            result = process_image(image_path, image_url, action_input.get("file_content"), analyze_content)
        
        # Format the observation
        tool_message = AIMessage(
            content=f"Observation: {result.strip()}"
        )
        
        print("=== End Image Processing Node ===\n")
        
        # Return the updated state
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }
    except Exception as e:
        error_message = f"Error in image processing node: {str(e)}"
        print(error_message)
        tool_message = AIMessage(content=f"Observation: {error_message}")
        print("=== End Image Processing Node ===\n")
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }

def read_file_node(state: AgentState) -> Dict[str, Any]:
    """Node that reads text file contents."""
    print("\n=== File Reading Node ===")
    
    try:
        # Extract tool arguments
        action_input = state.get("action_input", {})
        file_path = action_input.get("file_path", "") if isinstance(action_input, dict) else ""
        line_start = action_input.get("line_start") if isinstance(action_input, dict) else None
        line_end = action_input.get("line_end") if isinstance(action_input, dict) else None
        print(f"File: {file_path} (lines: {line_start}-{line_end if line_end else 'end'})")
        
        # Safety check
        if not file_path:
            result = "Error: File path is required"
        else:
            # Call the read file function
            result = read_file(file_path, action_input.get("file_content"), line_start, line_end)
        
        # Format the observation
        tool_message = AIMessage(
            content=f"Observation: {result.strip()}"
        )
        
        print("=== End File Reading Node ===\n")
        
        # Return the updated state
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }
    except Exception as e:
        error_message = f"Error in file reading node: {str(e)}"
        print(error_message)
        tool_message = AIMessage(content=f"Observation: {error_message}")
        print("=== End File Reading Node ===\n")
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }

def process_online_document_node(state: AgentState) -> Dict[str, Any]:
    """Node that processes online PDFs and images."""
    print("\n=== Online Document Processing Node ===")
    
    try:
        # Extract tool arguments
        action_input = state.get("action_input", {})
        url = action_input.get("url", "") if isinstance(action_input, dict) else action_input
        doc_type = action_input.get("doc_type", "auto") if isinstance(action_input, dict) else "auto"
        print(f"URL: {url} (type: {doc_type})")
        
        # Safety check - don't run with empty URL
        if not url:
            result = "Error: No URL provided. Please provide a valid URL to process."
        elif not url.startswith(("http://", "https://")):
            result = f"Error: Invalid URL format: {url}. Please provide a valid URL starting with http:// or https://."
        else:
            # Call the online document processing function
            try:
                result = process_online_document(url, doc_type)
            except Exception as e:
                result = f"Error processing online document: {str(e)}\n\nThis could be due to:\n- The document is not accessible\n- Network connectivity issues\n- Unsupported document type\n- Rate limiting"
        
        # Format the observation
        tool_message = AIMessage(
            content=f"Observation: {result.strip()}"
        )
        
        print("=== End Online Document Processing Node ===\n")
        
        # Return the updated state
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }
    except Exception as e:
        error_message = f"Error in online document processing node: {str(e)}"
        print(error_message)
        tool_message = AIMessage(content=f"Observation: {error_message}")
        print("=== End Online Document Processing Node ===\n")
        return {
            "messages": state["messages"] + [tool_message],
            "current_tool": None,
            "action_input": None
        }

# Router function to direct to the correct tool
def router(state: AgentState) -> str:
    """Route to the appropriate tool based on the current_tool field."""
    tool = state.get("current_tool")
    action_input = state.get("action_input")
    print(f"Routing to: {tool}")
    print(f"Router received action_input: {action_input}")
    
    if tool == "python_code":
        return "python_code"
    elif tool == "webpage_scrape":
        return "webpage_scrape"
    elif tool == "wikipedia_search":
        return "wikipedia_search"
    elif tool == "tavily_search":
        return "tavily_search"
    elif tool == "arxiv_search":
        return "arxiv_search"
    elif tool == "supabase_operation":
        return "supabase_operation"
    elif tool == "excel_to_text":
        return "excel_to_text"
    elif tool == "process_youtube_video":
        return "process_youtube_video"
    elif tool == "transcribe_audio":
        return "transcribe_audio"
    elif tool == "process_image":
        return "process_image"
    elif tool == "read_file":
        return "read_file"
    elif tool == "process_online_document":
        return "process_online_document"
    else:
        return "end"

# Create the graph
def create_agent_graph() -> StateGraph:
    """Create the complete agent graph with individual tool nodes."""
    builder = StateGraph(AgentState)

    # Define nodes: these do the work
    builder.add_node("assistant", assistant)
    builder.add_node("python_code", python_code_node)
    builder.add_node("webpage_scrape", webpage_scrape_node)
    builder.add_node("wikipedia_search", wikipedia_search_node)
    builder.add_node("tavily_search", tavily_search_node)
    builder.add_node("arxiv_search", arxiv_search_node)
    builder.add_node("supabase_operation", supabase_operation_node)
    builder.add_node("excel_to_text", excel_to_text_node)
    builder.add_node("process_youtube_video", process_youtube_video_node)
    builder.add_node("transcribe_audio", transcribe_audio_node)
    builder.add_node("process_image", process_image_node)
    builder.add_node("read_file", read_file_node)
    builder.add_node("process_online_document", process_online_document_node)

    # Define edges: these determine how the control flow moves
    builder.add_edge(START, "assistant")
    
    # Debug the state passing
    def debug_state(state):
        print("\n=== DEBUG STATE ===")
        print(f"State keys: {state.keys()}")
        print(f"Current tool: {state.get('current_tool')}")
        print(f"Action input: {state.get('action_input')}")
        print("=== END DEBUG ===\n")
        return state
    
    # Add a checkpoint between nodes to track state
    builder.add_node("debug", debug_state)
    
    # Conditional edge from assistant to debug
    builder.add_edge("assistant", "debug")
    
    # Conditional edge from debug to tools or end
    builder.add_conditional_edges(
        "debug",
        router,
        {
            "python_code": "python_code",
            "webpage_scrape": "webpage_scrape",
            "wikipedia_search": "wikipedia_search",
            "tavily_search": "tavily_search",
            "arxiv_search": "arxiv_search",
            "supabase_operation": "supabase_operation",
            "excel_to_text": "excel_to_text",
            "process_youtube_video": "process_youtube_video",
            "transcribe_audio": "transcribe_audio",
            "process_image": "process_image",
            "read_file": "read_file",
            "process_online_document": "process_online_document",
            "end": END
        }
    )
    
    # Tools always go back to assistant
    builder.add_edge("python_code", "assistant")
    builder.add_edge("webpage_scrape", "assistant")
    builder.add_edge("wikipedia_search", "assistant")
    builder.add_edge("tavily_search", "assistant")
    builder.add_edge("arxiv_search", "assistant")
    builder.add_edge("supabase_operation", "assistant")
    builder.add_edge("excel_to_text", "assistant")
    builder.add_edge("process_youtube_video", "assistant")
    builder.add_edge("transcribe_audio", "assistant")
    builder.add_edge("process_image", "assistant")
    builder.add_edge("read_file", "assistant")
    builder.add_edge("process_online_document", "assistant")
    
    # Compile the graph
    return builder.compile()

# Main agent class that integrates with your existing app.py
class ScholarAI:
    def __init__(self, max_iterations=35, temperature=0.1, max_tokens=2000, model="gpt-4o-mini", apify_api_token=None):
        # Check for OpenAI API key
        if not os.getenv("OPENAI_API_KEY"):
            raise ValueError("OpenAI API key not found. Please set the OPENAI_API_KEY environment variable.")
            
        try:
            # Test the API key with a simple request
            test_llm = ChatOpenAI(
                model=model,
                temperature=temperature,
                max_tokens=max_tokens
            )
            test_llm.invoke("test")  # This will fail if API key is invalid
        except Exception as e:
            error_msg = str(e).lower()
            if "invalid_api_key" in error_msg or "incorrect_api_key" in error_msg:
                raise ValueError("Invalid OpenAI API key. Please check your API key and try again.")
            elif "rate_limit" in error_msg or "quota" in error_msg:
                raise ValueError("OpenAI API rate limit exceeded or quota reached. Please try again later.")
            else:
                raise ValueError(f"Error initializing OpenAI client: {str(e)}")
        
        self.graph = create_agent_graph()
        self.tools = tools_config
        self.max_iterations = max_iterations  # Maximum iterations for the graph
        
        # Update the global llm instance with the specified parameters
        global llm
        llm = ChatOpenAI(
            model=model,
            temperature=temperature,
            max_tokens=max_tokens
        )
        
        # Set Apify API token if provided
        if apify_api_token:
            os.environ["APIFY_API_TOKEN"] = apify_api_token
            print("Apify API token set successfully")
        
        print(f"ScholarAI initialized with model={model}, temperature={temperature}, max_tokens={max_tokens}")
    
    def __call__(self, question: str, attachments: dict = None) -> str:
        """
        Process a question and return an answer.
        
        Args:
            question: The user's question text
            attachments: Optional dictionary of attachments with keys as names and values as base64-encoded content
        """
        # Process attachments if provided
        attachment_info = ""
        if attachments and isinstance(attachments, dict) and len(attachments) > 0:
            attachment_names = list(attachments.keys())
            attachment_info = f"\n\nI've attached the following files: {', '.join(attachment_names)}. "
            
            # Add different instructions based on detected file types
            excel_files = [name for name in attachment_names if name.endswith(('.xlsx', '.xls'))]
            if excel_files:
                attachment_info += f"Use the excel_to_text tool with the file_content parameter to process the Excel files."
        
        # Initialize the state with the question and attachment info
        question_with_attachments = question + attachment_info if attachment_info else question
        
        initial_state = {
            "messages": [HumanMessage(content=f"Question: {question_with_attachments}")],
            "current_tool": None,
            "action_input": None,
            "iteration_count": 0,  # Initialize iteration_count
            "attachments": attachments or {}  # Store attachments in the state
        }
        
        # Run the graph
        print(f"Starting graph execution with question: {question}")
        if attachments:
            print(f"Included attachments: {list(attachments.keys())}")
        
        try:
            # Set a reasonable recursion limit based on max_iterations
            result = self.graph.invoke(initial_state, {"recursion_limit": self.max_iterations})
            
            # Print the final state for debugging
            print(f"Final state keys: {result.keys()}")
            print(f"Final message count: {len(result['messages'])}")
            
            # Extract the final message and return just the final answer
            final_message = result["messages"][-1].content
            print("Final message: ", final_message)
            
            # Extract just the final answer part
            if "Final Answer:" in final_message:
                final_answer = final_message.split("Final Answer:", 1)[1].strip()
                return final_answer
            
            return final_message
        
        except Exception as e:
            print(f"Error processing question: {str(e)}")
            # Otherwise return the error
            return f"I encountered an error while processing your question: {str(e)}"

# Example usage:
if __name__ == "__main__":
    agent = ScholarAI(max_iterations=25)
    response = agent("""The attached Excel file contains the sales of menu items for a local fast-food chain. What were the total sales that the chain made from food (not including drinks)? Express your answer in USD with two decimal places. TEMPP\excel.xlsx """)
    print("\nFinal Response:")
    print(response)