From 340b72f1419e80aebd973591e3bde0e3da9785b4 Mon Sep 17 00:00:00 2001 From: linus-liu-web <395141741@qq.com> Date: Sat, 17 Oct 2026 22:48:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(models):=20=E5=9B=BE=E7=89=87=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=A4=8D=E7=94=A8=E4=BB=93=E5=82=A8=E5=B1=82=E7=9A=84?= =?UTF-8?q?=20provider=20scheme=20=E8=A1=A8=EF=BC=8C=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E4=B8=8A=E7=9A=84=E5=9B=BE=E7=89=87=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E5=8E=9F=E6=A0=B7=E4=BA=A4=E7=BB=99=E5=8E=82=E5=95=86?= =?UTF-8?q?=20(#4405)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/models/api/images.go | 15 ++++-- internal/models/api/images_test.go | 79 ++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/internal/models/api/images.go b/internal/models/api/images.go index 93f9f5b9..67421efb 100644 --- a/internal/models/api/images.go +++ b/internal/models/api/images.go @@ -9,11 +9,13 @@ import ( "os" "path/filepath" "strings" + + "github.com/Tencent/WeKnora/internal/types" ) // ResolveImageURLForLLM converts stored image paths to a format that LLM APIs can consume. // - data: URIs and http(s):// URLs are returned as-is. -// - resource:// and provider-backed local paths are read through the +// - resource:// references and provider:// paths are read through the // application resolver and converted to base64 data URIs. func ResolveImageURLForLLM(imageURL string) string { if strings.HasPrefix(imageURL, "data:") || @@ -50,10 +52,15 @@ func ResolveImageURLForOllama(imageURL string) []byte { return nil } +// isApplicationStoredImage reports whether imageURL names an image the +// application's storage layer can read: a resource:// reference, or a +// provider:// URL in the scheme the storage backends write (local://, +// minio://, s3://, cos://, ...). The provider list is owned by +// types.ParseProviderScheme, so a backend added there is resolvable here too. +// Keeping a second, narrower list in this package left "minio://bucket/key" +// unrecognized and it reached the vendor verbatim. func isApplicationStoredImage(imageURL string) bool { - return strings.HasPrefix(imageURL, "resource://") || - strings.HasPrefix(imageURL, "local://") || - strings.HasPrefix(imageURL, "storage://") + return strings.HasPrefix(imageURL, "resource://") || types.ParseProviderScheme(imageURL) != "" } // LocalImageResolver resolves a resource:// or provider storage URL to bytes diff --git a/internal/models/api/images_test.go b/internal/models/api/images_test.go index 743346a4..232e09d3 100644 --- a/internal/models/api/images_test.go +++ b/internal/models/api/images_test.go @@ -1,7 +1,9 @@ package api import ( + "bytes" "fmt" + "strings" "testing" ) @@ -51,6 +53,83 @@ func TestStripImagesFromMessages_DropsEmptiedMultiContent(t *testing.T) { } } +// A stored image is referenced by the scheme of the backend holding it: an +// object-storage FileService returns "minio://bucket/key", "s3://bucket/key" +// and so on, optionally wrapped as "storage:///://...". +// The predicate has to recognize the storage layer's scheme table, otherwise +// the handle travels to the vendor verbatim: remote APIs got "minio://..." as +// the image URL and the Ollama path dropped the picture without a word. +func TestResolveImageURLForStoredImages(t *testing.T) { + pngBytes := []byte("\x89PNG\r\n\x1a\nstored image payload") + resolved := map[string]bool{} + originalResolver := LocalImageResolver + LocalImageResolver = func(storageURL string) ([]byte, bool) { + resolved[storageURL] = true + return pngBytes, true + } + t.Cleanup(func() { LocalImageResolver = originalResolver }) + + stored := []struct { + name string + url string + }{ + {name: "local", url: "local://10000/1/img.png"}, + {name: "minio", url: "minio://bucket/1/img.png"}, + {name: "s3", url: "s3://bucket/1/img.png"}, + {name: "cos", url: "cos://bucket/region/1/img.png"}, + {name: "tos", url: "tos://bucket/1/img.png"}, + {name: "oss", url: "oss://bucket/1/img.png"}, + {name: "ks3", url: "ks3://bucket/1/img.png"}, + {name: "resource reference", url: "resource://a1b2c3/1/img.png"}, + {name: "backend-wrapped provider", url: "storage://backend-a/cos://bucket/region/1/img.png"}, + } + for _, tc := range stored { + t.Run(tc.name, func(t *testing.T) { + got := ResolveImageURLForLLM(tc.url) + if !strings.HasPrefix(got, "data:image/png;base64,") { + t.Fatalf("ResolveImageURLForLLM(%q) = %q, want a base64 PNG data URI", tc.url, got) + } + if data := ResolveImageURLForOllama(tc.url); !bytes.Equal(data, pngBytes) { + t.Fatalf("ResolveImageURLForOllama(%q) = %q, want the stored bytes", tc.url, data) + } + if !resolved[tc.url] { + t.Errorf("resolver was never asked for %q", tc.url) + } + }) + } +} + +// A data: or http(s) URL is not application-stored: it must pass through +// untouched and must never be routed through the storage resolver. +func TestResolveImageURLForNonStoredURLs(t *testing.T) { + originalResolver := LocalImageResolver + LocalImageResolver = func(storageURL string) ([]byte, bool) { + t.Errorf("resolver called for non-stored URL %q", storageURL) + return nil, false + } + t.Cleanup(func() { LocalImageResolver = originalResolver }) + + const dataURI = "data:image/png;base64,cGF5bG9hZA==" + const remoteURL = "https://example.com/1/img.png" + for _, tc := range []struct { + name string + url string + wantOllama []byte + }{ + {name: "data URI", url: dataURI, wantOllama: []byte("payload")}, + {name: "http URL", url: remoteURL}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := ResolveImageURLForLLM(tc.url); got != tc.url { + t.Errorf("ResolveImageURLForLLM(%q) = %q, want it unchanged", tc.url, got) + } + if data := ResolveImageURLForOllama(tc.url); !bytes.Equal(data, tc.wantOllama) { + t.Errorf("ResolveImageURLForOllama(%q) = %q, want %q", tc.url, data, tc.wantOllama) + } + }) + } +} + func TestIsMultimodalNotSupportedError(t *testing.T) { cases := []struct { name string