neighbads on X: a slow image endpoint needed more than the fixed 5 minutes. Settings > Images > Image generation timeout (imageTimeout in settings.json, 5 when unset, 1 to 60 accepted, anything else a 400) now gives every drawing that long, on each route it takes: - drawTimeout() reads the setting; drawWithin marks the context as a drawing and sets its deadline. Error texts say the time it was given (givenTo), ModelScope's task polling too. - A marked drawing goes through a vendor transport with no ResponseHeaderTimeout (drawsApart), since an images API sends its headers only once it has drawn; everything else keeps the 10-minute upstreamHeaderWait, so a 30-minute setting isn't cut at 10. - An image answered as a link is downloaded within the same time (imageLongest, never under ImageLongest). - Videos keep 5 minutes per call (videoTimeout): a video is made between its calls, so the setting isn't theirs. - magpie mcp image waits on a call up to callLongest (2h1m, past the 60-minute bound twice over), since it may not read the settings (WSL); ToolTimeout, which agent configs carry, is unchanged. - The plugin path: Bun 1.4.2's fetch has no 5-minute cap (tried against a server answering after 330s), so nothing changes there. - api.go keeps the saved value when a page doesn't send imageTimeout; prefsKeep carries it on every other Settings save. GUI: a segmented row under Image generation (5, 10, 15, 30, 60 min; a value set by hand gets its own segment), in en, zh, zh-TW, ja and de. Three GUI tests clicked controls the new row pushed under the window's foot or past the viewport; they now wheel to them with reader.cjs's reach (disabled-models, search-api, refusal-retries-setting). searcher-pick checks the help has no title before reach's pointer passes over it (the app's mouseover sets one on cut-short text). Run, at224ea7d5bplus this change (origin/main9fd80b212fetched): - gofmt -l: clean. go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean. GOOS=linux go build -tags nogui and GOOS=windows go build: ok. - go test -p 4 -timeout 45m ./...: 64 packages ok, exit 0. - New tests: TestLongImageTimeoutLetsASlowVendorFinish (drawUnit and upstreamHeaderWait shrunk, no sleeps: a vendor slower than the header wait succeeds when given 30, is a 504 at the default), TestImageTimeoutSettingReachesTheVendor, TestImageDownloadGetsTheSetMinutes, TestOnlyDrawingsWaitPastTheHeaderWait, TestImageTimeoutKeptAndBounded, TestCallOutwaitsTheLongestDrawing, TestSettingsImageTimeout, image-timeout-setting.test.cjs (5 languages x 1100/420 x chromium and webkit, plus a hand-set 45 min kept across another save). - Broken by hand, each red, then restored: drawsApart sending everything through the header-timeout transport (2 red); drawTimeout fixed at 5 minutes (2 red); callLongest back to ToolTimeout (red at 11m); the settings bound at 600 ("61 minutes accepted"); imageTimeout left out of prefsKeep (hand-set test red, wanted 45); api.go's keep removed ("imageTimeout 0"). - GUI, NODE_PATH at playwright 1.62.0, chromium and webkit: the 115 files that reach Settings or the changed assets, 1646 tests, 1627 pass. Of the 18 reds: codex-predictions (420, every language) and s3-sync are red on 224ea7d5b's own copy too, untouched here; refusal-retries, disabled-models, search-api and searcher-pick were the clicks fixed above; sidebar's "the list opens and folds as it plays" (chromium) samples getAnimations() once and missed the unroll under a load of ~175. Rerun of those five files plus image-timeout-setting: 104 of 104 pass. Not tried: a real slow image vendor; WSL; the Windows box; Bun other than 1.4.2; the GUI files that can't reach Settings.
161 lines
5.7 KiB
Go
161 lines
5.7 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// slowDrawer is a vendor whose images API answers after wait, as one does
|
|
// only once it has drawn, with Settings' Image generation timeout set to
|
|
// minutes (0 for the default).
|
|
func slowDrawer(t *testing.T, minutes int, wait time.Duration) *Server {
|
|
t.Helper()
|
|
fresh(t)
|
|
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"} {
|
|
t.Setenv(k, "")
|
|
}
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
select {
|
|
case <-r.Context().Done():
|
|
return
|
|
case <-time.After(wait):
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`{"created":1,"data":[{"b64_json":"` + base64.StdEncoding.EncodeToString(pngBytes) + `"}]}`))
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: "slow", Name: "Slow", Chat: up.URL + "/v1", Key: "key", Models: []string{"gpt-image-1"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := catalog.SaveLive("slow", up.URL+"/v1", []catalog.Model{{ID: "gpt-image-1"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if minutes > 0 {
|
|
s := settings.Load()
|
|
s.ImageTimeout = minutes
|
|
if err := settings.Save(s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return New()
|
|
}
|
|
|
|
// deadlines records the deadline each request to a vendor goes out with,
|
|
// and whether it went as a drawing, on its way to the real transport.
|
|
type deadlines struct {
|
|
next http.RoundTripper
|
|
mu sync.Mutex
|
|
got []time.Duration
|
|
draw []bool
|
|
}
|
|
|
|
func (d *deadlines) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
dl, _ := r.Context().Deadline()
|
|
_, drawn := drawWaitOf(r.Context())
|
|
d.mu.Lock()
|
|
d.got, d.draw = append(d.got, time.Until(dl)), append(d.draw, drawn)
|
|
d.mu.Unlock()
|
|
return d.next.RoundTrip(r)
|
|
}
|
|
|
|
// neighbads on X: a slow image endpoint needed longer than the 5 minutes
|
|
// drawing was given. Settings' Image generation timeout of 30 minutes
|
|
// is what a drawing's request goes to the vendor with, through the
|
|
// gateway's own transport; with none set it is 5 minutes, as before.
|
|
func TestImageTimeoutSettingReachesTheVendor(t *testing.T) {
|
|
for _, c := range []struct {
|
|
minutes int
|
|
want time.Duration
|
|
}{{30, 30 * time.Minute}, {0, 5 * time.Minute}, {60, time.Hour}} {
|
|
s := slowDrawer(t, c.minutes, 0)
|
|
rec := &deadlines{next: s.client.Transport}
|
|
s.client = &http.Client{Transport: rec}
|
|
code, _, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"slow/gpt-image-1","prompt":"a magpie"}`)
|
|
if code != 200 {
|
|
t.Fatalf("%d minutes: %d %s", c.minutes, code, raw)
|
|
}
|
|
if len(rec.got) != 1 || !rec.draw[0] || rec.got[0] > c.want || rec.got[0] < c.want-time.Minute {
|
|
t.Fatalf("%d minutes: the vendor was asked with %v left (as a drawing: %v), want %v", c.minutes, rec.got, rec.draw, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The same, with the minutes made short: a vendor that takes past the
|
|
// default 5 to begin answering, and past the wait any other request is
|
|
// given to begin (upstreamHeaderWait), draws when Settings gives it 30;
|
|
// with the default it is said not to have answered in 5.
|
|
func TestLongImageTimeoutLetsASlowVendorFinish(t *testing.T) {
|
|
wasUnit, wasHead := drawUnit, upstreamHeaderWait
|
|
drawUnit, upstreamHeaderWait = 20*time.Millisecond, 100*time.Millisecond // 5 minutes 100ms, 30 600ms, 10 minutes 100ms
|
|
t.Cleanup(func() { drawUnit, upstreamHeaderWait = wasUnit, wasHead })
|
|
const slow = 300 * time.Millisecond
|
|
|
|
s := slowDrawer(t, 30, slow)
|
|
code, a, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"slow/gpt-image-1","prompt":"a magpie"}`)
|
|
if code != 200 || len(a.Data) != 1 {
|
|
t.Fatalf("given 30: %d %s", code, raw)
|
|
}
|
|
if b, _ := base64.StdEncoding.DecodeString(a.Data[0].B64); string(b) != string(pngBytes) {
|
|
t.Fatalf("image %q", b)
|
|
}
|
|
|
|
s = slowDrawer(t, 0, slow)
|
|
code, _, raw = postImages(t, s, "/v1/images/generations", "application/json", `{"model":"slow/gpt-image-1","prompt":"a magpie"}`)
|
|
if code != 504 || !strings.Contains(raw, "Slow didn't answer in 100ms") {
|
|
t.Fatalf("given the default: %d %s", code, raw)
|
|
}
|
|
}
|
|
|
|
// An image a vendor answers with as a link is downloaded within the same
|
|
// minutes: a long setting isn't cut by the 5-minute download cap.
|
|
func TestImageDownloadGetsTheSetMinutes(t *testing.T) {
|
|
slowDrawer(t, 0, 0)
|
|
if got := imageLongest(); got != ImageLongest {
|
|
t.Fatalf("unset: %v", got)
|
|
}
|
|
slowDrawer(t, 45, 0)
|
|
if got := imageLongest(); got != 45*time.Minute {
|
|
t.Fatalf("given 45: %v", got)
|
|
}
|
|
}
|
|
|
|
// A request that isn't a drawing still has upstreamHeaderWait to begin
|
|
// answering: only drawings wait longer.
|
|
func TestOnlyDrawingsWaitPastTheHeaderWait(t *testing.T) {
|
|
was := upstreamHeaderWait
|
|
upstreamHeaderWait = 100 * time.Millisecond
|
|
t.Cleanup(func() { upstreamHeaderWait = was })
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
select {
|
|
case <-r.Context().Done():
|
|
case <-time.After(400 * time.Millisecond):
|
|
}
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
c := &http.Client{Transport: upstreamTransport()}
|
|
req, _ := http.NewRequest(http.MethodGet, up.URL, nil)
|
|
if res, err := c.Do(req); err == nil || !strings.Contains(err.Error(), "timeout awaiting response headers") {
|
|
if res != nil {
|
|
res.Body.Close()
|
|
}
|
|
t.Fatalf("a chat's request waited past the header wait: %v", err)
|
|
}
|
|
ctx, cancel := drawWithin(t.Context(), 5*time.Second)
|
|
defer cancel()
|
|
req, _ = http.NewRequestWithContext(ctx, http.MethodGet, up.URL, nil)
|
|
res, err := c.Do(req)
|
|
if err != nil {
|
|
t.Fatalf("a drawing's request was cut: %v", err)
|
|
}
|
|
res.Body.Close()
|
|
}
|