CI / test (push) Successful in 11m28s
A game sold in the EU cannot ship what a licence excluding the EU made. On the profile page a person leaves out licences one by one, or by rule: every licence forbidding commercial use, every licence granting nothing in a region. The rules cover models added later. The choice reaches every principal through the resolved limits, so permits() leaves such models out of /v1/models, requests, jobs, fallbacks and the portal's pickers, and a request for one is refused 403 licence_excluded saying which choice did. Kept in user_excluded_licences (migration 0046), copied with the store and gone with the person. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
228 lines
7.4 KiB
Go
228 lines
7.4 KiB
Go
package router
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
|
)
|
|
|
|
// Jobs: a request that takes minutes, answered with an id and asked after.
|
|
//
|
|
// A 3D model from a picture takes minutes on a card that first has to be
|
|
// emptied of whatever else was on it. That is not a request to hold a socket
|
|
// open for (activate.go says why), so it is a job: queued with 202, its
|
|
// status and progress read back, its result downloaded as a file. The
|
|
// request is kept as the client sent it and replayed, by whichever gateway
|
|
// holds the job's lease, through the same path a client's request takes —
|
|
// key, allowlist, limits, admission, guardrails, accounting — so a job is
|
|
// charged and judged like any request, tagged job=<id>.
|
|
|
|
// meshesPath is where 3D models are asked for. The synchronous path behind
|
|
// it is served to the job worker only; clients get a job.
|
|
const meshesPath = "/v1/3d/generations"
|
|
|
|
const (
|
|
purposeJobOutput = "job_output"
|
|
// jobWindow is how long a job may take, queue included, before it is
|
|
// given up on.
|
|
jobWindow = 24 * time.Hour
|
|
)
|
|
|
|
func (r *Router) jobJSON(ctx context.Context, j store.Job) map[string]any {
|
|
out := map[string]any{
|
|
"id": j.ID, "object": "job", "endpoint": j.Endpoint, "model": j.Model, "status": j.Status,
|
|
"progress": nil, "output_file_id": strOrNil(j.OutputFileID), "content_type": strOrNil(j.ContentType),
|
|
"error": nil, "created_at": j.CreatedAt.Unix(), "started_at": unixOrNil(j.StartedAt),
|
|
"finished_at": unixOrNil(j.FinishedAt), "expires_at": unixOrNil(j.ExpiresAt), "usage": nil,
|
|
}
|
|
if j.Progress != "" && json.Valid([]byte(j.Progress)) {
|
|
out["progress"] = json.RawMessage(j.Progress)
|
|
}
|
|
if j.Error != "" {
|
|
out["error"] = map[string]string{"message": j.Error}
|
|
}
|
|
// The terms what it makes comes under, beside the result: whoever ships
|
|
// the mesh answers for them.
|
|
if m, ok := r.modelMap()[j.Model]; ok && m.License != nil {
|
|
out["license"] = m.License
|
|
}
|
|
// What the work cost, from the usage record the replayed request left
|
|
// under the job's id.
|
|
if store.JobDone(j.Status) && r.store != nil {
|
|
if u, ok, err := r.store.UsageByRequest(ctx, j.ID); err == nil && ok {
|
|
out["usage"] = map[string]int64{"prompt_tokens": u.PromptTokens, "completion_tokens": u.CompletionTokens,
|
|
"images": u.Images, "meshes": u.Meshes}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// handleCreateJob queues a request for a 3D model.
|
|
func (r *Router) handleCreateJob(w http.ResponseWriter, req *http.Request) {
|
|
r.createJobAt(meshesPath)(w, req)
|
|
}
|
|
|
|
// createJobAt queues a request at path: a 3D model, a workflow run. Refused
|
|
// now: no key, a model that is not there or not allowed, a request the model
|
|
// cannot serve, a file it names that is not the caller's. Refused when it is
|
|
// served, and said as the job's error: limits, the guardrails, a model that
|
|
// will not come up.
|
|
func (r *Router) createJobAt(path string) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, req *http.Request) { r.createJob(w, req, path) }
|
|
}
|
|
|
|
func (r *Router) createJob(w http.ResponseWriter, req *http.Request, path string) {
|
|
p, ok := r.batchPrincipal(w, req)
|
|
if !ok {
|
|
return
|
|
}
|
|
body, err := io.ReadAll(io.LimitReader(req.Body, r.cfg.MaxBodyBytes))
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_request_error", "could not read the request: "+err.Error())
|
|
return
|
|
}
|
|
var in struct {
|
|
Model string `json:"model"`
|
|
}
|
|
if err := json.Unmarshal(body, &in); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_request_error", "the request is not valid JSON: "+err.Error())
|
|
return
|
|
}
|
|
if in.Model == "" {
|
|
writeError(w, http.StatusBadRequest, "invalid_request_error", "model is required")
|
|
return
|
|
}
|
|
if _, known := r.modelMap()[in.Model]; !known || r.hiddenModel(in.Model) {
|
|
writeErrorCode(w, http.StatusNotFound, "invalid_request_error", "model_not_found",
|
|
fmt.Sprintf("the model %q does not exist", in.Model))
|
|
return
|
|
}
|
|
if !r.permits(p, in.Model) {
|
|
r.writeForbidden(w, p, in.Model)
|
|
return
|
|
}
|
|
if why := r.imageRouteRefusal(path, in.Model, body); why != "" {
|
|
writeError(w, http.StatusBadRequest, "invalid_request_error", why)
|
|
return
|
|
}
|
|
if path == workflowsPath {
|
|
if m, known := r.modelMap()[in.Model]; known {
|
|
run, _ := parseRunRequest(m, body)
|
|
if err := r.checkRunFiles(req.Context(), ownerOf(p), run); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_request_error", err.Error())
|
|
return
|
|
}
|
|
}
|
|
}
|
|
now := time.Now()
|
|
j := store.Job{ID: newID("job_"), Owner: ownerOf(p), KeyID: p.KeyID, Endpoint: path, Model: in.Model,
|
|
Input: body, Status: store.JobQueued, CreatedAt: now, ExpiresAt: now.Add(jobWindow)}
|
|
if err := r.store.PutJob(req.Context(), j); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "server_error", err.Error())
|
|
return
|
|
}
|
|
r.wakeJobs()
|
|
writeJSON(w, http.StatusAccepted, r.jobJSON(req.Context(), j))
|
|
}
|
|
|
|
// ownedJob is a job the caller owns, or a 404 said.
|
|
func (r *Router) ownedJob(w http.ResponseWriter, req *http.Request, owner string) (store.Job, bool) {
|
|
j, err := r.store.JobByID(req.Context(), req.PathValue("id"))
|
|
if errors.Is(err, store.ErrNotFound) || err == nil && j.Owner != owner {
|
|
writeErrorCode(w, http.StatusNotFound, "invalid_request_error", "job_not_found",
|
|
fmt.Sprintf("no job %q", req.PathValue("id")))
|
|
return store.Job{}, false
|
|
}
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "server_error", err.Error())
|
|
return store.Job{}, false
|
|
}
|
|
return j, true
|
|
}
|
|
|
|
func (r *Router) handleGetJob(w http.ResponseWriter, req *http.Request) {
|
|
p, ok := r.batchPrincipal(w, req)
|
|
if !ok {
|
|
return
|
|
}
|
|
if j, ok := r.ownedJob(w, req, ownerOf(p)); ok {
|
|
writeJSON(w, http.StatusOK, r.jobJSON(req.Context(), j))
|
|
}
|
|
}
|
|
|
|
func (r *Router) handleCancelJob(w http.ResponseWriter, req *http.Request) {
|
|
p, ok := r.batchPrincipal(w, req)
|
|
if !ok {
|
|
return
|
|
}
|
|
j, ok := r.ownedJob(w, req, ownerOf(p))
|
|
if !ok {
|
|
return
|
|
}
|
|
j, err := r.store.CancelJob(req.Context(), j.ID, time.Now())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "server_error", err.Error())
|
|
return
|
|
}
|
|
r.wakeJobs()
|
|
writeJSON(w, http.StatusOK, r.jobJSON(req.Context(), j))
|
|
}
|
|
|
|
func (r *Router) handleListJobs(w http.ResponseWriter, req *http.Request) {
|
|
p, ok := r.batchPrincipal(w, req)
|
|
if !ok {
|
|
return
|
|
}
|
|
limit, _ := strconv.Atoi(req.URL.Query().Get("limit"))
|
|
if limit <= 0 || limit > 100 {
|
|
limit = 20
|
|
}
|
|
js, err := r.store.Jobs(req.Context(), ownerOf(p), req.URL.Query().Get("after"), limit+1)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "server_error", err.Error())
|
|
return
|
|
}
|
|
more := len(js) > limit
|
|
if more {
|
|
js = js[:limit]
|
|
}
|
|
data := make([]map[string]any, 0, len(js))
|
|
for _, j := range js {
|
|
data = append(data, r.jobJSON(req.Context(), j))
|
|
}
|
|
out := map[string]any{"object": "list", "data": data, "has_more": more, "first_id": nil, "last_id": nil}
|
|
if len(js) > 0 {
|
|
out["first_id"], out["last_id"] = js[0].ID, js[len(js)-1].ID
|
|
}
|
|
writeJSON(w, http.StatusOK, out)
|
|
}
|
|
|
|
// errorMessage is what an answer said went wrong: the message of an OpenAI
|
|
// error body, or the body itself, kept short.
|
|
func errorMessage(body []byte) string {
|
|
var e struct {
|
|
Error struct {
|
|
Message string `json:"message"`
|
|
} `json:"error"`
|
|
}
|
|
if json.Unmarshal(body, &e) == nil && e.Error.Message != "" {
|
|
return e.Error.Message
|
|
}
|
|
s := strings.TrimSpace(string(body))
|
|
if len(s) > 300 {
|
|
s = s[:300] + "…"
|
|
}
|
|
if s == "" {
|
|
return "no reason given"
|
|
}
|
|
return s
|
|
}
|