Files
Luminary/internal/gateway/model_resolve.go
T
renjue aca63d7526 Add model aliases, routing UX, and admin hardening.
Support ingress-facing model aliases with upstream rewrite, category-aware health checks, provider name validation, modal click-outside guard, and dropdown-based routing rule targets.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-21 02:49:49 +08:00

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)
}