diff --git a/internal/router/chat.go b/internal/router/chat.go index 97dfbd3..1a31b78 100644 --- a/internal/router/chat.go +++ b/internal/router/chat.go @@ -56,6 +56,7 @@ func (r *Router) registerChat(mux *http.ServeMux, pages *web.AccountPages) { func(w http.ResponseWriter, req *http.Request, _ store.User) { web.ReplyRenderer(r.cfg.MaxBodyBytes)(w, req) })) + r.registerImagesPage(mux, pages) } // chatSend dispatches one turn on behalf of a signed-in person. @@ -256,6 +257,12 @@ func (r *Router) principalFor(req *http.Request, user store.User, asked string) // by name, "big-model" came first and every visitor who pressed Send without // touching the picker got placeholder text. func (r *Router) modelsFor(req *http.Request, user store.User) []web.ChatModel { + return r.modelsOf(req, user, false) +} + +// modelsOf is the models a person may use from a page: those that make +// pictures, or those that write text. +func (r *Router) modelsOf(req *http.Request, user store.User, pictures bool) []web.ChatModel { principal, err := r.principalFor(req, user, req.URL.Query().Get("context")) if err != nil { return nil @@ -283,7 +290,7 @@ func (r *Router) modelsFor(req *http.Request, user store.User) []web.ChatModel { continue } seen[name] = struct{}{} - if r.permits(principal, name) { + if entry.MakesImages() == pictures && r.permits(principal, name) { out = append(out, web.ChatModel{Name: name, Simulated: entry.Simulated(), Hardware: hardware[name]}) } } @@ -299,7 +306,7 @@ func (r *Router) modelsFor(req *http.Request, user store.User) []web.ChatModel { continue } seen[name] = struct{}{} - if r.permits(principal, name) { + if entry.MakesImages() == pictures && r.permits(principal, name) { out = append(out, web.ChatModel{ Name: name, Simulated: entry.Simulated(), OnDemand: true, }) diff --git a/internal/router/images.go b/internal/router/images.go index 1f05de3..dab5bc4 100644 --- a/internal/router/images.go +++ b/internal/router/images.go @@ -1,20 +1,26 @@ package router import ( + "bytes" "context" "encoding/base64" "encoding/json" "errors" "fmt" + "io" "net/http" "strconv" "strings" "time" + "gitea.mynodes.xyz/ilker/llmgw/internal/auth" "gitea.mynodes.xyz/ilker/llmgw/internal/comfy" "gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog" "gitea.mynodes.xyz/ilker/llmgw/internal/router/endpoint" "gitea.mynodes.xyz/ilker/llmgw/internal/router/proxy" + "gitea.mynodes.xyz/ilker/llmgw/internal/store" + "gitea.mynodes.xyz/ilker/llmgw/internal/version" + "gitea.mynodes.xyz/ilker/llmgw/internal/web" ) // /v1/images/generations, OpenAI's shape, made by ComfyUI: the request goes @@ -199,3 +205,82 @@ func (r *Router) generateImages(ctx context.Context, w http.ResponseWriter, req func ctxCauseIsClient(ctx context.Context) bool { return !errors.Is(context.Cause(ctx), context.DeadlineExceeded) } + +// registerImagesPage mounts the page that makes pictures, beside the chat +// and for the same reasons: a model tried without a client, with the +// browser holding a session and never a key, and the request sent down the +// one path every API request takes. +func (r *Router) registerImagesPage(mux *http.ServeMux, pages *web.AccountPages) { + pages.SetImages(func() bool { + for _, m := range r.modelMap() { + if m.MakesImages() { + return true + } + } + return false + }) + mux.HandleFunc("GET /images", pages.Guard(func(w http.ResponseWriter, req *http.Request, user store.User) { + contexts, _ := r.chatContexts(req.Context(), user) + current, err := r.pickContext(req.Context(), user, req.URL.Query().Get("context")) + if err != nil { + current = "" + } + if current == "" && !r.personalOff { + current = personalContext + } + web.Images(user, r.modelsOf(req, user, true), contexts, current, version.Version)(w, req) + })) + mux.HandleFunc("POST /images/generate", pages.Guard(r.imagesSend)) +} + +// imagesSend makes pictures on behalf of a signed-in person. +func (r *Router) imagesSend(w http.ResponseWriter, req *http.Request, user store.User) { + body, err := io.ReadAll(io.LimitReader(req.Body, r.cfg.MaxBodyBytes)) + if err != nil { + http.Error(w, "could not read the request", http.StatusBadRequest) + return + } + var in struct { + Model string `json:"model"` + Prompt string `json:"prompt"` + Size string `json:"size"` + N int `json:"n"` + Seed *int64 `json:"seed"` + Steps int `json:"steps"` + NegativePrompt string `json:"negative_prompt"` + // Context is what the pictures are charged to, checked as the + // chat's is. + Context string `json:"context"` + } + if err := json.Unmarshal(body, &in); err != nil || in.Model == "" { + http.Error(w, "a model and a prompt are required", http.StatusBadRequest) + return + } + principal, err := r.principalFor(req, user, in.Context) + if err != nil { + http.Error(w, err.Error(), http.StatusForbidden) + return + } + // Rebuilt from the fields the page has, so it can set nothing else. + out := map[string]any{"model": in.Model, "prompt": in.Prompt, "size": in.Size, "n": in.N} + if in.Seed != nil { + out["seed"] = *in.Seed + } + if in.Steps > 0 { + out["steps"] = in.Steps + } + if s := strings.TrimSpace(in.NegativePrompt); s != "" { + out["negative_prompt"] = s + } + upstream, err := json.Marshal(out) + if err != nil { + http.Error(w, "could not build the request", http.StatusInternalServerError) + return + } + next := req.Clone(auth.WithPrincipal(req.Context(), principal)) + next.Body = io.NopCloser(bytes.NewReader(upstream)) + next.ContentLength = int64(len(upstream)) + next.Header.Set("Content-Type", "application/json") + next.Header.Del(knowledgeHeader) + r.handleInference(imagesPath)(w, next) +} diff --git a/internal/router/images_test.go b/internal/router/images_test.go index b6b90de..89bf4c3 100644 --- a/internal/router/images_test.go +++ b/internal/router/images_test.go @@ -227,3 +227,44 @@ func TestPicturesAreChargedByThePictureAndRecorded(t *testing.T) { time.Sleep(10 * time.Millisecond) } } + +func TestThePicturesPageOffersPictureModelsAndTheChatTheOthers(t *testing.T) { + e := newFakeEngine(t, "chat") + rt, _, _ := imageRouterWith(t, func(cfg *config.Server) { + cfg.Auth.Enabled = true + cfg.Auth.Store = filepath.Join(t.TempDir(), "store.db") + }) + rt.SetEndpoints(t.Context(), append(rt.reg.Infos(), endpoint.Info{ID: "c", URL: e.srv.URL, Models: []string{"chat"}})) + org, _ := rt.store.CreateOrg(t.Context(), "acme") + team, _ := rt.store.CreateTeam(t.Context(), org, "design") + project, err := rt.store.CreateProject(t.Context(), team, "posters") + if err != nil { + t.Fatal(err) + } + user := store.User{ID: "usr-1", ProjectID: project} + names := func(pictures bool) []string { + var out []string + for _, m := range rt.modelsOf(httptest.NewRequest("GET", "/", nil), user, pictures) { + out = append(out, m.Name) + } + return out + } + if got := names(false); len(got) != 1 || got[0] != "chat" { + t.Errorf("the chat offers %v", got) + } + if got := names(true); len(got) != 1 || got[0] != "flux" { + t.Errorf("the pictures page offers %v", got) + } + + // The page's request is rebuilt: a field it has no business sending + // does not reach the model. + body := `{"model":"flux","prompt":"a kite","size":"512x512","n":1,"seed":3,"response_format":"url"}` + rec := httptest.NewRecorder() + rt.imagesSend(rec, httptest.NewRequest("POST", "/images/generate", strings.NewReader(body)), user) + if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "b64_json") { + t.Fatalf("%d %s", rec.Code, rec.Body.String()) + } + if rec.Header().Get("x-llmgw-seed") != "3" { + t.Errorf("seed %q", rec.Header().Get("x-llmgw-seed")) + } +} diff --git a/internal/web/account.go b/internal/web/account.go index e421247..6a26a41 100644 --- a/internal/web/account.go +++ b/internal/web/account.go @@ -92,7 +92,10 @@ type AccountPages struct { // with it: the policies in force, whose judges the mirror page serves. hasGuard bool // hasSettings is set when /admin/settings is registered. - hasSettings bool + hasSettings bool + // images says whether some model makes pictures; nil when the + // pictures page is not mounted. + images func() bool guardLibrary func() guard.Config // guardFor names the guardrail policies in force for someone, and // whether they keep nothing; nil when there are none. diff --git a/internal/web/images.go b/internal/web/images.go new file mode 100644 index 0000000..84fe22b --- /dev/null +++ b/internal/web/images.go @@ -0,0 +1,37 @@ +package web + +import ( + "net/http" + "time" + + "gitea.mynodes.xyz/ilker/llmgw/internal/store" +) + +type imagesData struct { + Nav Nav + Contexts []ChatContext + Context string + User store.User + Models []ChatModel + CSRF string + Version string + Now string +} + +// Images renders the page that makes pictures, for one signed-in person. +// Its models are those that make them, as the chat's are those that write. +func Images(user store.User, models []ChatModel, contexts []ChatContext, current, version string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + noStore(w) + render(w, r, "images", imagesData{ + Nav: navFrom(r, "images"), + Contexts: contexts, Context: current, + User: user, Models: models, CSRF: csrfFor(r), + Version: version, Now: time.Now().UTC().Format(time.RFC3339), + }) + } +} + +// SetImages has the menu offer the pictures page while some model makes +// them. Asked on every page, so it must be cheap: the catalog, not a request. +func (p *AccountPages) SetImages(available func() bool) { p.images = available } diff --git a/internal/web/images_test.go b/internal/web/images_test.go new file mode 100644 index 0000000..fca0558 --- /dev/null +++ b/internal/web/images_test.go @@ -0,0 +1,36 @@ +package web + +import ( + "net/http/httptest" + "strings" + "testing" + + "gitea.mynodes.xyz/ilker/llmgw/internal/store" +) + +func TestThePicturesPageDrawsInEveryLanguage(t *testing.T) { + for _, lang := range []string{"en", "tr"} { + req := httptest.NewRequest("GET", "/images", nil) + req.Header.Set("Accept-Language", lang) + rec := httptest.NewRecorder() + Images(store.User{Email: "a@b.c"}, []ChatModel{{Name: "flux-schnell", Hardware: []string{"6000-24g"}}}, + []ChatContext{{Value: "personal", Label: "Personal"}}, "personal", "v0")(rec, req) + if rec.Code != 200 { + t.Fatalf("%s: %d %s", lang, rec.Code, rec.Body.String()) + } + page := rec.Body.String() + for _, want := range []string{`