76ba500417
CI / docker (push) Successful in 2m4s
OpenAI-compatible AI gateway with Vue admin UI, multi-provider egress, ingress key governance, monitoring, and security controls. Co-authored-by: Cursor <cursoragent@cursor.com>
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/rose_cat707/luminary/internal/model"
|
|
)
|
|
|
|
// IngressModelID returns the model identifier exposed to ingress clients.
|
|
func IngressModelID(m model.ModelEntry) string {
|
|
return m.IngressID()
|
|
}
|
|
|
|
func modelEntryMatches(m model.ModelEntry, name string) bool {
|
|
return m.MatchesIngressName(name)
|
|
}
|
|
|
|
func (s *Service) resolveUpstreamModelID(providerID uint, ingressModel string) string {
|
|
if ingressModel == "" {
|
|
return ""
|
|
}
|
|
for _, m := range s.cache.GetModels(providerID) {
|
|
if modelEntryMatches(m, ingressModel) {
|
|
return m.ModelID
|
|
}
|
|
}
|
|
return ingressModel
|
|
}
|
|
|
|
func rewriteModelInBody(body []byte, upstreamModel string) ([]byte, error) {
|
|
if upstreamModel == "" || len(body) == 0 {
|
|
return body, nil
|
|
}
|
|
var payload map[string]any
|
|
if err := json.Unmarshal(body, &payload); err != nil {
|
|
return body, nil
|
|
}
|
|
current, _ := payload["model"].(string)
|
|
if current == "" || current == upstreamModel {
|
|
return body, nil
|
|
}
|
|
payload["model"] = upstreamModel
|
|
return json.Marshal(payload)
|
|
}
|
|
|
|
func (s *Service) rewriteBodyForProvider(providerID uint, body []byte) ([]byte, error) {
|
|
ingressModel := extractModelName(body)
|
|
if ingressModel == "" {
|
|
return body, nil
|
|
}
|
|
upstream := s.resolveUpstreamModelID(providerID, ingressModel)
|
|
return rewriteModelInBody(body, upstream)
|
|
}
|