G3, the part that needs no model: a ComfyUI model's comfyui.output (or external.output) says pictures or meshes. A request for a 3D model — a job at /v1/3d/generations — carries the picture as base64; at the replica it is uploaded to ComfyUI's input directory (the gate lets /upload/image through), the workflow is filled in with its name, the seed and the resolution, and the mesh the workflow saves comes back with its type, which is the file the job keeps. comfy.Client reads every list of files an output names, not "images" only, and types a file by its extension where ComfyUI sends octet-stream. A model's license (name, url, notice) is said in /v1/models and in the job, and written into a GLB's asset.extras, since the file travels without the API; a mesh it cannot be written into is refused. per_mesh in auth.pricing, usage.meshes in the rows, the sums and the export (migration 0045). A picture asked of a mesh model, or a mesh of a picture model, is refused saying which it makes. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
210 lines
6.8 KiB
Go
210 lines
6.8 KiB
Go
package router
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/auth"
|
|
"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. Refused now: no key, a
|
|
// model that is not there or not allowed, a request the model cannot serve.
|
|
// 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) handleCreateJob(w http.ResponseWriter, req *http.Request) {
|
|
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) {
|
|
auth.WriteForbiddenModel(w, in.Model)
|
|
return
|
|
}
|
|
if why := r.imageRouteRefusal(meshesPath, in.Model, body); why != "" {
|
|
writeError(w, http.StatusBadRequest, "invalid_request_error", why)
|
|
return
|
|
}
|
|
now := time.Now()
|
|
j := store.Job{ID: newID("job_"), Owner: ownerOf(p), KeyID: p.KeyID, Endpoint: meshesPath, 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
|
|
}
|