draw: SenseNova's sensenova-u1.5-lite is an image model, and draws on its images API

lileo0668 (Discord, #help) added SenseNova's API (token.sensenova.cn/v1)
as a provider. sensenova-u1.5-lite showed in its model list but not in
Settings' image generation picker, so magpie's generate_image couldn't
use it.

SenseTime's launch post for U1.5 Lite on the Token Plan says it is an
image model only (no chat), served at /v1/images/generations and
/v1/images/edits in OpenAI's shape; an edit is JSON with
"images": [{"image_url": "<https URL or data URL>"}], and
response_format is url (a 24h link) or b64_json.

- catalog.DrawsID and ImagesAPI know "sensenova-u1" (u1.5-lite,
  u1.5-fast), so the model a vendor's /models lists is a drawer, listed
  in the picker and asked on the images API, not chat.
- drawImages, for those models only: ask for b64_json, and send an edit
  as JSON with the images as [{"image_url": data URL}], the shape
  ChatGPT's backend takes, instead of the multipart form other OpenAI
  images APIs get. No other vendor's request changes.

Test: TestSenseNovaU1Draws (internal/gateway) fetches a fake SenseNova
/models (a chat and the U1 model), checks Drawers lists the U1 model
alone, draws it (images API, b64 back, chat never asked) and edits it
(JSON images). Broken by hand: without "sensenova-u1" in DrawsID it is
red (drawers []); with senseNovaDrawer returning false it is red (the
fake answers a link, image ""), and the edit would go multipart.

Run at this head: gofmt -l (clean), go vet ./..., GOOS=linux go vet
-tags nogui ./..., GOOS=windows go vet ./..., go build, GOOS=linux go
build -tags nogui, GOOS=windows go build, and
go test -tags nogui -timeout 45m ./internal/catalog ./internal/gateway
./internal/provider ./internal/imagemcp ./internal/gui: catalog, gateway
(566s), imagemcp and gui ok; provider crashes in
TestUnreadLoginsWriteErrorsKeepCredentials (synctest channel closed
from outside its bubble in filememo.Forget), which is #3632, open and
red on main since 091153bf, and not touched here. After the rebase onto
origin/main 8b0132b3d: gofmt, vet (three OSes) and the catalog tests
plus TestSenseNovaU1Draws and the draw tests (-run 'Draw|Image|Edit')
again.

Not tried: a real SenseNova key (none here); its exact /models bytes and
the full answer JSON are from its docs, not a capture. Not run: the full
./... suite (load ~90) and the GUI Playwright suite (no GUI asset
changed). A mask on an edit is passed as in Codex's shape; SenseNova's
docs don't name a mask.

Reported by lileo0668 (Discord).
This commit is contained in:
yetone
2026-10-27 16:01:10 +08:00
parent 8ff90bee3d
commit ab546a4eea
3 changed files with 141 additions and 6 deletions
+4 -3
View File
@@ -1555,10 +1555,11 @@ func draws(m mdModel) bool {
// DrawsID is whether a model's id names one that makes images, for a model
// the catalog doesn't know (one typed in, or a vendor's own list's). Banana
// is the Nano Banana the Gemini image models are called on a relay, whatever
// its spelling there (#2071).
// its spelling there (#2071). SenseNova's U1 models (sensenova-u1.5-lite,
// -fast) draw only, on its images API (lileo0668 on Discord).
func DrawsID(id string) bool {
id = strings.ToLower(id)
for _, w := range []string{"image", "imagen", "imagine", "dall-e", "flux", "seedream", "cogview", "stable-diffusion", "sdxl", "wanx", "kolors", "hidream", "banana"} {
for _, w := range []string{"image", "imagen", "imagine", "dall-e", "flux", "seedream", "cogview", "stable-diffusion", "sdxl", "wanx", "kolors", "hidream", "banana", "sensenova-u1"} {
if strings.Contains(id, w) {
return true
}
@@ -1571,7 +1572,7 @@ func DrawsID(id string) bool {
// gemini-*-image or gpt-5-image, which chat.
func ImagesAPI(id string) bool {
id = strings.ToLower(id)
for _, w := range []string{"gpt-image", "chatgpt-image", "dall-e", "imagen", "imagine", "qwen-image", "wanx", "wan2", "seedream", "cogview", "flux", "stable-diffusion", "sdxl", "kolors", "hidream"} {
for _, w := range []string{"gpt-image", "chatgpt-image", "dall-e", "imagen", "imagine", "qwen-image", "wanx", "wan2", "seedream", "cogview", "flux", "stable-diffusion", "sdxl", "kolors", "hidream", "sensenova-u1"} {
if strings.Contains(id, w) {
return true
}
+17 -3
View File
@@ -1241,14 +1241,19 @@ func (s *Server) drawImages(ctx context.Context, p provider.Provider, model stri
}
}
// only dall-e gives a URL unless asked; gpt-image turns the field away
if strings.Contains(m, "dall-e") {
if strings.Contains(m, "dall-e") || senseNovaDrawer(m) {
req["response_format"] = "b64_json"
}
body, _ = json.Marshal(req)
ct, url = "application/json", base+"/images/generations"
} else if drawsCodex(p) {
// ChatGPT's backend takes an edit only as JSON, each image a data URL
} else if drawsCodex(p) || senseNovaDrawer(m) {
// ChatGPT's backend takes an edit only as JSON, each image a data URL;
// so does SenseNova's for its U1 models, which answer in a URL unless
// asked for base64
req := map[string]any{"model": model, "prompt": d.Prompt, "n": d.N}
if senseNovaDrawer(m) {
req["response_format"] = "b64_json"
}
for k, v := range map[string]string{"size": d.Size, "quality": d.Quality, "background": d.Background, "output_format": d.Format} {
if v != "" {
req[k] = v
@@ -1311,6 +1316,15 @@ func (s *Server) drawImages(ctx context.Context, p provider.Provider, model stri
return readImagesAnswer(p, url, b, code)
}
// senseNovaDrawer is whether model is one of SenseNova's U1 image models
// (sensenova-u1.5-lite, sensenova-u1.5-fast), whose /images/edits takes
// JSON alone, the images as [{"image_url": "data:…"}], and whose answer
// is a link for 24 hours unless response_format asks for b64_json
// (lileo0668 on Discord).
func senseNovaDrawer(model string) bool {
return strings.Contains(strings.ToLower(model), "sensenova-u1")
}
// drawOpenRouterImages asks OpenRouter's dedicated Images API, which accepts
// reference images as input_references rather than OpenAI's edits multipart.
func (s *Server) drawOpenRouterImages(ctx context.Context, p provider.Provider, model string, d drawing) (drawn, int, error) {
+120
View File
@@ -0,0 +1,120 @@
package gateway
import (
"context"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"slices"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// senseNova is token.sensenova.cn as its docs describe it: an OpenAI
// model list, /images/generations in JSON, /images/edits in JSON alone
// with the images as [{"image_url": "data:…"}] (a multipart form is
// turned away), and a link unless b64_json is asked for.
type senseNova struct {
mu sync.Mutex
sent map[string][]string
}
func (v *senseNova) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
v.mu.Lock()
if v.sent == nil {
v.sent = map[string][]string{}
}
v.sent[r.URL.Path] = append(v.sent[r.URL.Path], string(body))
v.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/v1/models":
io.WriteString(w, `{"object":"list","data":[{"id":"sensenova-6.8-flash-lite","object":"model"},{"id":"sensenova-u1.5-lite","object":"model"}]}`)
case "/v1/images/generations", "/v1/images/edits":
var req struct {
Format string `json:"response_format"`
Images []struct {
URL string `json:"image_url"`
} `json:"images"`
}
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") || json.Unmarshal(body, &req) != nil {
w.WriteHeader(400)
io.WriteString(w, `{"error":{"message":"invalid request body"}}`)
return
}
if r.URL.Path == "/v1/images/edits" && (len(req.Images) == 0 || !strings.HasPrefix(req.Images[0].URL, "data:image/")) {
w.WriteHeader(400)
io.WriteString(w, `{"error":{"message":"images is required"}}`)
return
}
if req.Format != "b64_json" {
io.WriteString(w, `{"created":1,"data":[{"url":"https://sensenova.invalid/out.png"}]}`)
return
}
io.WriteString(w, `{"created":1,"data":[{"b64_json":"`+base64.StdEncoding.EncodeToString(pngBytes)+`"}]}`)
default:
w.WriteHeader(404)
}
}
func (v *senseNova) got(path string) []string {
v.mu.Lock()
defer v.mu.Unlock()
return append([]string(nil), v.sent[path]...)
}
// SenseNova's sensenova-u1.5-lite, listed by its own /models beside its
// chat models, is an image model: Settings' image generation picker lists
// it, and it draws and edits on SenseNova's images API in the shape that
// API takes, with no setting of the user's (lileo0668 on Discord).
func TestSenseNovaU1Draws(t *testing.T) {
fresh(t)
v := &senseNova{}
up := httptest.NewServer(v)
t.Cleanup(up.Close)
base := up.URL + "/v1"
ms, err := catalog.Fetch(context.Background(), base, "key", false, nil)
if err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("sensenova", base, ms); err != nil {
t.Fatal(err)
}
p := provider.Provider{ID: "sensenova", Name: "SenseNova", Chat: base, Key: "key"}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
var drawers []string
for _, m := range Drawers(p) {
drawers = append(drawers, m.ID)
}
if !slices.Equal(drawers, []string{"sensenova-u1.5-lite"}) {
t.Fatalf("drawers %v, want sensenova-u1.5-lite alone", drawers)
}
s := New()
code, a, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"sensenova/sensenova-u1.5-lite","prompt":"a magpie","size":"1024x1024"}`)
if code != 200 || len(a.Data) != 1 {
t.Fatalf("%d %s", code, raw)
}
if b, _ := base64.StdEncoding.DecodeString(a.Data[0].B64); string(b) != string(pngBytes) {
t.Fatalf("image %q", b)
}
if sent := v.got("/v1/chat/completions"); len(sent) != 0 {
t.Fatalf("an image model was asked on chat: %v", sent)
}
img := "data:image/png;base64," + base64.StdEncoding.EncodeToString(pngBytes)
code, a, raw = postImages(t, s, "/v1/images/edits", "application/json", `{"model":"sensenova/sensenova-u1.5-lite","prompt":"bluer","images":[{"image_url":"`+img+`"}]}`)
if code != 200 || len(a.Data) != 1 {
t.Fatalf("edit: %d %s", code, raw)
}
if sent := v.got("/v1/images/edits"); len(sent) != 1 || !strings.Contains(sent[0], `"images":[{"image_url":"data:image/png;base64,`) {
t.Fatalf("edit was sent %v", sent)
}
}