The game's bridge found three things on the PoC. /mcp held its body to 64 KiB — the edit meant for it had raised the memory notes' limit instead — so a 1024 px concept sent as base64 failed as "expected one JSON-RPC request object". The notes are back to 64 KiB, /mcp takes 64 MB, and a call past it is a 413 saying so. /v1/files took batch files only, so a picture could not be sent once and named after. purpose vision and user_data are kept too, as long as job results, and generate_mesh hands a workflow model the id as it is. generate_image and review_images failed at once while their model was cold. They wait out an answer that says when to come back — loading, another model holding the GPU, a full queue — for as long as the call may, saying who holds the GPU and about how long; a refusal that says no Retry-After, a spent budget, is said at once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
224 lines
6.9 KiB
Go
224 lines
6.9 KiB
Go
package router
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"slices"
|
|
"time"
|
|
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/auth"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/guard"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
|
)
|
|
|
|
// The Files API, as far as batches need it: a client uploads a file of
|
|
// requests, and reads back the files a batch writes.
|
|
|
|
const (
|
|
purposeBatch = "batch"
|
|
purposeBatchOutput = "batch_output"
|
|
// purposeVision and purposeUserData are files a request works from — a
|
|
// picture to make a mesh of or to look at, a workflow's input — named
|
|
// by id rather than sent as base64 in every call. OpenAI's names.
|
|
purposeVision = "vision"
|
|
purposeUserData = "user_data"
|
|
)
|
|
|
|
// uploadPurposes are what a client may upload a file for.
|
|
var uploadPurposes = []string{purposeBatch, purposeVision, purposeUserData}
|
|
|
|
func newID(prefix string) string {
|
|
var b [12]byte
|
|
_, _ = rand.Read(b[:])
|
|
return prefix + hex.EncodeToString(b[:])
|
|
}
|
|
|
|
// ownerOf is who a file or a batch belongs to: the key's project, so every
|
|
// key of it sees them, or the person for a personal key.
|
|
func ownerOf(p auth.Principal) string {
|
|
switch {
|
|
case p.ProjectID != "":
|
|
return "project:" + p.ProjectID
|
|
case p.UserID != "":
|
|
return "user:" + p.UserID
|
|
}
|
|
return "key:" + p.KeyID
|
|
}
|
|
|
|
// batchPrincipal is the caller, or a refusal when there is no store to keep
|
|
// files and batches in.
|
|
func (r *Router) batchPrincipal(w http.ResponseWriter, req *http.Request) (auth.Principal, bool) {
|
|
p, ok := auth.FromContext(req.Context())
|
|
if !ok || r.store == nil {
|
|
writeError(w, http.StatusNotImplemented, "invalid_request_error",
|
|
"files and batches need authentication and the store, which this gateway runs without")
|
|
return auth.Principal{}, false
|
|
}
|
|
// A batch's files are prompts and answers kept: not under a policy
|
|
// that keeps nothing.
|
|
if r.noStore(guard.Subject{KeyID: p.KeyID, UserID: p.UserID, ProjectID: p.ProjectID,
|
|
TeamID: p.TeamID, OrgID: p.OrgID, Path: req.URL.Path}) {
|
|
writeError(w, http.StatusForbidden, "invalid_request_error",
|
|
"the gateway's policy for this caller keeps nothing, and a batch is prompts and answers kept")
|
|
return auth.Principal{}, false
|
|
}
|
|
return p, true
|
|
}
|
|
|
|
func (r *Router) batchCfg() (maxBytes int64, maxLines, concurrency, retainDays int) {
|
|
b := r.cfg.Batch
|
|
maxBytes, maxLines, concurrency, retainDays = b.MaxFileBytes, b.MaxLines, b.Concurrency, b.RetainDays
|
|
if maxBytes <= 0 {
|
|
maxBytes = 100 << 20
|
|
}
|
|
if maxLines <= 0 {
|
|
maxLines = 50000
|
|
}
|
|
if concurrency <= 0 {
|
|
concurrency = 4
|
|
}
|
|
if retainDays <= 0 {
|
|
retainDays = 30
|
|
}
|
|
return
|
|
}
|
|
|
|
func fileJSON(f store.File) map[string]any {
|
|
out := map[string]any{"id": f.ID, "object": "file", "bytes": f.Bytes, "created_at": f.CreatedAt.Unix(),
|
|
"filename": f.Filename, "purpose": f.Purpose, "status": "processed"}
|
|
if !f.ExpiresAt.IsZero() {
|
|
out["expires_at"] = f.ExpiresAt.Unix()
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (r *Router) handleUploadFile(w http.ResponseWriter, req *http.Request) {
|
|
p, ok := r.batchPrincipal(w, req)
|
|
if !ok {
|
|
return
|
|
}
|
|
maxBytes, _, _, _ := r.batchCfg()
|
|
req.Body = http.MaxBytesReader(w, req.Body, maxBytes+1<<20)
|
|
if err := req.ParseMultipartForm(32 << 20); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_request_error",
|
|
fmt.Sprintf("a multipart form with purpose and file, at most %d MB: %v", maxBytes>>20, err))
|
|
return
|
|
}
|
|
purpose := req.FormValue("purpose")
|
|
if !slices.Contains(uploadPurposes, purpose) {
|
|
writeError(w, http.StatusBadRequest, "invalid_request_error",
|
|
fmt.Sprintf("purpose %q: this gateway keeps files for batches (batch) and for requests to work from "+
|
|
"(vision, user_data)", purpose))
|
|
return
|
|
}
|
|
file, header, err := req.FormFile("file")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_request_error", "no file in the form: "+err.Error())
|
|
return
|
|
}
|
|
defer file.Close()
|
|
content, err := io.ReadAll(io.LimitReader(file, maxBytes+1))
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid_request_error", err.Error())
|
|
return
|
|
}
|
|
if int64(len(content)) > maxBytes {
|
|
writeError(w, http.StatusRequestEntityTooLarge, "invalid_request_error",
|
|
fmt.Sprintf("the file is larger than %d MB", maxBytes>>20))
|
|
return
|
|
}
|
|
f := store.File{ID: newID("file-"), Owner: ownerOf(p), Purpose: purpose,
|
|
Filename: header.Filename, Bytes: int64(len(content)), CreatedAt: time.Now()}
|
|
// A file to work from is kept as long as what is made from it.
|
|
if purpose != purposeBatch {
|
|
_, _, _, days := r.batchCfg()
|
|
f.ExpiresAt = f.CreatedAt.AddDate(0, 0, days)
|
|
}
|
|
if err := r.store.PutFile(req.Context(), f, content); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "server_error", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, fileJSON(f))
|
|
}
|
|
|
|
// ownedFile is a file the caller owns, or a 404 said.
|
|
func (r *Router) ownedFile(w http.ResponseWriter, req *http.Request, p auth.Principal) (store.File, bool) {
|
|
f, err := r.store.FileByID(req.Context(), req.PathValue("id"))
|
|
if errors.Is(err, store.ErrNotFound) || err == nil && f.Owner != ownerOf(p) {
|
|
writeErrorCode(w, http.StatusNotFound, "invalid_request_error", "file_not_found",
|
|
fmt.Sprintf("no file %q", req.PathValue("id")))
|
|
return store.File{}, false
|
|
}
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "server_error", err.Error())
|
|
return store.File{}, false
|
|
}
|
|
return f, true
|
|
}
|
|
|
|
func (r *Router) handleListFiles(w http.ResponseWriter, req *http.Request) {
|
|
p, ok := r.batchPrincipal(w, req)
|
|
if !ok {
|
|
return
|
|
}
|
|
fs, err := r.store.Files(req.Context(), ownerOf(p), req.URL.Query().Get("purpose"), 0)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "server_error", err.Error())
|
|
return
|
|
}
|
|
data := make([]map[string]any, 0, len(fs))
|
|
for _, f := range fs {
|
|
data = append(data, fileJSON(f))
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"object": "list", "data": data})
|
|
}
|
|
|
|
func (r *Router) handleGetFile(w http.ResponseWriter, req *http.Request) {
|
|
p, ok := r.batchPrincipal(w, req)
|
|
if !ok {
|
|
return
|
|
}
|
|
if f, ok := r.ownedFile(w, req, p); ok {
|
|
writeJSON(w, http.StatusOK, fileJSON(f))
|
|
}
|
|
}
|
|
|
|
func (r *Router) handleFileContent(w http.ResponseWriter, req *http.Request) {
|
|
p, ok := r.batchPrincipal(w, req)
|
|
if !ok {
|
|
return
|
|
}
|
|
f, ok := r.ownedFile(w, req, p)
|
|
if !ok {
|
|
return
|
|
}
|
|
b, err := r.store.FileContent(req.Context(), f.ID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "server_error", err.Error())
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", f.Filename))
|
|
_, _ = w.Write(b)
|
|
}
|
|
|
|
func (r *Router) handleDeleteFile(w http.ResponseWriter, req *http.Request) {
|
|
p, ok := r.batchPrincipal(w, req)
|
|
if !ok {
|
|
return
|
|
}
|
|
f, ok := r.ownedFile(w, req, p)
|
|
if !ok {
|
|
return
|
|
}
|
|
if _, err := r.store.DeleteFile(req.Context(), f.ID); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "server_error", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"id": f.ID, "object": "file", "deleted": true})
|
|
}
|