CI / test (push) Failing after 6m30s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
135 lines
4.1 KiB
Go
135 lines
4.1 KiB
Go
package router
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/auth"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/web"
|
|
)
|
|
|
|
// What a person may say not to use, besides a licence by its id: every
|
|
// licence that forbids commercial use, and every licence that grants nothing
|
|
// in a region. Rules rather than a list, so a model added later under such
|
|
// a licence is left out without anybody remembering to tick it.
|
|
const (
|
|
licenceUnspecified = "unspecified"
|
|
ruleNonCommercial = "rule:non-commercial"
|
|
ruleRegionPrefix = "rule:region:"
|
|
)
|
|
|
|
// licenceKey is how a model's licence is matched: its id, else its name,
|
|
// lowercased; licenceUnspecified for a model whose entry names none.
|
|
func licenceKey(m catalog.Model) string {
|
|
if m.License == nil {
|
|
return licenceUnspecified
|
|
}
|
|
if m.License.ID != "" {
|
|
return strings.ToLower(m.License.ID)
|
|
}
|
|
return strings.ToLower(strings.TrimSpace(m.License.Name))
|
|
}
|
|
|
|
// licenceExcluded says which of the person's choices leaves a model out,
|
|
// empty when none does.
|
|
func licenceExcluded(excluded []string, m catalog.Model) string {
|
|
if len(excluded) == 0 {
|
|
return ""
|
|
}
|
|
key := licenceKey(m)
|
|
// Its name too: a choice made before the entry gave an id keeps
|
|
// holding once it does.
|
|
name := key
|
|
if m.License != nil {
|
|
name = strings.ToLower(strings.TrimSpace(m.License.Name))
|
|
}
|
|
for _, e := range excluded {
|
|
switch {
|
|
case strings.EqualFold(e, key), strings.EqualFold(e, name):
|
|
return e
|
|
case e == ruleNonCommercial:
|
|
if m.License != nil && m.License.Commercial != nil && !*m.License.Commercial {
|
|
return e
|
|
}
|
|
case strings.HasPrefix(e, ruleRegionPrefix):
|
|
if m.License != nil && slices.Contains(m.License.ExcludedRegions, strings.TrimPrefix(e, ruleRegionPrefix)) {
|
|
return e
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// excludedByLicence says why the principal's person has left this model
|
|
// out, empty when they have not.
|
|
func (r *Router) excludedByLicence(p auth.Principal, requested string) string {
|
|
if len(p.Limits.ExcludedLicences) == 0 {
|
|
return ""
|
|
}
|
|
_, m := r.resolve(requested)
|
|
return licenceExcluded(p.Limits.ExcludedLicences, m)
|
|
}
|
|
|
|
// licenceChoices are what the profile page offers: each licence the
|
|
// catalog's models are under, with the models, and the regions any of them
|
|
// excludes.
|
|
func (r *Router) licenceChoices() web.LicenceChoices {
|
|
by := map[string]*web.LicenceChoice{}
|
|
regions := map[string]bool{}
|
|
nonCommercial := false
|
|
for name, m := range r.modelMap() {
|
|
if name != m.Name || r.hiddenModel(name) {
|
|
continue // an alias, or a guardrail's judge nobody asks for
|
|
}
|
|
key := licenceKey(m)
|
|
c, ok := by[key]
|
|
if !ok {
|
|
c = &web.LicenceChoice{Key: key}
|
|
if m.License != nil {
|
|
c.Name, c.URL = m.License.Name, m.License.URL
|
|
if cm := m.License.Commercial; cm != nil {
|
|
c.Commercial = map[bool]string{true: "yes", false: "no"}[*cm]
|
|
}
|
|
c.ExcludedRegions = m.License.ExcludedRegions
|
|
}
|
|
by[key] = c
|
|
}
|
|
c.Models = append(c.Models, m.Name)
|
|
if m.License != nil {
|
|
for _, reg := range m.License.ExcludedRegions {
|
|
regions[reg] = true
|
|
}
|
|
if m.License.Commercial != nil && !*m.License.Commercial {
|
|
nonCommercial = true
|
|
}
|
|
}
|
|
}
|
|
var out web.LicenceChoices
|
|
for _, c := range by {
|
|
sort.Strings(c.Models)
|
|
out.Licences = append(out.Licences, *c)
|
|
}
|
|
sort.Slice(out.Licences, func(i, j int) bool { return out.Licences[i].Key < out.Licences[j].Key })
|
|
for reg := range regions {
|
|
out.Regions = append(out.Regions, reg)
|
|
}
|
|
sort.Strings(out.Regions)
|
|
out.AnyNonCommercial = nonCommercial
|
|
return out
|
|
}
|
|
|
|
// writeForbidden refuses a model the principal may not use, saying why when
|
|
// it is the person's own choice of licences rather than the key's allowlist.
|
|
func (r *Router) writeForbidden(w http.ResponseWriter, p auth.Principal, model string) {
|
|
if why := r.excludedByLicence(p, model); why != "" {
|
|
writeErrorCode(w, http.StatusForbidden, "invalid_request_error", "licence_excluded",
|
|
fmt.Sprintf("the model %q is under a licence its account has chosen not to use (%s); change it on the profile page", model, why))
|
|
return
|
|
}
|
|
auth.WriteForbiddenModel(w, model)
|
|
}
|