77 "fmt"
88 "io"
99 "net/http"
10+ "strings"
1011 "sync"
1112 "time"
1213
@@ -117,11 +118,11 @@ func (f *Factory) Execute(req *Request) *Response {
117118 }
118119 }
119120
120- // Build metadata
121+ // Build metadata (only include successful responses)
121122 metadata .TotalLatencyMs = totalLatency
122123 metadata .ModelLatenciesMs = make (map [string ]int64 )
123124 metadata .ConfidenceScores = make (map [string ]float64 )
124- for _ , resp := range responses {
125+ for _ , resp := range successfulResponses {
125126 metadata .ModelLatenciesMs [resp .ModelName ] = resp .Latency .Milliseconds ()
126127 if resp .Confidence > 0 {
127128 metadata .ConfidenceScores [resp .ModelName ] = resp .Confidence
@@ -145,8 +146,12 @@ func (f *Factory) queryModels(req *Request) []ModelResponse {
145146 responses := make ([]ModelResponse , len (req .Models ))
146147 var wg sync.WaitGroup
147148
148- // Limit concurrent requests
149- semaphore := make (chan struct {}, f .config .MaxConcurrentRequests )
149+ // Limit concurrent requests (ensure at least 1)
150+ maxConcurrent := f .config .MaxConcurrentRequests
151+ if maxConcurrent <= 0 {
152+ maxConcurrent = 10 // Default to 10 if not set or invalid
153+ }
154+ semaphore := make (chan struct {}, maxConcurrent )
150155
151156 for i , modelName := range req .Models {
152157 wg .Add (1 )
@@ -157,7 +162,7 @@ func (f *Factory) queryModels(req *Request) []ModelResponse {
157162 semaphore <- struct {}{}
158163 defer func () { <- semaphore }()
159164
160- responses [idx ] = f .queryModel (req .Context , model , req .OriginalRequest )
165+ responses [idx ] = f .queryModel (req .Context , model , req .OriginalRequest , req . Headers )
161166 }(i , modelName )
162167 }
163168
@@ -166,7 +171,7 @@ func (f *Factory) queryModels(req *Request) []ModelResponse {
166171}
167172
168173// queryModel queries a single model endpoint
169- func (f * Factory ) queryModel (ctx context.Context , modelName string , requestBody []byte ) ModelResponse {
174+ func (f * Factory ) queryModel (ctx context.Context , modelName string , requestBody []byte , headers map [ string ] string ) ModelResponse {
170175 startTime := time .Now ()
171176
172177 endpoint , ok := f .endpoints [modelName ]
@@ -200,6 +205,15 @@ func (f *Factory) queryModel(ctx context.Context, modelName string, requestBody
200205
201206 httpReq .Header .Set ("Content-Type" , "application/json" )
202207
208+ // Forward authentication and other headers from original request
209+ for key , value := range headers {
210+ // Forward authorization and other important headers
211+ lowerKey := strings .ToLower (key )
212+ if lowerKey == "authorization" || lowerKey == "x-api-key" || strings .HasPrefix (lowerKey , "x-" ) {
213+ httpReq .Header .Set (key , value )
214+ }
215+ }
216+
203217 // Execute request
204218 resp , err := f .httpClient .Do (httpReq )
205219 if err != nil {
0 commit comments