kelvinjason11 on X: "图片识别好像只支持一个模型识别 没有fallback". When
the describer (Settings' Image recognition model, or AutoVision's pick)
failed, the turn failed with it, though other models that see were there.
Now describeWith asks the pick first, always. When it fails for any reason
but the client's own cancel, magpie asks the next model that sees: the
first ready one of seersRanked, AutoVision's own order (ready, then
Claude account, Codex, API key, other, then cost), leaving out each model
already tried and each one that turned an image away (sightRefused). At
most 3 models (visionTries), and all of them within visionWaitMost
together: each try gets what is left of it, not a fresh visionTimeout.
- The text header names the model that described the image
("as other/bigeye describes it"), not the pick.
- The Routing row of the stand-in carries Instead, Why and Picked
(describingInstead, the same fields web search's searchingInstead
sets), and the Routing page says in every language that the pick
couldn't describe it, why, and which model did instead.
- When none answers, the error is the first failure as before, then
"nor could X, asked in its place: why" for each other model tried.
- Spent/allowance: each try goes through serve as before, so autoSpent
and spentModel judge every stand-in the same as they judged the pick.
- vision_mcp's describe_image tool gets the same fallback.
Cache (s.sights): a description is kept under the pick and the image,
whichever model described it, with the describer's name beside it. The
same image sent again asks no one, the stand-in's text is reused, and
its header still names the stand-in. A new image asks the pick first
again, so a pick that comes back is used at once. Failures are not kept.
Settings' text ("When the model in use can't see images, this one
describes them to it") is left as it is: the pick is still asked first
for every image.
Tests (internal/gateway/vision_fallback_test.go):
- TestPickedDescriberThatFailsGivesWayToTheNextThatSees
- TestAutoDescriberThatFailsGivesWayToTheNext
- TestNoDescriberAnswersNamesEach
- TestPickedDescriberThatAnswersIsTheOnlyOneAsked
- TestCanceledDescriptionAsksNoOther
TestDescriberThatCantSeeIsntAskedToDescribe now expects the next model
that sees to describe in place of a text-only pick, not a 502.
GUI: internal/gui/tests/vision-instead.test.cjs (every language, 1100
and 520 wide).
Run on mote at 45739f15b (this tree, on origin/main 74f1df869):
- go test -tags nogui ./internal/gateway/ -count=1 -timeout 45m: ok
(542.988s). An earlier run while mote was at load ~26 failed four
Antigravity turned-away tests and TestCodexSpentGrokFailingIsTold.
None of them touches vision, and all passed in this run.
- gofmt -l: nothing.
- GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./...: ok.
- GUI, Chromium: vision-instead, search-instead, routing-vision-why,
vision-mcp, routing-kind and i18n-keys (4/4) pass.
Broken by hand (in a worktree at 0fcb3acb3):
- The old code with the new tests: Picked, Auto and NoDescriberAnswers
go red. PickedThatAnswers and Canceled pass, as they should on code
that never falls back.
- The ctx.Err() check removed from describeWith: Canceled goes red (a
vision row for other/bigeye appears).
- describeWith made to ask the next model after a success:
PickedThatAnswers goes red ("eye asked 1 times, bigeye 1").
Not tried:
- The GUI files in WebKit. Playwright's WebKit launch timed out at 180s
while mote was at load ~25 from other suites. To be rerun.
- vision-instead against the old Routing client, to watch it go red.
- No real account: the fallback was run only against fake upstreams.
202 lines
8.4 KiB
Go
202 lines
8.4 KiB
Go
package gateway
|
||
|
||
import (
|
||
"context"
|
||
"encoding/base64"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"log"
|
||
"net/http"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
|
||
"github.com/yetone/magpie/internal/access"
|
||
"github.com/yetone/magpie/internal/provider"
|
||
)
|
||
|
||
// magpie's image recognition, the model that describes images to a model
|
||
// that can't see them (seer: Settings › Models › Image recognition, else
|
||
// AutoVision's), is also an MCP server of its own beside the web search's
|
||
// (JonahFu on X), so an agent on a model that can't see, or any agent at
|
||
// all, can mount it as an analyze_image tool: Streamable HTTP at
|
||
// VisionMCPPath, served and guarded as SearchMCPPath is (serveMCP), a
|
||
// gateway key's limit holding each look (reserveFor).
|
||
|
||
// VisionMCPPath is where the image recognition MCP server is served.
|
||
const VisionMCPPath = "/mcp/magpie/vision"
|
||
|
||
const visionMCPTool = "analyze_image"
|
||
|
||
// visionMCPBody is how long a message to the server may be: an image is
|
||
// sent in it as a data URL.
|
||
const visionMCPBody = 32 << 20
|
||
|
||
// visionFileMax is the largest image file the tool reads from a path.
|
||
const visionFileMax = 20 << 20
|
||
|
||
// visionAskSystem is what the model is told when the agent asks something
|
||
// of the image, in place of visionSystem's full description.
|
||
const visionAskSystem = `You look at an image for an AI agent that cannot see it, and answer what it asks about the image from what the image shows.
|
||
- Transcribe text exactly as written wherever the answer needs it: code, terminal output, error messages, UI labels, numbers.
|
||
- Say so when what is asked can't be read or isn't in the image; don't guess.
|
||
No preamble.`
|
||
|
||
var visionMCPSchema = map[string]any{
|
||
"type": "object",
|
||
"properties": map[string]any{
|
||
"image": map[string]any{
|
||
"type": "string",
|
||
"description": "The image: an absolute path to an image file on this computer (PNG, JPEG, GIF or WebP), an http(s) URL, or a data URL (data:image/png;base64,...).",
|
||
},
|
||
"prompt": map[string]any{
|
||
"type": "string",
|
||
"description": "What to find out about the image. Without it the image is described in full: its text transcribed, its layout, charts and diagrams.",
|
||
},
|
||
},
|
||
"required": []string{"image"},
|
||
}
|
||
|
||
func (s *Server) visionMCP(w http.ResponseWriter, r *http.Request) {
|
||
serveMCP(w, r, "magpie's image recognition", "magpie-vision", visionMCPBody, s.visionMCPCall)
|
||
}
|
||
|
||
// visionMCPCall answers one request: its result, or its JSON-RPC error.
|
||
func (s *Server) visionMCPCall(r *http.Request, m mcpMessage) (any, map[string]any) {
|
||
switch m.Method {
|
||
case "initialize":
|
||
return map[string]any{
|
||
"protocolVersion": mcpVersion(m),
|
||
"capabilities": map[string]any{"tools": map[string]any{}},
|
||
"serverInfo": map[string]any{"name": "magpie-vision", "title": "Magpie Image Recognition", "version": Version},
|
||
"instructions": "analyze_image looks at an image with the model magpie describes images with (Settings → Models → Image recognition) and answers in text: for a model or agent that can't see images, or to read a screenshot, chart or diagram.",
|
||
}, nil
|
||
case "ping":
|
||
return map[string]any{}, nil
|
||
case "tools/list":
|
||
return map[string]any{"tools": []any{map[string]any{
|
||
"name": visionMCPTool,
|
||
"title": "Analyze image",
|
||
"description": "Looks at an image and answers in text: what the prompt asks of it, or, with no prompt, a full description with all its text transcribed. Use it to read screenshots, error dialogs, charts, diagrams and photos when you can't see images yourself.",
|
||
"inputSchema": visionMCPSchema,
|
||
"annotations": map[string]any{"readOnlyHint": true, "openWorldHint": false},
|
||
}}}, nil
|
||
case "tools/call":
|
||
var p struct {
|
||
Name string `json:"name"`
|
||
Arguments struct {
|
||
Image string `json:"image"`
|
||
Prompt string `json:"prompt"`
|
||
} `json:"arguments"`
|
||
}
|
||
if err := json.Unmarshal(m.Params, &p); err != nil {
|
||
return nil, map[string]any{"code": -32602, "message": "invalid params: " + err.Error()}
|
||
}
|
||
if p.Name != visionMCPTool {
|
||
return nil, map[string]any{"code": -32602, "message": "unknown tool " + p.Name + ": this server has " + visionMCPTool}
|
||
}
|
||
said, err := s.visionMCPRun(r, strings.TrimSpace(p.Arguments.Image), strings.TrimSpace(p.Arguments.Prompt))
|
||
if err != nil {
|
||
return toolResult(err.Error(), true), nil
|
||
}
|
||
return toolResult(said, false), nil
|
||
}
|
||
return nil, map[string]any{"code": -32601, "message": "method not found: " + m.Method}
|
||
}
|
||
|
||
// visionMCPRun has the image looked at as an image of a model's request
|
||
// would be, held to the calling gateway key's limit, and named in the
|
||
// Routing view as the agent's MCP call.
|
||
func (s *Server) visionMCPRun(r *http.Request, image, prompt string) (string, error) {
|
||
if image == "" {
|
||
return "", searchMCPError("analyze_image needs an image: a file path, an http(s) URL or a data URL")
|
||
}
|
||
src, err := mcpImageSrc(r, image, visionMCPTool)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
model, ok := seer()
|
||
if !ok {
|
||
if provider.HeldSettings().Vision == "off" {
|
||
return "", searchMCPError("Image recognition is off in magpie: pick a model for it in Settings → Models → Image recognition")
|
||
}
|
||
return "", searchMCPError("magpie has no model that sees images: add a provider with one, or pick one in Settings → Models → Image recognition")
|
||
}
|
||
release, err := reserveFor(access.Caller(r.Context()), int64(len(src)+len(prompt)), "", limitClock())
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
defer release()
|
||
ctx := context.WithValue(r.Context(), describeForKey{}, describeFor{call: &CallFor{Agent: callerOf(r).agent, Missing: VisionMissing(), MCP: true}})
|
||
var said string
|
||
if prompt == "" {
|
||
said, _, err = s.describe(ctx, model, src)
|
||
} else {
|
||
said, _, err = s.describeWith(ctx, model, src, visionAskSystem, prompt)
|
||
}
|
||
if err != nil {
|
||
if r.Context().Err() == nil {
|
||
log.Printf("vision MCP: %v", err)
|
||
}
|
||
return "", searchMCPError(fmt.Sprintf("magpie couldn't look at the image with %s: %v%s", model, err, missingSeerNote()))
|
||
}
|
||
return said, nil
|
||
}
|
||
|
||
// mcpImageSrc is an image an MCP tool (tool, which names it in what it says)
|
||
// was given, as the gateway's own APIs take it: a URL or data URL as it
|
||
// is, a file read into a data URL. A file is read only for an agent on
|
||
// this computer: another machine's path names none of its own here, and a
|
||
// shared magpie's files aren't its callers'. analyze_image and the image
|
||
// server's tools (image_mcp.go) read their images through it.
|
||
func mcpImageSrc(r *http.Request, image, tool string) (string, error) {
|
||
low := strings.ToLower(image)
|
||
switch {
|
||
case strings.HasPrefix(low, "data:"):
|
||
if !strings.HasPrefix(low, "data:image/") || !strings.Contains(low[:min(len(low), 64)], ";base64,") {
|
||
return "", searchMCPError(tool + " takes a data URL of an image in base64 (data:image/png;base64,...)")
|
||
}
|
||
return image, nil
|
||
case strings.HasPrefix(low, "http://"), strings.HasPrefix(low, "https://"):
|
||
return image, nil
|
||
}
|
||
path := image
|
||
if strings.HasPrefix(low, "file://") {
|
||
path = image[len("file://"):]
|
||
}
|
||
if strings.HasPrefix(path, "~/") {
|
||
if home, err := os.UserHomeDir(); err == nil {
|
||
path = filepath.Join(home, path[2:])
|
||
}
|
||
}
|
||
if !filepath.IsAbs(path) {
|
||
return "", searchMCPError(tool + " takes an absolute file path (the gateway doesn't know your agent's working folder), an http(s) URL or a data URL: " + image + " is none of them")
|
||
}
|
||
if !local(r) {
|
||
return "", searchMCPError(tool + " reads a file only for an agent on the computer magpie runs on: from another computer, send the image as a data URL (data:image/png;base64,...) or an http(s) URL")
|
||
}
|
||
f, err := os.Open(path)
|
||
if err != nil {
|
||
return "", searchMCPError(tool + " couldn't read " + path + ": " + err.Error())
|
||
}
|
||
defer f.Close()
|
||
if st, err := f.Stat(); err == nil && st.IsDir() {
|
||
return "", searchMCPError(path + " is a folder, not an image")
|
||
} else if err == nil && st.Size() > visionFileMax {
|
||
return "", searchMCPError(fmt.Sprintf("%s is %d MB: %s reads images up to %d MB", path, st.Size()>>20, tool, visionFileMax>>20))
|
||
}
|
||
b, err := io.ReadAll(io.LimitReader(f, visionFileMax+1))
|
||
if err != nil {
|
||
return "", searchMCPError(tool + " couldn't read " + path + ": " + err.Error())
|
||
}
|
||
if len(b) > visionFileMax {
|
||
return "", searchMCPError(fmt.Sprintf("%s is over %d MB: %s reads images up to %d MB", path, visionFileMax>>20, tool, visionFileMax>>20))
|
||
}
|
||
mime := http.DetectContentType(b)
|
||
if !strings.HasPrefix(mime, "image/") {
|
||
return "", searchMCPError(path + " isn't an image " + tool + " can read (PNG, JPEG, GIF or WebP): it reads as " + mime)
|
||
}
|
||
return "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(b), nil
|
||
}
|