|
| 1 | +package llmgateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "net/http/httputil" |
| 10 | + "net/url" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/juice-shop/multi-juicer/balancer/pkg/signutil" |
| 14 | +) |
| 15 | + |
| 16 | +// openAIResponse is a minimal representation of an OpenAI chat completion response for usage extraction. |
| 17 | +type openAIResponse struct { |
| 18 | + Usage *openAIUsage `json:"usage,omitempty"` |
| 19 | +} |
| 20 | + |
| 21 | +type openAIUsage struct { |
| 22 | + InputTokens int64 `json:"prompt_tokens"` |
| 23 | + OutputTokens int64 `json:"completion_tokens"` |
| 24 | +} |
| 25 | + |
| 26 | +// Gateway proxies LLM requests from JuiceShop instances to an upstream LLM API. |
| 27 | +type Gateway struct { |
| 28 | + signingKey string |
| 29 | + upstreamURL *url.URL |
| 30 | + apiKey string |
| 31 | + usage *UsageTracker |
| 32 | + logger *log.Logger |
| 33 | +} |
| 34 | + |
| 35 | +// NewGateway creates a new LLM gateway. |
| 36 | +func NewGateway(signingKey string, upstreamURL string, apiKey string, usage *UsageTracker, logger *log.Logger) (*Gateway, error) { |
| 37 | + u, err := url.Parse(upstreamURL) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + return &Gateway{ |
| 42 | + signingKey: signingKey, |
| 43 | + upstreamURL: u, |
| 44 | + apiKey: apiKey, |
| 45 | + usage: usage, |
| 46 | + logger: logger, |
| 47 | + }, nil |
| 48 | +} |
| 49 | + |
| 50 | +func (g *Gateway) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 51 | + // Extract bearer token |
| 52 | + authHeader := r.Header.Get("Authorization") |
| 53 | + if !strings.HasPrefix(authHeader, "Bearer ") { |
| 54 | + http.Error(w, `{"error":"missing or invalid Authorization header"}`, http.StatusUnauthorized) |
| 55 | + return |
| 56 | + } |
| 57 | + teamToken := strings.TrimPrefix(authHeader, "Bearer ") |
| 58 | + |
| 59 | + // Validate token by verifying the HMAC signature and extracting the team name |
| 60 | + team, err := signutil.Unsign(teamToken, g.signingKey) |
| 61 | + if err != nil { |
| 62 | + http.Error(w, `{"error":"invalid token"}`, http.StatusUnauthorized) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + // Check if this is a chat completions request (for usage tracking) |
| 67 | + isChatCompletion := strings.Contains(r.URL.Path, "/chat/completions") |
| 68 | + g.logger.Printf("LLM gateway: request from team '%s': %s %s (isChatCompletion=%v)", team, r.Method, r.URL.Path, isChatCompletion) |
| 69 | + |
| 70 | + // Create reverse proxy |
| 71 | + proxy := &httputil.ReverseProxy{ |
| 72 | + Rewrite: func(pr *httputil.ProxyRequest) { |
| 73 | + pr.SetURL(g.upstreamURL) |
| 74 | + pr.Out.Host = g.upstreamURL.Host |
| 75 | + // Replace the authorization header with the real API key |
| 76 | + pr.Out.Header.Set("Authorization", "Bearer "+g.apiKey) |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + if isChatCompletion { |
| 81 | + proxy.ModifyResponse = func(resp *http.Response) error { |
| 82 | + return g.extractUsage(resp, team) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) { |
| 87 | + g.logger.Printf("LLM gateway proxy error for team '%s': %v", team, err) |
| 88 | + http.Error(w, `{"error":"upstream LLM API error"}`, http.StatusBadGateway) |
| 89 | + } |
| 90 | + |
| 91 | + proxy.ServeHTTP(w, r) |
| 92 | +} |
| 93 | + |
| 94 | +func (g *Gateway) extractUsage(resp *http.Response, team string) error { |
| 95 | + if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 96 | + return nil |
| 97 | + } |
| 98 | + |
| 99 | + contentType := resp.Header.Get("Content-Type") |
| 100 | + isSSE := strings.Contains(contentType, "text/event-stream") |
| 101 | + |
| 102 | + body, err := io.ReadAll(resp.Body) |
| 103 | + resp.Body.Close() |
| 104 | + if err != nil { |
| 105 | + g.logger.Printf("LLM gateway: failed to read response body for team '%s': %v", team, err) |
| 106 | + resp.Body = io.NopCloser(bytes.NewReader(body)) |
| 107 | + return nil |
| 108 | + } |
| 109 | + |
| 110 | + // Restore the body for the client |
| 111 | + resp.Body = io.NopCloser(bytes.NewReader(body)) |
| 112 | + |
| 113 | + if isSSE { |
| 114 | + g.extractUsageFromSSE(body, team) |
| 115 | + } else { |
| 116 | + g.extractUsageFromJSON(body, team) |
| 117 | + } |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func (g *Gateway) extractUsageFromJSON(body []byte, team string) { |
| 122 | + var result openAIResponse |
| 123 | + if err := json.Unmarshal(body, &result); err != nil { |
| 124 | + return |
| 125 | + } |
| 126 | + if result.Usage != nil { |
| 127 | + g.logger.Printf("LLM gateway: usage for team '%s': input_tokens=%d, output_tokens=%d", team, result.Usage.InputTokens, result.Usage.OutputTokens) |
| 128 | + g.usage.Add(team, result.Usage.InputTokens, result.Usage.OutputTokens) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// extractUsageFromSSE scans SSE events for usage data, which typically appears in the last chunk. |
| 133 | +func (g *Gateway) extractUsageFromSSE(body []byte, team string) { |
| 134 | + // SSE format: lines starting with "data: " contain JSON payloads |
| 135 | + // Scan all data lines for usage (it's usually in the last real chunk before "data: [DONE]") |
| 136 | + lines := strings.Split(string(body), "\n") |
| 137 | + for _, line := range lines { |
| 138 | + line = strings.TrimSpace(line) |
| 139 | + if !strings.HasPrefix(line, "data: ") { |
| 140 | + continue |
| 141 | + } |
| 142 | + payload := strings.TrimPrefix(line, "data: ") |
| 143 | + if payload == "[DONE]" { |
| 144 | + continue |
| 145 | + } |
| 146 | + var chunk openAIResponse |
| 147 | + if err := json.Unmarshal([]byte(payload), &chunk); err != nil { |
| 148 | + continue |
| 149 | + } |
| 150 | + if chunk.Usage != nil { |
| 151 | + g.logger.Printf("LLM gateway: SSE usage for team '%s': input_tokens=%d, output_tokens=%d", team, chunk.Usage.InputTokens, chunk.Usage.OutputTokens) |
| 152 | + g.usage.Add(team, chunk.Usage.InputTokens, chunk.Usage.OutputTokens) |
| 153 | + return |
| 154 | + } |
| 155 | + } |
| 156 | + g.logger.Printf("LLM gateway: no usage data found in SSE stream for team '%s'", team) |
| 157 | +} |
0 commit comments