@@ -514,33 +514,31 @@ func (bifrost *Bifrost) TextCompletionRequest(ctx context.Context, req *schemas.
514
514
func (bifrost * Bifrost ) tryTextCompletion (req * schemas.BifrostRequest , ctx context.Context ) (* schemas.BifrostResponse , * schemas.BifrostError ) {
515
515
queue , err := bifrost .getProviderQueue (req .Provider )
516
516
if err != nil {
517
- return nil , & schemas.BifrostError {
518
- IsBifrostError : false ,
519
- Error : schemas.ErrorField {
520
- Message : err .Error (),
521
- },
522
- }
517
+ return nil , newBifrostError (err )
523
518
}
524
519
525
- for _ , plugin := range bifrost .plugins {
526
- req , err = plugin .PreHook (& ctx , req )
520
+ var resp * schemas.BifrostResponse
521
+ var processedPluginCount int
522
+ for i , plugin := range bifrost .plugins {
523
+ req , resp , err = plugin .PreHook (& ctx , req )
527
524
if err != nil {
528
- return nil , & schemas.BifrostError {
529
- IsBifrostError : false ,
530
- Error : schemas.ErrorField {
531
- Message : err .Error (),
532
- },
525
+ return nil , newBifrostError (err )
526
+ }
527
+ processedPluginCount = i + 1
528
+ if resp != nil {
529
+ // Run post-hooks in reverse order for plugins that had PreHook executed
530
+ for j := processedPluginCount - 1 ; j >= 0 ; j -- {
531
+ resp , err = bifrost .plugins [j ].PostHook (& ctx , resp )
532
+ if err != nil {
533
+ return nil , newBifrostError (err )
534
+ }
533
535
}
536
+ return resp , nil
534
537
}
535
538
}
536
539
537
540
if req == nil {
538
- return nil , & schemas.BifrostError {
539
- IsBifrostError : false ,
540
- Error : schemas.ErrorField {
541
- Message : "bifrost request after plugin hooks cannot be nil" ,
542
- },
543
- }
541
+ return nil , newBifrostErrorFromMsg ("bifrost request after plugin hooks cannot be nil" )
544
542
}
545
543
546
544
// Get a ChannelMessage from the pool
@@ -554,23 +552,13 @@ func (bifrost *Bifrost) tryTextCompletion(req *schemas.BifrostRequest, ctx conte
554
552
case <- ctx .Done ():
555
553
// Request was cancelled by caller
556
554
bifrost .releaseChannelMessage (msg )
557
- return nil , & schemas.BifrostError {
558
- IsBifrostError : false ,
559
- Error : schemas.ErrorField {
560
- Message : "request cancelled while waiting for queue space" ,
561
- },
562
- }
555
+ return nil , newBifrostErrorFromMsg ("request cancelled while waiting for queue space" )
563
556
default :
564
557
if bifrost .dropExcessRequests {
565
558
// Drop request immediately if configured to do so
566
559
bifrost .releaseChannelMessage (msg )
567
560
bifrost .logger .Warn ("Request dropped: queue is full, please increase the queue size or set dropExcessRequests to false" )
568
- return nil , & schemas.BifrostError {
569
- IsBifrostError : false ,
570
- Error : schemas.ErrorField {
571
- Message : "request dropped: queue is full" ,
572
- },
573
- }
561
+ return nil , newBifrostErrorFromMsg ("request dropped: queue is full" )
574
562
}
575
563
576
564
// If not dropping excess requests, wait with context
@@ -582,12 +570,7 @@ func (bifrost *Bifrost) tryTextCompletion(req *schemas.BifrostRequest, ctx conte
582
570
// Message was sent successfully
583
571
case <- ctx .Done ():
584
572
bifrost .releaseChannelMessage (msg )
585
- return nil , & schemas.BifrostError {
586
- IsBifrostError : false ,
587
- Error : schemas.ErrorField {
588
- Message : "request cancelled while waiting for queue space" ,
589
- },
590
- }
573
+ return nil , newBifrostErrorFromMsg ("request cancelled while waiting for queue space" )
591
574
}
592
575
}
593
576
@@ -600,12 +583,7 @@ func (bifrost *Bifrost) tryTextCompletion(req *schemas.BifrostRequest, ctx conte
600
583
result , err = bifrost .plugins [i ].PostHook (& ctx , result )
601
584
if err != nil {
602
585
bifrost .releaseChannelMessage (msg )
603
- return nil , & schemas.BifrostError {
604
- IsBifrostError : false ,
605
- Error : schemas.ErrorField {
606
- Message : err .Error (),
607
- },
608
- }
586
+ return nil , newBifrostError (err )
609
587
}
610
588
}
611
589
case err := <- msg .Err :
@@ -623,30 +601,15 @@ func (bifrost *Bifrost) tryTextCompletion(req *schemas.BifrostRequest, ctx conte
623
601
// If the primary provider fails, it will try each fallback provider in order until one succeeds.
624
602
func (bifrost * Bifrost ) ChatCompletionRequest (ctx context.Context , req * schemas.BifrostRequest ) (* schemas.BifrostResponse , * schemas.BifrostError ) {
625
603
if req == nil {
626
- return nil , & schemas.BifrostError {
627
- IsBifrostError : false ,
628
- Error : schemas.ErrorField {
629
- Message : "bifrost request cannot be nil" ,
630
- },
631
- }
604
+ return nil , newBifrostErrorFromMsg ("bifrost request cannot be nil" )
632
605
}
633
606
634
607
if req .Provider == "" {
635
- return nil , & schemas.BifrostError {
636
- IsBifrostError : false ,
637
- Error : schemas.ErrorField {
638
- Message : "provider is required" ,
639
- },
640
- }
608
+ return nil , newBifrostErrorFromMsg ("provider is required" )
641
609
}
642
610
643
611
if req .Model == "" {
644
- return nil , & schemas.BifrostError {
645
- IsBifrostError : false ,
646
- Error : schemas.ErrorField {
647
- Message : "model is required" ,
648
- },
649
- }
612
+ return nil , newBifrostErrorFromMsg ("model is required" )
650
613
}
651
614
652
615
// Try the primary provider first
@@ -688,33 +651,31 @@ func (bifrost *Bifrost) ChatCompletionRequest(ctx context.Context, req *schemas.
688
651
func (bifrost * Bifrost ) tryChatCompletion (req * schemas.BifrostRequest , ctx context.Context ) (* schemas.BifrostResponse , * schemas.BifrostError ) {
689
652
queue , err := bifrost .getProviderQueue (req .Provider )
690
653
if err != nil {
691
- return nil , & schemas.BifrostError {
692
- IsBifrostError : false ,
693
- Error : schemas.ErrorField {
694
- Message : err .Error (),
695
- },
696
- }
654
+ return nil , newBifrostError (err )
697
655
}
698
656
699
- for _ , plugin := range bifrost .plugins {
700
- req , err = plugin .PreHook (& ctx , req )
657
+ var resp * schemas.BifrostResponse
658
+ var processedPluginCount int
659
+ for i , plugin := range bifrost .plugins {
660
+ req , resp , err = plugin .PreHook (& ctx , req )
701
661
if err != nil {
702
- return nil , & schemas.BifrostError {
703
- IsBifrostError : false ,
704
- Error : schemas.ErrorField {
705
- Message : err .Error (),
706
- },
662
+ return nil , newBifrostError (err )
663
+ }
664
+ processedPluginCount = i + 1
665
+ if resp != nil {
666
+ // Run post-hooks in reverse order for plugins that had PreHook executed
667
+ for j := processedPluginCount - 1 ; j >= 0 ; j -- {
668
+ resp , err = bifrost .plugins [j ].PostHook (& ctx , resp )
669
+ if err != nil {
670
+ return nil , newBifrostError (err )
671
+ }
707
672
}
673
+ return resp , nil
708
674
}
709
675
}
710
676
711
677
if req == nil {
712
- return nil , & schemas.BifrostError {
713
- IsBifrostError : false ,
714
- Error : schemas.ErrorField {
715
- Message : "bifrost request after plugin hooks cannot be nil" ,
716
- },
717
- }
678
+ return nil , newBifrostErrorFromMsg ("bifrost request after plugin hooks cannot be nil" )
718
679
}
719
680
720
681
// Get a ChannelMessage from the pool
@@ -728,23 +689,13 @@ func (bifrost *Bifrost) tryChatCompletion(req *schemas.BifrostRequest, ctx conte
728
689
case <- ctx .Done ():
729
690
// Request was cancelled by caller
730
691
bifrost .releaseChannelMessage (msg )
731
- return nil , & schemas.BifrostError {
732
- IsBifrostError : false ,
733
- Error : schemas.ErrorField {
734
- Message : "request cancelled while waiting for queue space" ,
735
- },
736
- }
692
+ return nil , newBifrostErrorFromMsg ("request cancelled while waiting for queue space" )
737
693
default :
738
694
if bifrost .dropExcessRequests {
739
695
// Drop request immediately if configured to do so
740
696
bifrost .releaseChannelMessage (msg )
741
697
bifrost .logger .Warn ("Request dropped: queue is full, please increase the queue size or set dropExcessRequests to false" )
742
- return nil , & schemas.BifrostError {
743
- IsBifrostError : false ,
744
- Error : schemas.ErrorField {
745
- Message : "request dropped: queue is full" ,
746
- },
747
- }
698
+ return nil , newBifrostErrorFromMsg ("request dropped: queue is full" )
748
699
}
749
700
// If not dropping excess requests, wait with context
750
701
if ctx == nil {
@@ -755,12 +706,7 @@ func (bifrost *Bifrost) tryChatCompletion(req *schemas.BifrostRequest, ctx conte
755
706
// Message was sent successfully
756
707
case <- ctx .Done ():
757
708
bifrost .releaseChannelMessage (msg )
758
- return nil , & schemas.BifrostError {
759
- IsBifrostError : false ,
760
- Error : schemas.ErrorField {
761
- Message : "request cancelled while waiting for queue space" ,
762
- },
763
- }
709
+ return nil , newBifrostErrorFromMsg ("request cancelled while waiting for queue space" )
764
710
}
765
711
}
766
712
@@ -773,12 +719,7 @@ func (bifrost *Bifrost) tryChatCompletion(req *schemas.BifrostRequest, ctx conte
773
719
result , err = bifrost .plugins [i ].PostHook (& ctx , result )
774
720
if err != nil {
775
721
bifrost .releaseChannelMessage (msg )
776
- return nil , & schemas.BifrostError {
777
- IsBifrostError : false ,
778
- Error : schemas.ErrorField {
779
- Message : err .Error (),
780
- },
781
- }
722
+ return nil , newBifrostError (err )
782
723
}
783
724
}
784
725
case err := <- msg .Err :
@@ -794,7 +735,7 @@ func (bifrost *Bifrost) tryChatCompletion(req *schemas.BifrostRequest, ctx conte
794
735
// Cleanup gracefully stops all workers when triggered.
795
736
// It closes all request channels and waits for workers to exit.
796
737
func (bifrost * Bifrost ) Cleanup () {
797
- bifrost .logger .Info ("[BIFROST] Graceful Cleanup Initiated - Closing all request channels..." )
738
+ bifrost .logger .Info ("Graceful Cleanup Initiated - Closing all request channels..." )
798
739
799
740
// Close all provider queues to signal workers to stop
800
741
for _ , queue := range bifrost .requestQueues {
@@ -805,4 +746,14 @@ func (bifrost *Bifrost) Cleanup() {
805
746
for _ , waitGroup := range bifrost .waitGroups {
806
747
waitGroup .Wait ()
807
748
}
749
+
750
+ // Cleanup plugins
751
+ for _ , plugin := range bifrost .plugins {
752
+ err := plugin .Cleanup ()
753
+ if err != nil {
754
+ bifrost .logger .Warn (fmt .Sprintf ("Error cleaning up plugin: %s" , err .Error ()))
755
+ }
756
+ }
757
+
758
+ bifrost .logger .Info ("Graceful Cleanup Completed" )
808
759
}
0 commit comments