File size: 110,463 Bytes
b2c2a89
77830e0
7db4283
 
b2c2a89
f234f8e
7db4283
f3518c5
77830e0
f3518c5
 
7db4283
f3518c5
cd70b5d
5cb387e
f3518c5
7db4283
cba6b3c
f3518c5
77830e0
f3518c5
 
cd70b5d
 
77830e0
cd70b5d
 
 
 
77830e0
 
 
68b21f6
77830e0
 
 
 
 
68b21f6
77830e0
 
 
f234f8e
 
 
f3518c5
77830e0
7f67a14
a583ded
7fdfe87
 
 
 
a583ded
 
 
ac10cac
7fdfe87
a583ded
 
5cb387e
f3518c5
bea78d0
f8272eb
bea78d0
f3518c5
 
 
77830e0
 
 
 
 
 
 
 
 
 
7db4283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fdfe87
7db4283
7fdfe87
7db4283
 
 
 
 
 
 
 
 
 
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2c2a89
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2c2a89
f234f8e
 
 
 
7db4283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77830e0
 
 
 
 
 
 
 
7db4283
 
 
77830e0
 
 
 
7db4283
 
 
77830e0
 
 
 
7db4283
 
 
77830e0
 
 
 
 
f8272eb
77830e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7db4283
 
 
77830e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fdfe87
77830e0
7fdfe87
 
 
 
77830e0
7fdfe87
77830e0
 
7fdfe87
77830e0
 
7fdfe87
77830e0
 
 
 
 
68b21f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fdfe87
 
77830e0
 
 
 
 
7fdfe87
 
77830e0
 
 
 
 
 
 
7fdfe87
77830e0
7fdfe87
77830e0
 
7fdfe87
77830e0
 
 
 
 
 
 
 
 
f8272eb
77830e0
 
 
 
7db4283
 
 
 
 
 
 
 
 
 
 
77830e0
f8272eb
 
 
 
 
 
 
 
 
 
 
77830e0
 
f8272eb
7fdfe87
77830e0
 
 
 
 
7fdfe87
 
 
77830e0
 
 
 
7fdfe87
77830e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff54322
77830e0
 
ff54322
 
 
 
 
77830e0
 
ff54322
 
 
 
 
 
 
77830e0
ff54322
 
 
 
 
 
 
 
 
 
77830e0
 
 
 
 
 
f8272eb
77830e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff54322
77830e0
 
ff54322
 
 
 
 
 
 
77830e0
ff54322
 
 
 
 
 
 
 
 
77830e0
 
 
 
 
a583ded
7db4283
a583ded
 
77830e0
a583ded
 
77830e0
a583ded
 
77830e0
a583ded
77830e0
 
a583ded
7db4283
a583ded
 
 
77830e0
a583ded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77830e0
a583ded
 
 
 
 
77830e0
 
 
a583ded
77830e0
 
7db4283
 
 
 
 
 
 
 
 
cd70b5d
7db4283
 
 
f234f8e
 
 
 
7db4283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77830e0
7db4283
 
77830e0
cd70b5d
7db4283
 
 
 
 
 
ac10cac
 
 
 
 
b2c2a89
 
 
 
25bce68
b2c2a89
 
 
 
 
 
 
 
 
 
dbb1ef4
b2c2a89
 
 
dbb1ef4
b2c2a89
 
 
378caa6
 
 
 
 
 
 
b2c2a89
 
 
 
 
ac10cac
25bce68
dbb1ef4
25bce68
ac10cac
25bce68
ac10cac
 
 
 
 
dbb1ef4
 
 
 
 
 
25bce68
dbb1ef4
25bce68
 
dbb1ef4
ac10cac
7db4283
cd70b5d
ac10cac
cd70b5d
ac10cac
cd70b5d
f2fc66c
7db4283
 
 
 
 
f2fc66c
7db4283
 
 
 
 
 
 
 
93d79eb
ac10cac
68b21f6
ac10cac
7db4283
f2fc66c
77830e0
7db4283
 
7fdfe87
 
f234f8e
7db4283
77830e0
f234f8e
 
 
 
 
 
ac10cac
 
 
 
 
 
 
 
f234f8e
7db4283
 
7fdfe87
7db4283
 
7fdfe87
f234f8e
 
 
 
7db4283
 
7fdfe87
 
 
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
7fdfe87
 
f234f8e
 
 
 
77830e0
f234f8e
7db4283
f234f8e
7db4283
 
7fdfe87
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fdfe87
 
 
 
f234f8e
7fdfe87
f234f8e
7fdfe87
 
 
 
f234f8e
 
 
 
 
7fdfe87
 
 
 
f234f8e
 
 
 
 
 
 
 
 
7fdfe87
 
f234f8e
 
7fdfe87
 
f234f8e
7fdfe87
f234f8e
7fdfe87
 
f234f8e
 
 
 
 
 
 
 
 
ac10cac
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac10cac
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fdfe87
 
f234f8e
 
7db4283
f234f8e
7fdfe87
7db4283
7fdfe87
 
 
7db4283
7fdfe87
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac10cac
 
 
 
f234f8e
ac10cac
 
 
 
 
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
7fdfe87
f234f8e
 
 
 
 
 
ac10cac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fdfe87
 
 
7db4283
 
7fdfe87
f234f8e
7fdfe87
7db4283
 
7fdfe87
 
7db4283
 
 
 
f234f8e
 
7fdfe87
7db4283
 
7fdfe87
 
 
f234f8e
7fdfe87
7db4283
 
7fdfe87
 
7db4283
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7db4283
7fdfe87
f234f8e
 
 
77830e0
 
7db4283
77830e0
 
7db4283
 
 
 
 
 
7f67a14
 
7db4283
 
 
 
 
f8272eb
7db4283
 
cba6b3c
f8272eb
7db4283
 
 
 
 
 
cba6b3c
7db4283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b93f165
 
 
 
7db4283
 
b93f165
7db4283
 
 
 
 
 
 
 
 
 
b93f165
7db4283
 
b93f165
 
f234f8e
 
ac10cac
f234f8e
 
 
7db4283
 
 
b93f165
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cba6b3c
7db4283
 
 
cba6b3c
7db4283
 
f234f8e
 
 
 
 
 
 
 
7db4283
 
cba6b3c
7db4283
 
7f67a14
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3518c5
7db4283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3518c5
7db4283
 
 
 
 
 
 
 
 
 
f234f8e
77830e0
7db4283
 
f234f8e
 
 
 
 
 
 
 
 
f3518c5
 
 
 
7db4283
 
f234f8e
7db4283
 
f3518c5
7db4283
 
 
 
 
 
 
 
 
cd70b5d
f3518c5
 
 
 
 
7db4283
 
 
 
 
 
 
 
 
 
 
 
 
 
77830e0
7db4283
 
 
 
77830e0
 
 
5cb387e
7db4283
77830e0
 
 
7db4283
 
 
77830e0
7db4283
 
 
 
5cb387e
7db4283
 
 
b93f165
 
 
 
 
7db4283
 
 
b93f165
7db4283
b93f165
 
 
f234f8e
 
ac10cac
f234f8e
 
 
7db4283
 
 
b93f165
f234f8e
 
 
 
 
 
 
7db4283
 
 
 
5cb387e
7db4283
 
f234f8e
 
 
 
 
 
 
 
7db4283
 
 
 
 
f234f8e
 
 
7db4283
 
 
 
 
 
 
77830e0
7db4283
 
 
 
 
 
 
 
b2c2a89
 
 
 
 
 
 
 
 
 
 
7db4283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77830e0
 
 
 
 
 
f234f8e
f3518c5
77830e0
f234f8e
 
 
 
 
 
 
 
 
7db4283
f234f8e
 
 
 
 
 
 
 
7db4283
 
f234f8e
7db4283
f234f8e
 
 
 
 
 
 
 
 
 
 
7db4283
77830e0
f234f8e
 
 
 
 
7db4283
 
 
f234f8e
 
 
 
7db4283
 
 
 
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7db4283
 
f234f8e
7db4283
 
7fdfe87
f234f8e
 
 
 
7db4283
 
 
 
 
 
 
 
 
 
 
 
a583ded
7db4283
 
 
a583ded
7db4283
 
 
 
 
 
 
 
 
 
 
 
f234f8e
7db4283
 
 
 
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac10cac
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7db4283
 
 
 
 
 
f234f8e
 
7db4283
 
 
 
 
f234f8e
7db4283
 
 
 
 
 
 
 
 
 
 
 
f234f8e
7db4283
 
 
 
b2c2a89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7db4283
 
 
 
 
 
 
 
 
 
 
 
a583ded
7db4283
 
 
 
 
 
 
 
 
 
 
 
f234f8e
7db4283
 
 
 
f234f8e
7db4283
 
 
 
 
 
 
 
 
 
f234f8e
7db4283
 
 
 
 
 
f234f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7db4283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f234f8e
 
 
 
 
 
 
7db4283
7fdfe87
 
 
 
7db4283
 
 
 
 
 
 
 
 
f234f8e
 
 
7fdfe87
 
b2c2a89
 
7fdfe87
 
 
7db4283
 
 
 
 
 
 
 
 
 
77830e0
7db4283
 
 
 
 
 
 
77830e0
7db4283
 
77830e0
7db4283
 
 
 
 
 
 
 
 
 
 
5cb387e
7db4283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2c2a89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7db4283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
import asyncio
import base64
import hashlib
import io
import random
import re
import reprlib
import uuid
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, AsyncGenerator

import orjson
from fastapi import APIRouter, Depends, HTTPException, Request, status
from fastapi.responses import StreamingResponse
from gemini_webapi import ModelOutput
from gemini_webapi.client import ChatSession
from gemini_webapi.constants import Model
from gemini_webapi.types.image import GeneratedImage, Image
from loguru import logger

from ..models import (
    ChatCompletionRequest,
    ContentItem,
    ConversationInStore,
    Message,
    ModelData,
    ModelListResponse,
    ResponseCreateRequest,
    ResponseCreateResponse,
    ResponseImageGenerationCall,
    ResponseImageTool,
    ResponseInputContent,
    ResponseInputItem,
    ResponseOutputContent,
    ResponseOutputMessage,
    ResponseToolCall,
    ResponseToolChoice,
    ResponseUsage,
    Tool,
    ToolChoiceFunction,
    ToolChoiceFunctionDetail,
    ToolFunctionDefinition,
    AnthropicMessageRequest,
)
from ..services import GeminiClientPool, GeminiClientWrapper, LMDBConversationStore
from ..utils import g_config
from ..utils.helper import (
    TOOL_HINT_LINE_END,
    TOOL_HINT_LINE_START,
    TOOL_HINT_STRIPPED,
    TOOL_WRAP_HINT,
    estimate_tokens,
    extract_image_dimensions,
    extract_tool_calls,
    retry_with_backoff,
    strip_system_hints,
    text_from_message,
)
from .middleware import get_image_store_dir, get_image_token, get_temp_dir, verify_api_key

MAX_CHARS_PER_REQUEST = int(g_config.gemini.max_chars_per_request * 0.9)
METADATA_TTL_MINUTES = 15

router = APIRouter()


@dataclass
class StructuredOutputRequirement:
    """Represents a structured response request from the client."""

    schema_name: str
    schema: dict[str, Any]
    instruction: str
    raw_format: dict[str, Any]


# --- Helper Functions ---


async def _image_to_base64(
    image: Image, temp_dir: Path
) -> tuple[str, int | None, int | None, str, str]:
    """Persist an image provided by gemini_webapi and return base64 plus dimensions, filename, and hash."""
    if isinstance(image, GeneratedImage):
        try:
            saved_path = await image.save(path=str(temp_dir), full_size=True)
        except Exception as e:
            logger.warning(
                f"Failed to download full-size GeneratedImage, retrying with default size: {e}"
            )
            saved_path = await image.save(path=str(temp_dir), full_size=False)
    else:
        saved_path = await image.save(path=str(temp_dir))

    if not saved_path:
        raise ValueError("Failed to save generated image")

    original_path = Path(saved_path)
    random_name = f"img_{uuid.uuid4().hex}{original_path.suffix}"
    new_path = temp_dir / random_name
    original_path.rename(new_path)

    data = new_path.read_bytes()
    width, height = extract_image_dimensions(data)
    filename = random_name
    file_hash = hashlib.sha256(data).hexdigest()
    return base64.b64encode(data).decode("ascii"), width, height, filename, file_hash


def _calculate_usage(
    messages: list[Message],
    assistant_text: str | None,
    tool_calls: list[Any] | None,
) -> tuple[int, int, int]:
    """Calculate prompt, completion and total tokens consistently."""
    prompt_tokens = sum(estimate_tokens(text_from_message(msg)) for msg in messages)
    tool_args_text = ""
    if tool_calls:
        for call in tool_calls:
            if hasattr(call, "function"):
                tool_args_text += call.function.arguments or ""
            elif isinstance(call, dict):
                tool_args_text += call.get("function", {}).get("arguments", "")

    completion_basis = assistant_text or ""
    if tool_args_text:
        completion_basis = (
            f"{completion_basis}\n{tool_args_text}" if completion_basis else tool_args_text
        )

    completion_tokens = estimate_tokens(completion_basis)
    return prompt_tokens, completion_tokens, prompt_tokens + completion_tokens


def _create_responses_standard_payload(
    response_id: str,
    created_time: int,
    model_name: str,
    detected_tool_calls: list[Any] | None,
    image_call_items: list[ResponseImageGenerationCall],
    response_contents: list[ResponseOutputContent],
    usage: ResponseUsage,
    request: ResponseCreateRequest,
    normalized_input: Any,
) -> ResponseCreateResponse:
    """Unified factory for building ResponseCreateResponse objects."""
    message_id = f"msg_{uuid.uuid4().hex}"
    tool_call_items: list[ResponseToolCall] = []
    if detected_tool_calls:
        tool_call_items = [
            ResponseToolCall(
                id=call.id if hasattr(call, "id") else call["id"],
                status="completed",
                function=call.function if hasattr(call, "function") else call["function"],
            )
            for call in detected_tool_calls
        ]

    return ResponseCreateResponse(
        id=response_id,
        created_at=created_time,
        model=model_name,
        output=[
            ResponseOutputMessage(
                id=message_id,
                type="message",
                role="assistant",
                content=response_contents,
            ),
            *tool_call_items,
            *image_call_items,
        ],
        status="completed",
        usage=usage,
        input=normalized_input or None,
        metadata=request.metadata or None,
        tools=request.tools,
        tool_choice=request.tool_choice,
    )


def _create_chat_completion_standard_payload(
    completion_id: str,
    created_time: int,
    model_name: str,
    visible_output: str | None,
    tool_calls_payload: list[dict] | None,
    finish_reason: str,
    usage: dict,
) -> dict:
    """Unified factory for building Chat Completion response dictionaries."""
    return {
        "id": completion_id,
        "object": "chat.completion",
        "created": created_time,
        "model": model_name,
        "choices": [
            {
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": visible_output or None,
                    "tool_calls": tool_calls_payload or None,
                },
                "finish_reason": finish_reason,
            }
        ],
        "usage": usage,
    }


def _process_llm_output(
    raw_output_with_think: str,
    raw_output_clean: str,
    structured_requirement: StructuredOutputRequirement | None,
) -> tuple[str, str, list[Any]]:
    """
    Common post-processing logic for Gemini output.
    Returns: (visible_text, storage_output, tool_calls)
    """
    visible_with_think, tool_calls = extract_tool_calls(raw_output_with_think)
    if tool_calls:
        logger.debug(f"Detected {len(tool_calls)} tool call(s) in model output.")

    visible_output = visible_with_think.strip()

    storage_output, _ = extract_tool_calls(raw_output_clean)
    storage_output = storage_output.strip()

    if structured_requirement:
        cleaned_for_json = LMDBConversationStore.remove_think_tags(visible_output)
        if cleaned_for_json:
            try:
                structured_payload = orjson.loads(cleaned_for_json)
                canonical_output = orjson.dumps(structured_payload).decode("utf-8")
                visible_output = canonical_output
                storage_output = canonical_output
                logger.debug(
                    f"Structured response fulfilled (schema={structured_requirement.schema_name})."
                )
            except orjson.JSONDecodeError:
                logger.warning(
                    f"Failed to decode JSON for structured response (schema={structured_requirement.schema_name})."
                )
    else:
        # Apply standard cleaning (citations, links, html entities) to the final output
        # treating it as a ModelOutput object wrapper or just reusing the static method logic
        # Since extract_output expects ModelOutput, we can use the helper method logic directly
        # or construct a dummy object.
        # Actually, let's just use the static method with a dummy object for convenience,
        # or better: refactor extract_output to take string?
        # For minimum blast radius, we'll wrap it.
        # But wait, extract_output handles "thoughts" too. 
        # Here we already separated thoughts.
        # We just want to clean `visible_output`.
        
        # We can expose a `clean_text` static method in implementation, but strict instruction says "modify chat.py".
        # Let's import the cleaning functions? They are internal to client.py.
        # Better: Use GeminiClientWrapper.extract_output with a dummy ModelOutput.
        
        dummy_output = ModelOutput(metadata=[], candidates=[{"rcid": "dummy", "text": visible_output}])
        # We don't want to re-add thoughts (include_thoughts=False) because we handle them separately?
        # extract_output puts thoughts in <think> tags if include_thoughts=True.
        # Here `visible_output` DOES NOT have thoughts (they are in `raw_output_with_think`'s <think> part).
        # Wait, `extract_tool_calls` returned `visible_with_think`.
        # `visible_output` = `visible_with_think.strip()`.
        # So `visible_output` MIGHT have <think> tags if they weren't stripped?
        # Actually `extract_tool_calls` doesn't strip <think>.
        # So `visible_output` HAS thoughts.
        # So we can pass it to extract_output? 
        # extract_output re-adds <think> from `response.thoughts`.
        # If we pass `text=visible_output` (which has <think>), and `thoughts=None`, and `include_thoughts=False`.
        # It should process `text`.
        
        # BUT `extract_output` does NOT strip existing <think> tags from `text`.
        # So it's safe.
        
        try:
            visible_output = GeminiClientWrapper.extract_output(dummy_output, include_thoughts=False)
            # Apply to storage_output too? 
            # storage_output usually should match visible_output for history consistency.
            storage_output = GeminiClientWrapper.extract_output(
                 ModelOutput(metadata=[], candidates=[{"rcid": "dummy", "text": storage_output}]), 
                 include_thoughts=False
            )
        except Exception as e:
            logger.warning(f"Failed to clean output with GeminiClientWrapper: {e}")

    return visible_output, storage_output, tool_calls


def _persist_conversation(
    db: LMDBConversationStore,
    model_name: str,
    client_id: str,
    metadata: list[str | None],
    messages: list[Message],
    storage_output: str | None,
    tool_calls: list[Any] | None,
) -> str | None:
    """Unified logic to save conversation history to LMDB."""
    try:
        current_assistant_message = Message(
            role="assistant",
            content=storage_output or None,
            tool_calls=tool_calls or None,
        )
        full_history = [*messages, current_assistant_message]
        cleaned_history = db.sanitize_assistant_messages(full_history)

        conv = ConversationInStore(
            model=model_name,
            client_id=client_id,
            metadata=metadata,
            messages=cleaned_history,
        )
        key = db.store(conv)
        logger.debug(f"Conversation saved to LMDB with key: {key[:12]}")
        return key
    except Exception as e:
        logger.warning(f"Failed to save {len(messages) + 1} messages to LMDB: {e}")
        return None


def _build_structured_requirement(
    response_format: dict[str, Any] | None,
) -> StructuredOutputRequirement | None:
    """Translate OpenAI-style response_format into internal instructions."""
    if not response_format or not isinstance(response_format, dict):
        return None

    if response_format.get("type") != "json_schema":
        logger.warning(
            f"Unsupported response_format type requested: {reprlib.repr(response_format)}"
        )
        return None

    json_schema = response_format.get("json_schema")
    if not isinstance(json_schema, dict):
        logger.warning(
            f"Invalid json_schema payload in response_format: {reprlib.repr(response_format)}"
        )
        return None

    schema = json_schema.get("schema")
    if not isinstance(schema, dict):
        logger.warning(
            f"Missing `schema` object in response_format payload: {reprlib.repr(response_format)}"
        )
        return None

    schema_name = json_schema.get("name") or "response"
    strict = json_schema.get("strict", True)

    pretty_schema = orjson.dumps(schema, option=orjson.OPT_SORT_KEYS).decode("utf-8")
    instruction_parts = [
        "You must respond with a single valid JSON document that conforms to the schema shown below.",
        "Do not include explanations, comments, or any text before or after the JSON.",
        f'Schema name: "{schema_name}"',
        "JSON Schema:",
        pretty_schema,
    ]
    if not strict:
        instruction_parts.insert(
            1,
            "The schema allows unspecified fields, but include only what is necessary to satisfy the user's request.",
        )

    instruction = "\n\n".join(instruction_parts)
    return StructuredOutputRequirement(
        schema_name=schema_name,
        schema=schema,
        instruction=instruction,
        raw_format=response_format,
    )


def _build_tool_prompt(
    tools: list[Tool],
    tool_choice: str | ToolChoiceFunction | None,
) -> str:
    """Generate a system prompt chunk describing available tools."""
    if not tools:
        return ""

    lines: list[str] = [
        "You can invoke the following developer tools. Call a tool only when it is required and follow the JSON schema exactly when providing arguments."
    ]

    for tool in tools:
        function = tool.function
        description = function.description or "No description provided."
        lines.append(f"Tool `{function.name}`: {description}")
        if function.parameters:
            schema_text = orjson.dumps(function.parameters, option=orjson.OPT_SORT_KEYS).decode(
                "utf-8"
            )
            lines.append("Arguments JSON schema:")
            lines.append(schema_text)
        else:
            lines.append("Arguments JSON schema: {}")

    if tool_choice == "none":
        lines.append(
            "For this request you must not call any tool. Provide the best possible natural language answer."
        )
    elif tool_choice == "required":
        lines.append(
            "You must call at least one tool before responding to the user. Do not provide a final user-facing answer until a tool call has been issued."
        )
    elif isinstance(tool_choice, ToolChoiceFunction):
        target = tool_choice.function.name
        lines.append(
            f"You are required to call the tool named `{target}`. Do not call any other tool."
        )

    lines.append(
        "When you decide to call a tool you MUST respond with nothing except a single [function_calls] block exactly like the template below."
    )
    lines.append("Do not add text before or after the block.")
    lines.append("[function_calls]")
    lines.append('[call:tool_name]{"argument": "value"}[/call]')
    lines.append("[/function_calls]")
    lines.append(
        "Use double quotes for JSON keys and values. CRITICAL: The content inside [call:...]...[/call] MUST be a raw JSON object. Do not wrap it in ```json blocks or add any conversational text inside the tag."
    )
    lines.append(
        "To call multiple tools, list each [call:tool_name]...[/call] entry sequentially within a single [function_calls] block."
    )
    lines.append(
        "If no tool call is needed, provide a normal response and DO NOT use the [function_calls] tag."
    )

    return "\n".join(lines)


def _build_image_generation_instruction(
    tools: list[ResponseImageTool] | None,
    tool_choice: ResponseToolChoice | None,
) -> str | None:
    """Construct explicit guidance so Gemini emits images when requested."""
    has_forced_choice = tool_choice is not None and tool_choice.type == "image_generation"
    primary = tools[0] if tools else None

    if not has_forced_choice and primary is None:
        return None

    instructions: list[str] = [
        "Image generation is enabled. When the user requests an image, you must return an actual generated image, not a text description.",
        "For new image requests, generate at least one new image matching the description.",
        "If the user provides an image and asks for edits or variations, return a newly generated image with the requested changes.",
        "Avoid all text replies unless a short caption is explicitly requested. Do not explain, apologize, or describe image creation steps.",
        "Never send placeholder text like 'Here is your image' or any other response without an actual image attachment.",
    ]

    if primary:
        if primary.model:
            instructions.append(
                f"Where styles differ, favor the `{primary.model}` image model when rendering the scene."
            )
        if primary.output_format:
            instructions.append(
                f"Encode the image using the `{primary.output_format}` format whenever possible."
            )

    if has_forced_choice:
        instructions.append(
            "Image generation was explicitly requested. You must return at least one generated image. Any response without an image will be treated as a failure."
        )

    return "\n\n".join(instructions)


def _append_tool_hint_to_last_user_message(messages: list[Message]) -> None:
    """Ensure the last user message carries the tool wrap hint."""
    for msg in reversed(messages):
        if msg.role != "user" or msg.content is None:
            continue

        if isinstance(msg.content, str):
            if TOOL_HINT_STRIPPED not in msg.content:
                msg.content = f"{msg.content}\n{TOOL_WRAP_HINT}"
            return

        if isinstance(msg.content, list):
            for part in reversed(msg.content):
                if getattr(part, "type", None) != "text":
                    continue
                text_value = part.text or ""
                if TOOL_HINT_STRIPPED in text_value:
                    return
                part.text = f"{text_value}\n{TOOL_WRAP_HINT}"
                return

            messages_text = TOOL_WRAP_HINT.strip()
            msg.content.append(ContentItem(type="text", text=messages_text))
            return


def _prepare_messages_for_model(
    source_messages: list[Message],
    tools: list[Tool] | None,
    tool_choice: str | ToolChoiceFunction | None,
    extra_instructions: list[str] | None = None,
    inject_system_defaults: bool = True,
) -> list[Message]:
    """Return a copy of messages enriched with tool instructions when needed."""
    prepared = [msg.model_copy(deep=True) for msg in source_messages]

    # Resolve tool names for 'tool' messages by looking back at previous assistant tool calls
    tool_id_to_name = {}
    for msg in prepared:
        if msg.role == "assistant" and msg.tool_calls:
            for tc in msg.tool_calls:
                tool_id_to_name[tc.id] = tc.function.name

    for msg in prepared:
        if msg.role == "tool" and not msg.name and msg.tool_call_id:
            msg.name = tool_id_to_name.get(msg.tool_call_id)

    instructions: list[str] = []
    if inject_system_defaults:
        if tools:
            tool_prompt = _build_tool_prompt(tools, tool_choice)
            if tool_prompt:
                instructions.append(tool_prompt)

        if extra_instructions:
            instructions.extend(instr for instr in extra_instructions if instr)
            logger.debug(
                f"Applied {len(extra_instructions)} extra instructions for tool/structured output."
            )

    if not instructions:
        if tools and tool_choice != "none":
            _append_tool_hint_to_last_user_message(prepared)
        return prepared

    combined_instructions = "\n\n".join(instructions)
    if prepared and prepared[0].role == "system" and isinstance(prepared[0].content, str):
        existing = prepared[0].content or ""
        if combined_instructions not in existing:
            separator = "\n\n" if existing else ""
            prepared[0].content = f"{existing}{separator}{combined_instructions}"
    else:
        prepared.insert(0, Message(role="system", content=combined_instructions))

    if tools and tool_choice != "none":
        _append_tool_hint_to_last_user_message(prepared)

    return prepared


def _response_items_to_messages(
    items: str | list[ResponseInputItem],
) -> tuple[list[Message], str | list[ResponseInputItem]]:
    """Convert Responses API input items into internal Message objects and normalized input."""
    messages: list[Message] = []

    if isinstance(items, str):
        messages.append(Message(role="user", content=items))
        logger.debug("Normalized Responses input: single string message.")
        return messages, items

    normalized_input: list[ResponseInputItem] = []
    for item in items:
        role = item.role
        content = item.content
        normalized_contents: list[ResponseInputContent] = []
        if isinstance(content, str):
            normalized_contents.append(ResponseInputContent(type="input_text", text=content))
            messages.append(Message(role=role, content=content))
        else:
            converted: list[ContentItem] = []
            for part in content:
                if part.type == "input_text":
                    text_value = part.text or ""
                    normalized_contents.append(
                        ResponseInputContent(type="input_text", text=text_value)
                    )
                    if text_value:
                        converted.append(ContentItem(type="text", text=text_value))
                elif part.type == "input_image":
                    image_url = part.image_url
                    if image_url:
                        normalized_contents.append(
                            ResponseInputContent(
                                type="input_image",
                                image_url=image_url,
                                detail=part.detail if part.detail else "auto",
                            )
                        )
                        converted.append(
                            ContentItem(
                                type="image_url",
                                image_url={
                                    "url": image_url,
                                    "detail": part.detail if part.detail else "auto",
                                },
                            )
                        )
                elif part.type == "input_file":
                    if part.file_url or part.file_data:
                        normalized_contents.append(part)
                        file_info = {}
                        if part.file_data:
                            file_info["file_data"] = part.file_data
                            file_info["filename"] = part.filename
                        if part.file_url:
                            file_info["url"] = part.file_url
                        converted.append(ContentItem(type="file", file=file_info))
            messages.append(Message(role=role, content=converted or None))

        normalized_input.append(
            ResponseInputItem(type="message", role=item.role, content=normalized_contents or [])
        )

    logger.debug(f"Normalized Responses input: {len(normalized_input)} message items.")
    return messages, normalized_input


def _instructions_to_messages(
    instructions: str | list[ResponseInputItem] | None,
) -> list[Message]:
    """Normalize instructions payload into Message objects."""
    if not instructions:
        return []

    if isinstance(instructions, str):
        return [Message(role="system", content=instructions)]

    instruction_messages: list[Message] = []
    for item in instructions:
        if item.type and item.type != "message":
            continue

        role = item.role
        content = item.content
        if isinstance(content, str):
            instruction_messages.append(Message(role=role, content=content))
        else:
            converted: list[ContentItem] = []
            for part in content:
                if part.type == "input_text":
                    text_value = part.text or ""
                    if text_value:
                        converted.append(ContentItem(type="text", text=text_value))
                elif part.type == "input_image":
                    image_url = part.image_url
                    if image_url:
                        converted.append(
                            ContentItem(
                                type="image_url",
                                image_url={
                                    "url": image_url,
                                    "detail": part.detail if part.detail else "auto",
                                },
                            )
                        )
                elif part.type == "input_file":
                    file_info = {}
                    if part.file_data:
                        file_info["file_data"] = part.file_data
                        file_info["filename"] = part.filename
                    if part.file_url:
                        file_info["url"] = part.file_url
                    if file_info:
                        converted.append(ContentItem(type="file", file=file_info))
            instruction_messages.append(Message(role=role, content=converted or None))

    return instruction_messages


def _get_model_by_name(name: str) -> Model:
    """Retrieve a Model instance by name."""
    strategy = g_config.gemini.model_strategy
    custom_models = {m.model_name: m for m in g_config.gemini.models if m.model_name}

    if name in custom_models:
        return Model.from_dict(custom_models[name].model_dump())

    if strategy == "overwrite":
        raise ValueError(f"Model '{name}' not found in custom models (strategy='overwrite').")

    return Model.from_name(name)


def _get_available_models() -> list[ModelData]:
    """Return a list of available models based on configuration strategy."""
    now = int(datetime.now(tz=timezone.utc).timestamp())
    strategy = g_config.gemini.model_strategy
    models_data = []

    custom_models = [m for m in g_config.gemini.models if m.model_name]
    for m in custom_models:
        models_data.append(
            ModelData(
                id=m.model_name,
                created=now,
                owned_by="custom",
            )
        )

    if strategy == "append":
        custom_ids = {m.model_name for m in custom_models}
        for model in Model:
            m_name = model.model_name
            if not m_name or m_name == "unspecified":
                continue
            if m_name in custom_ids:
                continue

            models_data.append(
                ModelData(
                    id=m_name,
                    created=now,
                    owned_by="gemini-web",
                )
            )

    return models_data


async def _find_reusable_session(
    db: LMDBConversationStore,
    pool: GeminiClientPool,
    model: Model,
    messages: list[Message],
) -> tuple[ChatSession | None, GeminiClientWrapper | None, list[Message]]:
    """Find an existing chat session matching the longest suitable history prefix."""
    if len(messages) < 2:
        return None, None, messages

    search_end = len(messages)
    while search_end >= 2:
        search_history = messages[:search_end]
        # Note: 'tool' role is excluded from session matching because tool messages
        # are new inputs that should trigger generation, not part of existing history.
        # When a tool result is sent, it needs to be processed as a new message.
        if search_history[-1].role in {"assistant", "system"}:
            try:
                if conv := db.find(model.model_name, search_history):
                    now = datetime.now()
                    updated_at = conv.updated_at or conv.created_at or now
                    age_minutes = (now - updated_at).total_seconds() / 60
                    if age_minutes <= METADATA_TTL_MINUTES:
                        client = await pool.acquire(conv.client_id)
                        session = client.start_chat(metadata=conv.metadata, model=model)
                        remain = messages[search_end:]
                        logger.debug(
                            f"Match found at prefix length {search_end}/{len(messages)}. Client: {conv.client_id}"
                        )
                        return session, client, remain
                    else:
                        logger.debug(
                            f"Matched conversation at length {search_end} is too old ({age_minutes:.1f}m), skipping reuse."
                        )
                else:
                    # Log that we tried this prefix but failed
                    pass
            except Exception as e:
                logger.warning(
                    f"Error checking LMDB for reusable session at length {search_end}: {e}"
                )
                break
        search_end -= 1

    logger.debug(f"No reusable session found for {len(messages)} messages.")
    return None, None, messages


async def _send_with_split(
    session: ChatSession,
    text: str,
    files: list[Path | str | io.BytesIO] | None = None,
    stream: bool = False,
) -> AsyncGenerator[ModelOutput, None] | ModelOutput:
    """
    Send text to Gemini, splitting or converting to attachment if too long.
    Includes retry with exponential backoff for transient failures.
    """
    
    async def _stream_with_retry(
        content: str, file_list: list | None
    ) -> AsyncGenerator[ModelOutput, None]:
        """Manual retry logic for streaming."""
        max_retries = 1 # 再次减少到 1 次,确保总时长可控
        for attempt in range(max_retries + 1):
            try:
                gen = session.send_message_stream(content, files=file_list)
                has_yielded = False
                async for chunk in gen:
                    yield chunk
                    has_yielded = True
                return
            except Exception as e:
                if has_yielded:
                    logger.error(f"Stream interrupted after yielding data: {e}")
                    raise e
                
                error_str = str(e).lower()
                if any(code in error_str for code in ["429", "403", "401", "quota"]):
                    raise e

                if attempt < max_retries:
                    # 如果是流中断,给予更长的冷却时间让上游恢复
                    if "interrupted" in error_str or "truncated" in error_str:
                        delay = 5.0 + random.uniform(1.0, 3.0)
                        logger.warning(f"Stream interrupted. Cooling down for {delay:.2f}s before retry...")
                    else:
                        delay = 2.0 + random.uniform(0.1, 0.5)
                        
                    logger.warning(f"Stream failed to start (attempt {attempt+1}/{max_retries}). Retrying in {delay:.2f}s. Error: {e}")
                    await asyncio.sleep(delay)
                else:
                    raise e

    @retry_with_backoff(
        max_retries=1, # 减少重试次数,避免触发 Cloudflare 524 超时
        base_delay=2.0,
        max_delay=10.0,
        exponential_base=2.0,
        retryable_exceptions=(ConnectionError, TimeoutError, OSError, Exception),
    )
    async def _send_with_retry(
        content: str, file_list: list | None, is_stream: bool
    ) -> AsyncGenerator[ModelOutput, None] | ModelOutput:
        """Internal function with retry logic."""
        try:
            if is_stream:
                return _stream_with_retry(content, file_list)
            return await session.send_message(content, files=file_list)
        except Exception as e:
            error_msg = str(e)
            # 如果是流中断,记录警告并抛出以触发重试
            if "Stream interrupted" in error_msg or "truncated" in error_msg:
                logger.warning(f"Gemini stream interrupted (Session: {session.sid}): {e}")
                raise e
            raise e
    
    if len(text) <= MAX_CHARS_PER_REQUEST:
        try:
            return await _send_with_retry(text, files, stream)
        except Exception as e:
            logger.exception(f"Error sending message to Gemini after retries: {e}")
            raise

    logger.info(
        f"Message length ({len(text)}) exceeds limit ({MAX_CHARS_PER_REQUEST}). Converting text to file attachment."
    )
    file_obj = io.BytesIO(text.encode("utf-8"))
    file_obj.name = "message.txt"
    try:
        final_files = list(files) if files else []
        final_files.append(file_obj)
        instruction = (
            "The user's input exceeds the character limit and is provided in the attached file `message.txt`.\n\n"
            "**System Instruction:**\n"
            "1. Read the content of `message.txt`.\n"
            "2. Treat that content as the **primary** user prompt for this turn.\n"
            "3. Execute the instructions or answer the questions found *inside* that file immediately.\n"
        )
        return await _send_with_retry(instruction, final_files, stream)
    except Exception as e:
        logger.exception(f"Error sending large text as file to Gemini after retries: {e}")
        raise


class StreamingOutputFilter:
    """
    State Machine filter to suppress technical markers, tool calls, and system hints.
    Handles fragmentation where markers are split across multiple chunks.
    Returns structured events: {'type': 'text'|'tool_start'|'tool_delta'|'tool_end', 'content': ...}
    """

    # Internal technical artifacts to suppress in real-time
    GARBAGE_URL_RE = re.compile(
        r"https?://(?:[a-zA-Z0-9-]+\.)*(?:googleusercontent\.com|gstatic\.com|google\.com)/(?:image_collection|recs|image_retrieval)/[^\s]+"
    )
    # Citation markers (e.g., [1] or 【1†source】)
    CITATION_RE = re.compile(r"【\d+†source】|\[\d+\]")
    # Match patterns for JSON-like content
    JSON_START_RE = re.compile(r"\s*\{")
    # Match whitespace for faster stripping
    LEADING_WS_RE = re.compile(r"^\s+")
    # Pre-compiled patterns for streaming performance
    TRAILING_FENCE_RE = re.compile(r'\n?```$')
    URL_DELIMITER_RE = re.compile(r"[\s\]\)]")
    BRACKET_NUM_RE = re.compile(r"\[\d+\]")

    def __init__(self):
        self.buffer = ""
        self.state = "NORMAL"
        self.current_role = ""

        self.TOOL_START = "[function_calls]"
        self.TOOL_END_PREFIX = "[/function_"
        self.CALL_START = "[call:"
        self.CALL_TITLE_END = "]"
        self.CALL_END = "[/call]"
        self.TAG_START = "<|im_start|>"
        self.TAG_END = "<|im_end|>"
        self.HINT_START = f"\n{TOOL_HINT_LINE_START}" if TOOL_HINT_LINE_START else ""
        self.HINT_END = TOOL_HINT_LINE_END

        self.WATCH_PREFIXES = [
            self.TOOL_START,
            self.TAG_START,
            self.TAG_END,
            self.TOOL_END_PREFIX,
            "http://googleusercontent.com/",
            "https://googleusercontent.com/",
            "http://www.googleusercontent.com/",
            "https://www.googleusercontent.com/",
            "http://",
            "https://",
            "[",  # For citations
            "【", # For source citations
        ]
        if self.HINT_START:
            self.WATCH_PREFIXES.append(self.HINT_START)
        
        self.tool_call_index = 0
        self.current_call_id = None
        self.args_started = False

    def process(self, chunk: str) -> list[dict[str, Any]]:
        self.buffer += chunk
        events = []

        while self.buffer:
            if self.state == "NORMAL":
                low_buf = self.buffer.lower()
                tool_idx = low_buf.find(self.TOOL_START.lower())
                tag_idx = low_buf.find(self.TAG_START.lower())
                end_idx = low_buf.find(self.TAG_END.lower())
                hint_idx = low_buf.find(self.HINT_START.lower()) if self.HINT_START else -1
                
                url_idx = -1
                http_idx = low_buf.find("http://")
                https_idx = low_buf.find("https://")
                
                if http_idx != -1 and https_idx != -1:
                    url_idx = min(http_idx, https_idx)
                elif http_idx != -1:
                    url_idx = http_idx
                elif https_idx != -1:
                    url_idx = https_idx

                # Check for brackets (potential citations or tool calls)
                bracket_idx = low_buf.find("[")
                source_idx = low_buf.find("【")

                indices = [(i, t) for i, t in [
                    (tool_idx, "TOOL"), (tag_idx, "TAG"), (end_idx, "END"), 
                    (hint_idx, "HINT"), (url_idx, "URL_START"),
                    (bracket_idx, "BRACKET"), (source_idx, "SOURCE")
                ] if i != -1]

                if not indices:
                    keep_len = 0
                    for p in self.WATCH_PREFIXES:
                        p_low = p.lower()
                        for i in range(len(p) - 1, 0, -1):
                            if low_buf.endswith(p_low[:i]):
                                keep_len = max(keep_len, i)
                                break
                    yield_len = len(self.buffer) - keep_len
                    if yield_len > 0:
                        content = self.buffer[:yield_len]
                        content = self.GARBAGE_URL_RE.sub("", content)
                        content = self.CITATION_RE.sub("", content)
                        if content:
                            events.append({"type": "text", "content": content})
                        self.buffer = self.buffer[yield_len:]
                    break

                indices.sort()
                m_idx, m_type = indices[0]
                
                if m_idx > 0:
                    content = self.buffer[:m_idx]
                    content = self.GARBAGE_URL_RE.sub("", content)
                    content = self.CITATION_RE.sub("", content)
                    if content:
                        events.append({"type": "text", "content": content})
                self.buffer = self.buffer[m_idx:]

                if m_type == "TOOL":
                    self.state = "IN_TOOL_BLOCK"
                    self.buffer = self.buffer[len(self.TOOL_START):]
                elif m_type == "TAG":
                    self.state = "IN_TAG"
                    self.buffer = self.buffer[len(self.TAG_START):]
                elif m_type == "END":
                    self.buffer = self.buffer[len(self.TAG_END):]
                elif m_type == "HINT":
                    self.state = "IN_HINT"
                    self.buffer = self.buffer[len(self.HINT_START):]
                elif m_type == "URL_START":
                    self.state = "IN_POTENTIAL_URL"
                elif m_type == "BRACKET":
                    self.state = "IN_BRACKETED"
                elif m_type == "SOURCE":
                    self.state = "IN_POTENTIAL_SOURCE"

            elif self.state == "IN_POTENTIAL_URL":
                match = self.URL_DELIMITER_RE.search(self.buffer)
                if match:
                    end_idx = match.start()
                    url_candidate = self.buffer[:end_idx]
                    delimiter = self.buffer[end_idx]
                    
                    if self.GARBAGE_URL_RE.match(url_candidate):
                         self.buffer = self.buffer[end_idx:]
                         self.state = "NORMAL"
                         continue

                    remaining = self.buffer[end_idx:]
                    if len(remaining) >= 2:
                        if remaining.startswith("]("):
                            events.append({"type": "text", "content": f"[{url_candidate}]("})
                            self.buffer = remaining[2:]
                            self.state = "NORMAL"
                            continue
                        else:
                            events.append({"type": "text", "content": url_candidate})
                            self.buffer = self.buffer[end_idx:]
                            self.state = "NORMAL"
                            continue
                    else:
                        break
                else:
                    if len(self.buffer) > 2000:
                         events.append({"type": "text", "content": self.buffer})
                         self.buffer = ""
                         self.state = "NORMAL"
                    break

            elif self.state == "IN_BRACKETED":
                # Handles citations [1] or tool calls [call:...]
                end_idx = self.buffer.find("]")
                if end_idx != -1:
                    full_match = self.buffer[:end_idx + 1]
                    if self.BRACKET_NUM_RE.match(full_match):
                        # It's a citation, skip it
                        self.buffer = self.buffer[end_idx + 1:]
                        self.state = "NORMAL"
                    else:
                        # Pass back to NORMAL (first character) to be handled as text or other markers
                        events.append({"type": "text", "content": "["})
                        self.buffer = self.buffer[1:]
                        self.state = "NORMAL"
                else:
                    if len(self.buffer) > 100:
                         events.append({"type": "text", "content": "["})
                         self.buffer = self.buffer[1:]
                         self.state = "NORMAL"
                    break

            elif self.state == "IN_POTENTIAL_SOURCE":
                end_idx = self.buffer.find("】")
                if end_idx != -1:
                    full_match = self.buffer[:end_idx + 1]
                    if "source" in full_match.lower():
                        # Discard citation
                        self.buffer = self.buffer[end_idx + 1:]
                        self.state = "NORMAL"
                    else:
                        events.append({"type": "text", "content": "【"})
                        self.buffer = self.buffer[1:]
                        self.state = "NORMAL"
                else:
                    if len(self.buffer) > 100:
                         events.append({"type": "text", "content": "【"})
                         self.buffer = self.buffer[1:]
                         self.state = "NORMAL"
                    break

            elif self.state == "IN_HINT":
                low_buf = self.buffer.lower()
                end_idx = low_buf.find(self.HINT_END.lower())
                if end_idx != -1:
                    self.buffer = self.buffer[end_idx + len(self.HINT_END):]
                    self.state = "NORMAL"
                else:
                    keep_len = len(self.HINT_END) - 1
                    if len(self.buffer) > keep_len:
                        self.buffer = self.buffer[-keep_len:]
                    break

            elif self.state == "IN_TOOL_BLOCK":
                low_buf = self.buffer.lower()
                call_idx = low_buf.find(self.CALL_START.lower())
                block_end_idx = low_buf.find(self.TOOL_END_PREFIX.lower())
                
                m_indices = [(i, t) for i, t in [(call_idx, "CALL"), (block_end_idx, "BLOCK_END")] if i != -1]
                if not m_indices:
                    pass 
                else:
                    m_indices.sort()
                    idx, m_type = m_indices[0]
                    self.buffer = self.buffer[idx:]
                    
                    if m_type == "CALL":
                        self.state = "IN_CALL_TITLE"
                        self.buffer = self.buffer[len(self.CALL_START):]
                        continue
                    elif m_type == "BLOCK_END":
                        self.state = "NORMAL"
                        bracket_idx = self.buffer.find("]", len(self.TOOL_END_PREFIX))
                        if bracket_idx != -1 and bracket_idx < 50:
                            self.buffer = self.buffer[bracket_idx + 1:]
                        else:
                            self.buffer = self.buffer[len(self.TOOL_END_PREFIX):]
                        continue

                max_marker_len = max(len(self.CALL_START), len(self.TOOL_END_PREFIX))
                if len(self.buffer) > max_marker_len:
                    self.buffer = self.buffer[-(max_marker_len - 1):] if max_marker_len > 1 else ""
                break

            elif self.state == "IN_CALL_TITLE":
                low_buf = self.buffer.lower()
                title_end_idx = low_buf.find(self.CALL_TITLE_END.lower())
                if title_end_idx != -1:
                    tool_name = self.buffer[:title_end_idx].strip()
                    self.current_call_id = f"call_{uuid.uuid4().hex[:24]}"
                    events.append({
                        "type": "tool_start", 
                        "index": self.tool_call_index, 
                        "id": self.current_call_id,
                        "name": tool_name
                    })
                    self.buffer = self.buffer[title_end_idx + len(self.CALL_TITLE_END):]
                    self.state = "IN_CALL_ARGS"
                    self.args_started = False
                else:
                    break

            elif self.state == "IN_CALL_ARGS":
                low_buf = self.buffer.lower()
                call_end_idx = low_buf.find(self.CALL_END.lower())
                block_end_idx = low_buf.find(self.TOOL_END_PREFIX.lower())
                
                stop_idx = -1
                m_type = ""
                if call_end_idx != -1 and block_end_idx != -1:
                    if call_end_idx < block_end_idx:
                        stop_idx = call_end_idx
                        m_type = "CALL_END"
                    else:
                        stop_idx = block_end_idx
                        m_type = "BLOCK_END"
                elif call_end_idx != -1:
                    stop_idx = call_end_idx
                    m_type = "CALL_END"
                elif block_end_idx != -1:
                    stop_idx = block_end_idx
                    m_type = "BLOCK_END"

                if stop_idx != -1:
                    args_part = self.buffer[:stop_idx]
                    if not self.args_started:
                        stripped_args = args_part.lstrip()
                        # More robust handling: accept any non-empty content
                        # Even if it doesn't start with '{', we still emit it
                        # The JSON parsing will handle errors downstream
                        if stripped_args:
                            self.args_started = True
                            # If doesn't start with '{', try to fix common issues
                            if not stripped_args.startswith("{"):
                                # Check if it might be a valid JSON that's missing the opening brace
                                # or if it's just plain text arguments
                                args_part = "{" + args_part.lstrip()

                    if args_part:
                        events.append({"type": "tool_delta", "index": self.tool_call_index, "content": args_part})
                    events.append({"type": "tool_end", "index": self.tool_call_index})
                    self.tool_call_index += 1
                    self.current_call_id = None
                    self.args_started = False 
                    
                    if m_type == "CALL_END":
                        self.buffer = self.buffer[stop_idx + len(self.CALL_END):]
                        self.state = "IN_TOOL_BLOCK"
                    else:
                        self.buffer = self.buffer[stop_idx:]
                        self.state = "IN_TOOL_BLOCK"
                else:
                    if not self.args_started:
                        stripped = self.buffer.lstrip()
                        if stripped:
                            if stripped.startswith("{"):
                                self.args_started = True
                            else:
                                # Be more tolerant: accept arguments even without opening brace
                                # Common issue: LLM outputs `key: value` instead of `{"key": "value"}`
                                self.args_started = True
                    
                    if self.args_started:
                        keep_len = max(len(self.CALL_END), len(self.TOOL_END_PREFIX)) - 1
                        if len(self.buffer) > keep_len:
                            chunk_to_send = self.buffer[:-keep_len]
                            if chunk_to_send:
                                events.append({"type": "tool_delta", "index": self.tool_call_index, "content": chunk_to_send})
                            self.buffer = self.buffer[-keep_len:]
                    else:
                        # Buffer has content but we haven't found JSON start yet
                        # Wait a bit more for potential JSON start
                        if len(self.buffer) > 100:
                            # Timeout waiting for JSON, emit what we have
                            events.append({"type": "tool_end", "index": self.tool_call_index})
                            self.tool_call_index += 1
                            self.current_call_id = None
                            self.args_started = False
                            self.state = "IN_TOOL_BLOCK"
                    break

            elif self.state == "IN_TAG":
                nl_idx = self.buffer.find("\n")
                if nl_idx != -1:
                    self.current_role = self.buffer[:nl_idx].strip().lower()
                    self.buffer = self.buffer[nl_idx + 1:]
                    self.state = "IN_BLOCK"
                else:
                    break

            elif self.state == "IN_BLOCK":
                end_idx = self.buffer.find(self.TAG_END)
                if end_idx != -1:
                    content = self.buffer[:end_idx]
                    if self.current_role != "tool":
                        events.append({"type": "text", "content": content})
                    self.buffer = self.buffer[end_idx + len(self.TAG_END):]
                    self.state = "NORMAL"
                    self.current_role = ""
                else:
                    keep_len = len(self.TAG_END) - 1
                    if self.current_role != "tool":
                        if len(self.buffer) > keep_len:
                            events.append({"type": "text", "content": self.buffer[:-keep_len]})
                            self.buffer = self.buffer[-keep_len:]
                        break
                    else:
                        if len(self.buffer) > keep_len:
                            self.buffer = self.buffer[-keep_len:]
                        break
        return events

    def flush(self) -> list[dict[str, Any]]:
        events = []
        if self.buffer:
            if self.state == "IN_CALL_ARGS":
                events.append({"type": "tool_delta", "index": self.tool_call_index, "content": self.buffer})
                events.append({"type": "tool_end", "index": self.tool_call_index})
                self.tool_call_index += 1
            elif self.state in ("NORMAL", "IN_POTENTIAL_URL", "IN_BRACKETED", "IN_POTENTIAL_SOURCE"):
                content = self.GARBAGE_URL_RE.sub("", self.buffer)
                content = self.CITATION_RE.sub("", content)
                if content:
                    events.append({"type": "text", "content": content})
            elif self.state == "IN_BLOCK" and self.current_role != "tool":
                events.append({"type": "text", "content": self.buffer})
            
        self.buffer = ""
        self.state = "NORMAL"
        self.current_call_id = None
        self.args_started = False
        return events


# --- Response Builders & Streaming ---


def _create_real_streaming_response(
    generator: AsyncGenerator[ModelOutput, None],
    completion_id: str,
    created_time: int,
    model_name: str,
    messages: list[Message],
    db: LMDBConversationStore,
    model: Model,
    client_wrapper: GeminiClientWrapper,
    session: ChatSession,
    base_url: str,
    structured_requirement: StructuredOutputRequirement | None = None,
) -> StreamingResponse:
    """
    Create a real-time streaming response.
    Reconciles manual delta accumulation with the model's final authoritative state.
    """

    async def generate_stream():
        full_thoughts, full_text = "", ""
        has_started = False
        last_chunk_was_thought = False
        all_outputs: list[ModelOutput] = []
        suppressor = StreamingOutputFilter()
        try:
            async for chunk in generator:
                all_outputs.append(chunk)
                if not has_started:
                    data = {
                        "id": completion_id,
                        "object": "chat.completion.chunk",
                        "created": created_time,
                        "model": model_name,
                        "choices": [
                            {"index": 0, "delta": {"role": "assistant"}, "finish_reason": None}
                        ],
                    }
                    yield f"data: {orjson.dumps(data).decode('utf-8')}\n\n"
                    has_started = True

                # Robust thought delta calculation
                new_thoughts = chunk.thoughts or ""
                if len(new_thoughts) > len(full_thoughts):
                    t_delta = new_thoughts[len(full_thoughts) :]
                    if not last_chunk_was_thought and not full_thoughts:
                        yield f"data: {orjson.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'created': created_time, 'model': model_name, 'choices': [{'index': 0, 'delta': {'content': '<think>'}, 'finish_reason': None}]}).decode('utf-8')}\n\n"
                    
                    data = {
                        "id": completion_id,
                        "object": "chat.completion.chunk",
                        "created": created_time,
                        "model": model_name,
                        "choices": [
                            {"index": 0, "delta": {"content": t_delta}, "finish_reason": None}
                        ],
                    }
                    yield f"data: {orjson.dumps(data).decode('utf-8')}\n\n"
                    full_thoughts = new_thoughts
                    last_chunk_was_thought = True

                # Robust text delta calculation
                new_text = chunk.text or ""
                # Strip transient triple-backtick fence which is often added by the web-scraping backend
                # during streaming snapshots. We will restore the real ending after the loop.
                display_text = StreamingOutputFilter.TRAILING_FENCE_RE.sub('', new_text)
                
                if len(display_text) > len(full_text):
                    text_delta = display_text[len(full_text) :]
                    if last_chunk_was_thought:
                        yield f"data: {orjson.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'created': created_time, 'model': model_name, 'choices': [{'index': 0, 'delta': {'content': '</think>\n'}, 'finish_reason': None}]}).decode('utf-8')}\n\n"
                        last_chunk_was_thought = False
                    
                    filter_events = suppressor.process(text_delta)
                    for event in filter_events:
                        delta = {}
                        if event['type'] == 'text':
                            delta = {"content": event['content']}
                        elif event['type'] == 'tool_start':
                            delta = {"tool_calls": [{"index": event['index'], "id": event['id'], "type": "function", "function": {"name": event['name'], "arguments": ""}}]}
                        elif event['type'] == 'tool_delta':
                            delta = {"tool_calls": [{"index": event['index'], "function": {"arguments": event['content']}}]}
                        
                        if delta:
                            data = {
                                "id": completion_id,
                                "object": "chat.completion.chunk",
                                "created": created_time,
                                "model": model_name,
                                "choices": [
                                    {
                                        "index": 0,
                                        "delta": delta,
                                        "finish_reason": None,
                                    }
                                ],
                            }
                            yield f"data: {orjson.dumps(data).decode('utf-8')}\n\n"
                    full_text = display_text
        except Exception as e:
            logger.exception(f"Error during OpenAI streaming: {e}")
            yield f"data: {orjson.dumps({'error': {'message': 'Streaming error occurred.', 'type': 'server_error', 'param': None, 'code': None}}).decode('utf-8')}\n\n"
            return

        if all_outputs:
            final_chunk = all_outputs[-1]
            final_text = final_chunk.text or ""
            if len(final_text) > len(full_text):
                # Yield any real trailing content (like the actual closing fence)
                final_delta = final_text[len(full_text) :]
                for event in suppressor.process(final_delta):
                    if event['type'] == 'text':
                        yield f"data: {orjson.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'created': created_time, 'model': model_name, 'choices': [{'index': 0, 'delta': {'content': event['content']}, 'finish_reason': None}]}).decode('utf-8')}\n\n"
                full_text = final_text
            if final_chunk.thoughts:
                full_thoughts = final_chunk.thoughts

        if last_chunk_was_thought:
            yield f"data: {orjson.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'created': created_time, 'model': model_name, 'choices': [{'index': 0, 'delta': {'content': '</think>\n'}, 'finish_reason': None}]}).decode('utf-8')}\n\n"

        for event in suppressor.flush():
            delta = {}
            if event['type'] == 'text':
                delta = {"content": event['content']}
            elif event['type'] == 'tool_start':
                delta = {"tool_calls": [{"index": event['index'], "id": event['id'], "type": "function", "function": {"name": event['name'], "arguments": ""}}]}
            elif event['type'] == 'tool_delta':
                delta = {"tool_calls": [{"index": event['index'], "function": {"arguments": event['content']}}]}
            
            if delta:
                data = {
                    "id": completion_id,
                    "object": "chat.completion.chunk",
                    "created": created_time,
                    "model": model_name,
                    "choices": [
                        {"index": 0, "delta": delta, "finish_reason": None}
                    ],
                }
                yield f"data: {orjson.dumps(data).decode('utf-8')}\n\n"

        raw_output_with_think = f"<think>{full_thoughts}</think>\n" if full_thoughts else ""
        raw_output_with_think += full_text
        assistant_text, storage_output, tool_calls = _process_llm_output(
            raw_output_with_think, full_text, structured_requirement
        )

        images = []
        seen_urls = set()
        for out in all_outputs:
            if out.images:
                for img in out.images:
                    # Use the image URL as a stable identifier across chunks
                    if img.url not in seen_urls:
                        images.append(img)
                        seen_urls.add(img.url)

        image_markdown = ""
        seen_hashes = set()
        for image in images:
            try:
                image_store = get_image_store_dir()
                _, _, _, filename, file_hash = await _image_to_base64(image, image_store)
                if file_hash in seen_hashes:
                    # Duplicate content, delete the file and skip
                    (image_store / filename).unlink(missing_ok=True)
                    continue
                seen_hashes.add(file_hash)

                img_url = (
                    f"![{filename}]({base_url}images/{filename}?token={get_image_token(filename)})"
                )
                image_markdown += f"\n\n{img_url}"
            except Exception as exc:
                logger.warning(f"Failed to process image in OpenAI stream: {exc}")

        if image_markdown:
            assistant_text += image_markdown
            storage_output += image_markdown
            yield f"data: {orjson.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'created': created_time, 'model': model_name, 'choices': [{'index': 0, 'delta': {'content': image_markdown}, 'finish_reason': None}]}).decode('utf-8')}\n\n"

        p_tok, c_tok, t_tok = _calculate_usage(messages, assistant_text, tool_calls)
        usage = {"prompt_tokens": p_tok, "completion_tokens": c_tok, "total_tokens": t_tok}
        
        final_delta = {}
        if tool_calls:
            # Add index field to each tool_call for OpenAI compatibility
            final_delta["tool_calls"] = [
                {**call.model_dump(mode="json"), "index": i} 
                for i, call in enumerate(tool_calls)
            ]
            
        data = {
            "id": completion_id,
            "object": "chat.completion.chunk",
            "created": created_time,
            "model": model_name,
            "choices": [
                {"index": 0, "delta": final_delta, "finish_reason": "tool_calls" if tool_calls else "stop"}
            ],
            "usage": usage,
        }
        _persist_conversation(
            db,
            model.model_name,
            client_wrapper.id,
            session.metadata,
            messages,  # This should be the prepared messages
            storage_output,
            tool_calls,
        )
        yield f"data: {orjson.dumps(data).decode('utf-8')}\n\n"
        yield "data: [DONE]\n\n"

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


def _create_responses_real_streaming_response(
    generator: AsyncGenerator[ModelOutput, None],
    response_id: str,
    created_time: int,
    model_name: str,
    messages: list[Message],
    db: LMDBConversationStore,
    model: Model,
    client_wrapper: GeminiClientWrapper,
    session: ChatSession,
    request: ResponseCreateRequest,
    image_store: Path,
    base_url: str,
    structured_requirement: StructuredOutputRequirement | None = None,
) -> StreamingResponse:
    """
    Create a real-time streaming response for the Responses API.
    Ensures final accumulated text and thoughts are synchronized.
    """
    base_event = {
        "id": response_id,
        "object": "response",
        "created_at": created_time,
        "model": model_name,
    }

    async def generate_stream():
        yield f"data: {orjson.dumps({**base_event, 'type': 'response.created', 'response': {'id': response_id, 'object': 'response', 'created_at': created_time, 'model': model_name, 'status': 'in_progress', 'metadata': request.metadata, 'input': None, 'tools': request.tools, 'tool_choice': request.tool_choice}}).decode('utf-8')}\n\n"
        message_id = f"msg_{uuid.uuid4().hex}"
        yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_item.added', 'output_index': 0, 'item': {'id': message_id, 'type': 'message', 'role': 'assistant', 'content': []}}).decode('utf-8')}\n\n"

        full_thoughts, full_text = "", ""
        last_chunk_was_thought = False
        all_outputs: list[ModelOutput] = []
        suppressor = StreamingOutputFilter()

        try:
            async for chunk in generator:
                all_outputs.append(chunk)

                # Robust thought delta calculation
                new_thoughts = chunk.thoughts or ""
                if len(new_thoughts) > len(full_thoughts):
                    t_delta = new_thoughts[len(full_thoughts) :]
                    if not last_chunk_was_thought and not full_thoughts:
                        yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': '<think>'}).decode('utf-8')}\n\n"
                    yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': t_delta}).decode('utf-8')}\n\n"
                    full_thoughts = new_thoughts
                    last_chunk_was_thought = True

                # Robust text delta calculation
                new_text = chunk.text or ""
                # Strip transient triple-backtick fence which is often added by the web-scraping backend
                # during streaming snapshots. We will restore the real ending after the loop.
                display_text = StreamingOutputFilter.TRAILING_FENCE_RE.sub('', new_text)

                if len(display_text) > len(full_text):
                    text_delta = display_text[len(full_text) :]
                    if last_chunk_was_thought:
                        yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': '</think>\n'}).decode('utf-8')}\n\n"
                        last_chunk_was_thought = False
                    
                    filter_events = suppressor.process(text_delta)
                    for event in filter_events:
                        if event['type'] == 'text':
                            yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': event['content']}).decode('utf-8')}\n\n"
                        # Note: This API currently handles tool calls as discrete items at the end.
                        # We ignore internal tool call events in the text stream for now.
                    full_text = display_text
        except Exception as e:
            logger.exception(f"Error during Responses API streaming: {e}")
            yield f"data: {orjson.dumps({**base_event, 'type': 'error', 'error': {'message': 'Streaming error.'}}).decode('utf-8')}\n\n"
            return

        if all_outputs:
            final_chunk = all_outputs[-1]
            final_text = final_chunk.text or ""
            if len(final_text) > len(full_text):
                # Yield any real trailing content (like the actual closing fence)
                final_delta = final_text[len(full_text) :]
                for event in suppressor.process(final_delta):
                    if event['type'] == 'text':
                        yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': event['content']}).decode('utf-8')}\n\n"
                full_text = final_text
            if final_chunk.thoughts:
                full_thoughts = final_chunk.thoughts

        if last_chunk_was_thought:
            yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': '</think>\n'}).decode('utf-8')}\n\n"
        for event in suppressor.flush():
            if event['type'] == 'text':
                yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': event['content']}).decode('utf-8')}\n\n"
        yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.done', 'output_index': 0}).decode('utf-8')}\n\n"

        raw_output_with_think = f"<think>{full_thoughts}</think>\n" if full_thoughts else ""
        raw_output_with_think += full_text
        assistant_text, storage_output, detected_tool_calls = _process_llm_output(
            raw_output_with_think, full_text, structured_requirement
        )

        images = []
        seen_urls = set()
        for out in all_outputs:
            if out.images:
                for img in out.images:
                    if img.url not in seen_urls:
                        images.append(img)
                        seen_urls.add(img.url)
        
        # Check if image generation was forced via tool_choice (same logic as non-streaming)
        image_generation_forced = (
            request.tool_choice is not None 
            and isinstance(request.tool_choice, ResponseToolChoice) 
            and request.tool_choice.type == "image_generation"
        )
        if image_generation_forced and not images and not assistant_text:
            logger.warning("Image generation was forced via tool_choice but no images or text were returned in stream.")
            yield f"data: {orjson.dumps({**base_event, 'type': 'error', 'error': {'message': 'No images returned from forced image generation request.'}}).decode('utf-8')}\n\n"
            return

        response_contents, image_call_items = [], []
        seen_hashes = set()
        for image in images:
            try:
                image_base64, width, height, filename, file_hash = await _image_to_base64(
                    image, image_store
                )
                if file_hash in seen_hashes:
                    (image_store / filename).unlink(missing_ok=True)
                    continue
                seen_hashes.add(file_hash)

                img_format = "png" if isinstance(image, GeneratedImage) else "jpeg"
                image_url = (
                    f"![{filename}]({base_url}images/{filename}?token={get_image_token(filename)})"
                )
                image_call_items.append(
                    ResponseImageGenerationCall(
                        id=filename.rsplit(".", 1)[0],
                        result=image_base64,
                        output_format=img_format,
                        size=f"{width}x{height}" if width and height else None,
                    )
                )
                response_contents.append(ResponseOutputContent(type="output_text", text=image_url))
            except Exception as exc:
                logger.warning(f"Failed to process image in stream: {exc}")

        if assistant_text:
            response_contents.append(ResponseOutputContent(type="output_text", text=assistant_text))
        if not response_contents:
            response_contents.append(ResponseOutputContent(type="output_text", text=""))

        # Aggregate images for storage
        image_markdown = ""
        for img_call in image_call_items:
            fname = f"{img_call.id}.{img_call.output_format}"
            img_url = f"![{fname}]({base_url}images/{fname}?token={get_image_token(fname)})"
            image_markdown += f"\n\n{img_url}"

        if image_markdown:
            storage_output += image_markdown

        yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_item.done', 'output_index': 0, 'item': {'id': message_id, 'type': 'message', 'role': 'assistant', 'content': [c.model_dump(mode='json') for c in response_contents]}}).decode('utf-8')}\n\n"

        current_idx = 1
        for call in detected_tool_calls:
            tc_item = ResponseToolCall(id=call.id, status="completed", function=call.function)
            yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_item.added', 'output_index': current_idx, 'item': tc_item.model_dump(mode='json')}).decode('utf-8')}\n\n"
            yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_item.done', 'output_index': current_idx, 'item': tc_item.model_dump(mode='json')}).decode('utf-8')}\n\n"
            current_idx += 1
        for img_call in image_call_items:
            yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_item.added', 'output_index': current_idx, 'item': img_call.model_dump(mode='json')}).decode('utf-8')}\n\n"
            yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_item.done', 'output_index': current_idx, 'item': img_call.model_dump(mode='json')}).decode('utf-8')}\n\n"
            current_idx += 1

        p_tok, c_tok, t_tok = _calculate_usage(messages, assistant_text, detected_tool_calls)
        usage = ResponseUsage(input_tokens=p_tok, output_tokens=c_tok, total_tokens=t_tok)
        payload = _create_responses_standard_payload(
            response_id,
            created_time,
            model_name,
            detected_tool_calls,
            image_call_items,
            response_contents,
            usage,
            request,
            None,
        )
        _persist_conversation(
            db,
            model.model_name,
            client_wrapper.id,
            session.metadata,
            messages,
            storage_output,
            detected_tool_calls,
        )
        yield f"data: {orjson.dumps({**base_event, 'type': 'response.completed', 'response': payload.model_dump(mode='json')}).decode('utf-8')}\n\n"
        yield "data: [DONE]\n\n"

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



# --- Core Session Logic ---


async def _execute_core_chat_session(
    request_model: str,
    messages: list[Message],
    tmp_dir: Path,
    tools: list[Tool] | None = None,
    tool_choice: str | ToolChoiceFunction | None = None,
    stream: bool = False,
    extra_instructions: list[str] | None = None,
    response_format: dict[str, Any] | None = None,
):
    """
    Common logic or executing a chat session with Gemini.
    Forces model to 'gemini-3.0-pro' regardless of request_model.
    Returns: (resp_or_stream, completion_id, created_time, session, client, db, model_obj, prepared_messages, structured_req)
    """
    # FORCE MODEL OVERRIDE
    forced_model_name = "gemini-3.0-pro"
    
    pool, db = GeminiClientPool(), LMDBConversationStore()
    try:
        model = _get_model_by_name(forced_model_name)
    except ValueError as exc:
        # Fallback if gemini-3.0-pro isn't in config/models for some reason, though it should be.
        # But per requirements we MUST use it. 
        # If it fails here, it means the server isn't configured for it.
        # We try to proceed with what we have if 3.0 fails, OR we hard error.
        # Given "can only be gemini-3.0-pro", we should probably assume it exists or fail.
        # Let's try to get it, if not found, we might try to construct it or fail.
        # _get_model_by_name handles custom models and strategy.
        # If it fails, we raise.
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"Required model '{forced_model_name}' not available: {str(exc)}") from exc
        
    if not messages:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Messages required.")

    structured_requirement = _build_structured_requirement(response_format)
    # Merge extra instructions with structured requirement instruction
    final_extra_instr = list(extra_instructions) if extra_instructions else []
    if structured_requirement:
        final_extra_instr.append(structured_requirement.instruction)

    # This ensures that server-injected system instructions are part of the history
    msgs = _prepare_messages_for_model(
        messages,
        tools,
        tool_choice,
        final_extra_instr or None,
    )

    session, client, remain = await _find_reusable_session(db, pool, model, msgs)

    # Check if we should actually reuse the session
    if session and not remain:
        # Special case: if the last message is a tool result, we should process it
        # even if remain is empty (this happens when tool message is at the end)
        if messages[-1].role == "tool":
            # Create a remain list with just the tool message
            remain = [messages[-1]]
        elif messages[-1].role == "assistant":
            # If the last message is assistant and there are no new messages,
            # we should not reuse this session. Let the client create a new session
            # or handle this case differently.
            logger.debug("Session matched but no new messages after assistant turn - not reusing session.")
            session = None
            client = None
            remain = msgs
        else:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="No new messages.")

    if session:
        # For reused sessions, we only need to process the remaining messages.
        input_msgs = _prepare_messages_for_model(
            remain,
            tools,
            tool_choice,
            final_extra_instr or None,
            True, # Ensure tool hints/instructions are present in the turn
        )
        if len(input_msgs) == 1:
            m_input, files = await GeminiClientWrapper.process_message(
                input_msgs[0], tmp_dir, tagged=False
            )
        else:
            m_input, files = await GeminiClientWrapper.process_conversation(input_msgs, tmp_dir)

        logger.debug(
            f"Reused session {reprlib.repr(session.metadata)} - sending {len(input_msgs)} prepared messages."
        )
    else:
        try:
            client = await pool.acquire()
            session = client.start_chat(model=model)
            m_input, files = await GeminiClientWrapper.process_conversation(msgs, tmp_dir)
        except Exception as e:
            logger.exception("Error in preparing conversation")
            raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=str(e))

    completion_id = f"chatcmpl-{uuid.uuid4()}"
    created_time = int(datetime.now(tz=timezone.utc).timestamp())

    try:
        assert session and client
        logger.debug(
            f"Client ID: {client.id}, Input length: {len(m_input)}, files count: {len(files)}"
        )
        resp_or_stream = await _send_with_split(
            session, m_input, files=files, stream=stream
        )
    except Exception as e:
        logger.exception("Gemini API error")
        raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail=str(e))
        
    return resp_or_stream, completion_id, created_time, session, client, db, model, msgs, structured_requirement


async def _create_anthropic_streaming_response(
    generator: AsyncGenerator[ModelOutput, None],
    completion_id: str,
    created_time: int,
    model_name: str,
    messages: list[Message],
):
    """
    Stream Gemini output as Anthropic SSE events.
    Now supports structured tool call streaming.
    """
    async def generate_stream():
        # 1. Message Start
        msg_start = {
            "type": "message_start",
            "message": {
                "id": completion_id,
                "type": "message",
                "role": "assistant",
                "content": [],
                "model": model_name,
                "stop_reason": None,
                "stop_sequence": None,
                "usage": {"input_tokens": 0, "output_tokens": 0} 
            }
        }
        yield f"event: message_start\ndata: {orjson.dumps(msg_start).decode('utf-8')}\n\n"

        full_text = ""
        suppressor = StreamingOutputFilter()
        content_block_index = 0
        in_text_block = False

        last_chunk: ModelOutput | None = None
        # 3. Stream Deltas
        try:
            async for chunk in generator:
                last_chunk = chunk
                if chunk.text:
                    display_text = StreamingOutputFilter.TRAILING_FENCE_RE.sub('', chunk.text)
                    if len(display_text) > len(full_text):
                        delta_text = display_text[len(full_text):]
                        full_text = display_text
                        filter_events = suppressor.process(delta_text)
                        
                        for event in filter_events:
                            if event['type'] == 'text':
                                if not in_text_block:
                                    # Start a new text block if not in one
                                    yield f"event: content_block_start\ndata: {orjson.dumps({'type': 'content_block_start', 'index': content_block_index, 'content_block': {'type': 'text', 'text': ''}}).decode('utf-8')}\n\n"
                                    in_text_block = True
                                
                                delta = {
                                    "type": "content_block_delta",
                                    "index": content_block_index,
                                    "delta": {"type": "text_delta", "text": event['content']}
                                }
                                yield f"event: content_block_delta\ndata: {orjson.dumps(delta).decode('utf-8')}\n\n"
                            
                            elif event['type'] == 'tool_start':
                                if in_text_block:
                                    yield f"event: content_block_stop\ndata: {orjson.dumps({'type': 'content_block_stop', 'index': content_block_index}).decode('utf-8')}\n\n"
                                    content_block_index += 1
                                    in_text_block = False
                                
                                # Anthropic tool_use start
                                yield f"event: content_block_start\ndata: {orjson.dumps({'type': 'content_block_start', 'index': content_block_index, 'content_block': {'type': 'tool_use', 'id': event['id'], 'name': event['name'], 'input': {}}}).decode('utf-8')}\n\n"
                            
                            elif event['type'] == 'tool_delta':
                                # Anthropic tool_use delta (input_json_delta)
                                delta = {
                                    "type": "content_block_delta",
                                    "index": content_block_index,
                                    "delta": {"type": "input_json_delta", "partial_json": event['content']}
                                }
                                yield f"event: content_block_delta\ndata: {orjson.dumps(delta).decode('utf-8')}\n\n"
                            
                            elif event['type'] == 'tool_end':
                                yield f"event: content_block_stop\ndata: {orjson.dumps({'type': 'content_block_stop', 'index': content_block_index}).decode('utf-8')}\n\n"
                                content_block_index += 1
                                # We stay in "not in_text_block" state until next text event

            # Handle final delta after loop ends
            final_text = ""
            final_thoughts = ""
            if last_chunk:
                final_text = last_chunk.text or ""
                final_thoughts = last_chunk.thoughts or ""
                
            if len(final_text) > len(full_text):
                final_delta = final_text[len(full_text):]
                for event in suppressor.process(final_delta):
                    if event['type'] == 'text':
                        if not in_text_block:
                            yield f"event: content_block_start\ndata: {orjson.dumps({'type': 'content_block_start', 'index': content_block_index, 'content_block': {'type': 'text', 'text': ''}}).decode('utf-8')}\n\n"
                            in_text_block = True
                        delta = {
                            "type": "content_block_delta",
                            "index": content_block_index,
                            "delta": {"type": "text_delta", "text": event['content']}
                        }
                        yield f"event: content_block_delta\ndata: {orjson.dumps(delta).decode('utf-8')}\n\n"
                full_text = final_text
                
            # Final verification of tool calls (Built-in tools like image_retrieval often appear at the end)
            raw_output_with_think = f"<think>{final_thoughts}</think>\n" if final_thoughts else ""
            raw_output_with_think += full_text
            assistant_visible, assistant_storage, tool_calls = _process_llm_output(
                raw_output_with_think, full_text, None 
            )
            
            # If we detected tool_calls that weren't captured by suppressor yet, emit them now
            # (Note: suppressor currently handles manual formatting, but built-in tools are caught here)
            # To avoid duplicates if suppressor already found them, we could check event IDs.
            # But normally built-in tools won't trigger the regex-based suppressor.
            if tool_calls:
                for tcall in tool_calls:
                    # Emit as a new content block if it's a tool_use
                    if in_text_block:
                        yield f"event: content_block_stop\ndata: {orjson.dumps({'type': 'content_block_stop', 'index': content_block_index}).decode('utf-8')}\n\n"
                        content_block_index += 1
                        in_text_block = False
                    
                    yield f"event: content_block_start\ndata: {orjson.dumps({'type': 'content_block_start', 'index': content_block_index, 'content_block': {'type': 'tool_use', 'id': tcall.id, 'name': tcall.function.name, 'input': {}}}).decode('utf-8')}\n\n"
                    
                    yield f"event: content_block_delta\ndata: {orjson.dumps({'type': 'content_block_delta', 'index': content_block_index, 'delta': {'type': 'input_json_delta', 'partial_json': tcall.function.arguments}}).decode('utf-8')}\n\n"
                    
                    yield f"event: content_block_stop\ndata: {orjson.dumps({'type': 'content_block_stop', 'index': content_block_index}).decode('utf-8')}\n\n"
                    content_block_index += 1

            # Handle trailing buffer
            for event in suppressor.flush():
                if event['type'] == 'text':
                    if not in_text_block:
                        yield f"event: content_block_start\ndata: {orjson.dumps({'type': 'content_block_start', 'index': content_block_index, 'content_block': {'type': 'text', 'text': ''}}).decode('utf-8')}\n\n"
                        in_text_block = True
                    delta = {
                        "type": "content_block_delta",
                        "index": content_block_index,
                        "delta": {"type": "text_delta", "text": event['content']}
                    }
                    yield f"event: content_block_delta\ndata: {orjson.dumps(delta).decode('utf-8')}\n\n"
                elif event['type'] == 'tool_start':
                    if in_text_block:
                        yield f"event: content_block_stop\ndata: {orjson.dumps({'type': 'content_block_stop', 'index': content_block_index}).decode('utf-8')}\n\n"
                        content_block_index += 1
                        in_text_block = False
                    yield f"event: content_block_start\ndata: {orjson.dumps({'type': 'content_block_start', 'index': content_block_index, 'content_block': {'type': 'tool_use', 'id': event['id'], 'name': event['name'], 'input': {}}}).decode('utf-8')}\n\n"
                elif event['type'] == 'tool_delta':
                    delta = {
                        "type": "content_block_delta",
                        "index": content_block_index,
                        "delta": {"type": "input_json_delta", "partial_json": event['content']}
                    }
                    yield f"event: content_block_delta\ndata: {orjson.dumps(delta).decode('utf-8')}\n\n"
                elif event['type'] == 'tool_end':
                    yield f"event: content_block_stop\ndata: {orjson.dumps({'type': 'content_block_stop', 'index': content_block_index}).decode('utf-8')}\n\n"
                    content_block_index += 1

        except Exception as e:
             logger.exception(f"Error during Anthropic streaming: {e}")
             yield f"event: error\ndata: {orjson.dumps({'type': 'error', 'error': {'type': 'api_error', 'message': str(e)}}).decode('utf-8')}\n\n"
             return

        if in_text_block:
            yield f"event: content_block_stop\ndata: {orjson.dumps({'type': 'content_block_stop', 'index': content_block_index}).decode('utf-8')}\n\n"

        # 5. Message Delta (Stop Reason & Usage)
        p_tok = sum(estimate_tokens(text_from_message(msg)) for msg in messages)
        c_tok = estimate_tokens(full_text)
        msg_delta = {
            "type": "message_delta",
            "delta": {"stop_reason": "end_turn", "stop_sequence": None},
            "usage": {"output_tokens": c_tok}
        }
        yield f"event: message_delta\ndata: {orjson.dumps(msg_delta).decode('utf-8')}\n\n"

        # 6. Message Stop
        yield f"event: message_stop\ndata: {orjson.dumps({'type': 'message_stop'}).decode('utf-8')}\n\n"

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


# --- Main Router Endpoints ---


@router.get("/v1/models", response_model=ModelListResponse)
async def list_models(api_key: str = Depends(verify_api_key)):
    models = _get_available_models()
    return ModelListResponse(data=models)


@router.post("/v1/chat/completions")
async def create_chat_completion(
    request: ChatCompletionRequest,
    raw_request: Request,
    api_key: str = Depends(verify_api_key),
    tmp_dir: Path = Depends(get_temp_dir),
    image_store: Path = Depends(get_image_store_dir),
):
    base_url = str(raw_request.base_url)

    resp_or_stream, completion_id, created_time, session, client, db, model, msgs, struct_req = await _execute_core_chat_session(
        request_model=request.model,
        messages=request.messages,
        tmp_dir=tmp_dir,
        tools=request.tools,
        tool_choice=request.tool_choice,
        stream=request.stream,
        response_format=request.response_format,
    )

    if request.stream:
        return _create_real_streaming_response(
            resp_or_stream,
            completion_id,
            created_time,
            "gemini-3.0-pro", # Force reported model name too? Or keep request? Let's report what we used.
            msgs,
            db,
            model,
            client,
            session,
            base_url,
            struct_req,
        )

    try:
        raw_with_t = GeminiClientWrapper.extract_output(resp_or_stream, include_thoughts=True)
        raw_clean = GeminiClientWrapper.extract_output(resp_or_stream, include_thoughts=False)
    except Exception as exc:
        logger.exception("Gemini output parsing failed.")
        raise HTTPException(
            status_code=status.HTTP_502_BAD_GATEWAY, detail="Malformed response."
        ) from exc

    visible_output, storage_output, tool_calls = _process_llm_output(
        raw_with_t, raw_clean, struct_req
    )

    # Process images for OpenAI non-streaming flow
    images = resp_or_stream.images or []
    
    # Log response details for debugging
    logger.debug(f"Chat response: text_len={len(visible_output)}, images={len(images)}, tool_calls={len(tool_calls)}")
    logger.debug(f"Raw response text (first 500 chars): {raw_with_t[:500] if raw_with_t else 'EMPTY'}")
    
    # Check if response is completely empty
    if not visible_output and not images and not tool_calls:
        logger.warning("Gemini returned an empty response for chat completion (no text, images, or tool calls)")
        # Log more details about the response object
        logger.debug(f"Response object type: {type(resp_or_stream)}")
        logger.debug(f"Response has candidates: {hasattr(resp_or_stream, 'candidates')}")
        if hasattr(resp_or_stream, 'candidates'):
            logger.debug(f"Candidates: {resp_or_stream.candidates}")
        if hasattr(resp_or_stream, 'text'):
            logger.debug(f"Response text attribute: {repr(resp_or_stream.text)[:200] if resp_or_stream.text else 'None'}")
    
    image_markdown = ""
    seen_hashes = set()
    for image in images:
        try:
            _, _, _, filename, file_hash = await _image_to_base64(image, image_store)
            if file_hash in seen_hashes:
                (image_store / filename).unlink(missing_ok=True)
                continue
            seen_hashes.add(file_hash)

            img_url = (
                f"![{filename}]({base_url}images/{filename}?token={get_image_token(filename)})"
            )
            image_markdown += f"\n\n{img_url}"
        except Exception as exc:
            logger.warning(f"Failed to process image in OpenAI response: {exc}")

    if image_markdown:
        visible_output += image_markdown
        storage_output += image_markdown

    tool_calls_payload = [call.model_dump(mode="json") for call in tool_calls]
    if tool_calls_payload:
        logger.debug(f"Detected tool calls: {reprlib.repr(tool_calls_payload)}")

    p_tok, c_tok, t_tok = _calculate_usage(msgs, visible_output, tool_calls)
    usage = {"prompt_tokens": p_tok, "completion_tokens": c_tok, "total_tokens": t_tok}
    payload = _create_chat_completion_standard_payload(
        completion_id,
        created_time,
        "gemini-3.0-pro",
        visible_output,
        tool_calls_payload,
        "tool_calls" if tool_calls else "stop",
        usage,
    )
    _persist_conversation(
        db,
        model.model_name,
        client.id,
        session.metadata,
        msgs,
        storage_output,
        tool_calls,
    )
    return payload


@router.post("/v1/messages")
async def create_anthropic_message(
    request: AnthropicMessageRequest,
    raw_request: Request,
    api_key: str = Depends(verify_api_key),
    tmp_dir: Path = Depends(get_temp_dir),
):
    """
    Anthropic-compatible endpoint.
    """
    # Adapter: Convert Anthropic request to internal Message format
    # Anthropic separates 'system' from 'messages'.
    converted_messages = []
    
    # Handle system prompt
    if request.system:
        system_content = ""
        if isinstance(request.system, str):
            system_content = request.system
        elif isinstance(request.system, list):
             # Anthropic system list: [{"type": "text", "text": "..."}]
             for item in request.system:
                 if item.get("type") == "text":
                     system_content += item.get("text", "") + "\n"
        
        if system_content:
            converted_messages.append(Message(role="system", content=system_content))

    # Handle messages (with potential Anthropic-style content arrays)
    for msg in request.messages:
        original_content = msg.content
        if isinstance(original_content, list):
            # Adapter: Convert Anthropic content array to internal ContentItem list
            new_content = []
            for part in original_content:
                if isinstance(part, dict):
                    p_type = part.get("type")
                    if p_type == "text":
                         new_content.append(ContentItem(type="text", text=part.get("text")))
                    elif p_type == "image" and "source" in part:
                         # Anthropic image -> internal image_url
                         source = part["source"]
                         if source.get("type") == "base64":
                             data = source.get("data")
                             media_type = source.get("media_type")
                             new_content.append(ContentItem(
                                 type="image_url",
                                 image_url={"url": f"data:{media_type};base64,{data}"}
                             ))
                    else:
                         # Fallback for other types or already compatible parts
                         pass
            if new_content:
                msg.content = new_content
        converted_messages.append(msg)

    # Tools: Convert Anthropic tool format to OpenAI-compatible format
    # Anthropic tools: [{"name": "...", "description": "...", "input_schema": {...}}]
    # OpenAI tools: [{"type": "function", "function": {"name": "...", "description": "...", "parameters": {...}}}]
    converted_tools = None
    if request.tools:
        converted_tools = []
        for tool in request.tools:
            if isinstance(tool, dict):
                # Anthropic format: {name, description, input_schema}
                tool_name = tool.get("name")
                tool_desc = tool.get("description", "")
                # Anthropic uses "input_schema", OpenAI uses "parameters"
                tool_params = tool.get("input_schema") or tool.get("parameters", {})
                if tool_name:
                    converted_tools.append(Tool(
                        type="function",
                        function=ToolFunctionDefinition(
                            name=tool_name,
                            description=tool_desc,
                            parameters=tool_params
                        )
                    ))

    # Convert Anthropic tool_choice to OpenAI format
    converted_tool_choice = None
    if request.tool_choice:
        if isinstance(request.tool_choice, dict):
            # Anthropic: {"type": "tool", "name": "..."} or {"type": "auto"} etc.
            choice_type = request.tool_choice.get("type")
            if choice_type == "tool":
                tool_name = request.tool_choice.get("name")
                if tool_name:
                    converted_tool_choice = ToolChoiceFunction(
                        type="function",
                        function=ToolChoiceFunctionDetail(name=tool_name)
                    )
            elif choice_type in ("auto", "any"):
                converted_tool_choice = "auto"  # "any" maps to "auto" for now
            elif choice_type == "none":
                converted_tool_choice = "none"
        elif isinstance(request.tool_choice, str):
            # Direct string values: "auto", "any", "none"
            if request.tool_choice == "none":
                converted_tool_choice = "none"
            else:
                converted_tool_choice = "auto"  # "auto" and "any" both map to auto

    resp_or_stream, completion_id, created_time, session, client, db, model, msgs, struct_req = await _execute_core_chat_session(
        request_model=request.model,
        messages=converted_messages,
        tmp_dir=tmp_dir,
        tools=converted_tools,
        tool_choice=converted_tool_choice,
        stream=request.stream,
        response_format=None, # Anthropic doesn't use response_format this way
    )

    if request.stream:
        return await _create_anthropic_streaming_response(
            resp_or_stream,
            completion_id,
            created_time,
            "gemini-3.0-pro",
            msgs,
        )

    # Non-streaming response
    try:
        raw_clean = GeminiClientWrapper.extract_output(resp_or_stream, include_thoughts=False)
        # Anthropic doesn't support thoughts natively in same field, let's just return clean text.
    except Exception as exc:
        raise HTTPException(status_code=502, detail="Malformed response") from exc

    # Process tool calls from the response
    raw_with_think = GeminiClientWrapper.extract_output(resp_or_stream, include_thoughts=True)
    assistant_visible, assistant_storage, tool_calls = _process_llm_output(raw_with_think, raw_clean, None)

    # Build Anthropic-style content blocks
    content_blocks = []
    if assistant_visible:
        content_blocks.append({"type": "text", "text": assistant_visible})

    # Add tool_use blocks if tool calls detected
    if tool_calls:
        for tcall in tool_calls:
            # Safely parse tool arguments
            tool_input = {}
            if tcall.function.arguments:
                try:
                    tool_input = orjson.loads(tcall.function.arguments)
                except orjson.JSONDecodeError:
                    # If parsing fails, use the raw string as input
                    tool_input = {"raw": tcall.function.arguments}
            content_blocks.append({
                "type": "tool_use",
                "id": tcall.id,
                "name": tcall.function.name,
                "input": tool_input
            })

    p_tok, c_tok, t_tok = _calculate_usage(converted_messages, assistant_visible, tool_calls)

    # Determine stop_reason based on whether tool calls were made
    stop_reason = "tool_use" if tool_calls else "end_turn"

    # Persist conversation for future reuse
    _persist_conversation(
        db,
        model.model_name,
        client.id,
        session.metadata,
        msgs,
        assistant_storage,
        tool_calls,
    )

    return {
        "id": completion_id,
        "type": "message",
        "role": "assistant",
        "content": content_blocks,
        "model": "gemini-3.0-pro",
        "stop_reason": stop_reason,
        "stop_sequence": None,
        "usage": {
            "input_tokens": p_tok,
            "output_tokens": c_tok
        }
    }


@router.post("/v1/responses")
async def create_response(
    request: ResponseCreateRequest,
    raw_request: Request,
    api_key: str = Depends(verify_api_key),
    tmp_dir: Path = Depends(get_temp_dir),
    image_store: Path = Depends(get_image_store_dir),
):
    base_url = str(raw_request.base_url)
    base_messages, norm_input = _response_items_to_messages(request.input)
    struct_req = _build_structured_requirement(request.response_format)
    extra_instr = [struct_req.instruction] if struct_req else []

    standard_tools, image_tools = [], []
    if request.tools:
        for t in request.tools:
            if isinstance(t, Tool):
                standard_tools.append(t)
            elif isinstance(t, ResponseImageTool):
                image_tools.append(t)
            elif isinstance(t, dict):
                if t.get("type") == "function":
                    standard_tools.append(Tool.model_validate(t))
                elif t.get("type") == "image_generation":
                    image_tools.append(ResponseImageTool.model_validate(t))

    img_instr = _build_image_generation_instruction(
        image_tools,
        request.tool_choice if isinstance(request.tool_choice, ResponseToolChoice) else None,
    )
    if img_instr:
        extra_instr.append(img_instr)
    preface = _instructions_to_messages(request.instructions)
    conv_messages = [*preface, *base_messages] if preface else base_messages
    model_tool_choice = (
        request.tool_choice if isinstance(request.tool_choice, (str, ToolChoiceFunction)) else None
    )

    # Reuse core logic for session setup? 
    # Responses API is slightly distinct (ResponseCreateRequest), let's keep it separate or partially aligned.
    # It uses 'response.model' which we must also force.
    
    # Manual force for this endpoint too:
    request.model = "gemini-3.0-pro" # Force locally
    
    messages = _prepare_messages_for_model(
        conv_messages,
        standard_tools or None,
        model_tool_choice,
        extra_instr or None,
    )
    pool, db = GeminiClientPool(), LMDBConversationStore()
    try:
        model = _get_model_by_name(request.model)
    except ValueError as exc:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc

    session, client, remain = await _find_reusable_session(db, pool, model, messages)
    if session:
        # Special case: if the last message is a tool result, process it even if remain is empty
        if not remain and messages and messages[-1].role == "tool":
            remain = [messages[-1]]
        msgs = _prepare_messages_for_model(
            remain,
            standard_tools or None,
            model_tool_choice,
            None,
            False,
        )
        if not msgs:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="No new messages.")
        m_input, files = (
            await GeminiClientWrapper.process_message(msgs[0], tmp_dir, tagged=False)
            if len(msgs) == 1
            else await GeminiClientWrapper.process_conversation(msgs, tmp_dir)
        )
        logger.debug(
            f"Reused session {reprlib.repr(session.metadata)} - sending {len(msgs)} prepared messages."
        )
    else:
        try:
            client = await pool.acquire()
            session = client.start_chat(model=model)
            m_input, files = await GeminiClientWrapper.process_conversation(messages, tmp_dir)
        except Exception as e:
            logger.exception("Error in preparing conversation")
            raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=str(e))

    response_id = f"resp_{uuid.uuid4().hex}"
    created_time = int(datetime.now(tz=timezone.utc).timestamp())

    try:
        assert session and client
        logger.debug(
            f"Client ID: {client.id}, Input length: {len(m_input)}, files count: {len(files)}"
        )
        resp_or_stream = await _send_with_split(
            session, m_input, files=files, stream=request.stream
        )
    except Exception as e:
        logger.exception("Gemini API error")
        raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail=str(e))

    if request.stream:
        return _create_responses_real_streaming_response(
            resp_or_stream,
            response_id,
            created_time,
            request.model,
            messages,
            db,
            model,
            client,
            session,
            request,
            image_store,
            base_url,
            struct_req,
        )

    try:
        raw_t = GeminiClientWrapper.extract_output(resp_or_stream, include_thoughts=True)
        raw_c = GeminiClientWrapper.extract_output(resp_or_stream, include_thoughts=False)
    except Exception as exc:
        logger.exception("Gemini parsing failed")
        raise HTTPException(
            status_code=status.HTTP_502_BAD_GATEWAY, detail="Malformed response."
        ) from exc

    assistant_text, storage_output, tool_calls = _process_llm_output(raw_t, raw_c, struct_req)
    images = resp_or_stream.images or []
    
    # Log response details for debugging
    logger.debug(f"Response: text_len={len(assistant_text)}, images={len(images)}, tool_calls={len(tool_calls)}")
    
    # Check if response is completely empty (no text, no images, no tool calls)
    if not assistant_text and not images and not tool_calls:
        logger.warning("Gemini returned an empty response (no text, images, or tool calls)")
        # Check if there's an error in the response
        if hasattr(resp_or_stream, 'candidates') and resp_or_stream.candidates:
            logger.debug(f"Candidates: {resp_or_stream.candidates}")
    
    # Check if image generation was forced via tool_choice
    # Only enforce image requirement if tool_choice explicitly requests image generation
    # tools: [{"type": "image_generation"}] just declares the tool is available, not mandatory
    image_generation_forced = (
        request.tool_choice is not None 
        and isinstance(request.tool_choice, ResponseToolChoice) 
        and request.tool_choice.type == "image_generation"
    )
    if image_generation_forced and not images and not assistant_text:
        logger.warning("Image generation was forced via tool_choice but no images or text were returned.")
        raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="No images returned from forced image generation request.")

    contents, img_calls = [], []
    seen_hashes = set()
    for img in images:
        try:
            b64, w, h, fname, fhash = await _image_to_base64(img, image_store)
            if fhash in seen_hashes:
                (image_store / fname).unlink(missing_ok=True)
                continue
            seen_hashes.add(fhash)

            contents.append(
                ResponseOutputContent(
                    type="output_text",
                    text=f"![{fname}]({base_url}images/{fname}?token={get_image_token(fname)})",
                )
            )
            img_calls.append(
                ResponseImageGenerationCall(
                    id=fname.rsplit(".", 1)[0],
                    result=b64,
                    output_format="png" if isinstance(img, GeneratedImage) else "jpeg",
                    size=f"{w}x{h}" if w and h else None,
                )
            )
        except Exception as e:
            logger.warning(f"Image error: {e}")

    if assistant_text:
        contents.append(ResponseOutputContent(type="output_text", text=assistant_text))
    if not contents:
        contents.append(ResponseOutputContent(type="output_text", text=""))

    # Aggregate images for storage
    image_markdown = ""
    for img_call in img_calls:
        fname = f"{img_call.id}.{img_call.output_format}"
        img_url = f"![{fname}]({base_url}images/{fname}?token={get_image_token(fname)})"
        image_markdown += f"\n\n{img_url}"

    if image_markdown:
        storage_output += image_markdown

    p_tok, c_tok, t_tok = _calculate_usage(messages, assistant_text, tool_calls)
    usage = ResponseUsage(input_tokens=p_tok, output_tokens=c_tok, total_tokens=t_tok)
    payload = _create_responses_standard_payload(
        response_id,
        created_time,
        request.model,
        tool_calls,
        img_calls,
        contents,
        usage,
        request,
        norm_input,
    )
    _persist_conversation(
        db, model.model_name, client.id, session.metadata, messages, storage_output, tool_calls
    )
    return payload