Files
magpie/internal/imagemcp/imagemcp.go
T
yetone f0dc9ea695 imagemcp, gateway: a 4K image a relay answers by URL is saved, and one that can't be downloaded is answered with its URL, not lost (#1576, david-zdw)
What the reporter saw: generate_image (magpie mcp image) failed on 4K and
2048x1152 with "image 1: context deadline exceeded" and "no image: context
deadline exceeded", while the same request to /v1/images/generations came
back in ~40 s, and usage.jsonl showed the upstream's 200. Their relay
answers with a URL whatever response_format says.

Two causes, one per error:

- "image 1: context deadline exceeded" was generate_image's own download
  of the URL (imagemcp.download): 2 minutes for the whole of it, on
  http.DefaultTransport, which knows the global proxy but not a provider's
  own (#237). A 10 MB PNG that was still coming was cut, and the error
  dropped the URL of an image already drawn and paid for. Any earlier
  image of n was saved but not said either.
- "no image: context deadline exceeded" (the usage log's 502s at
  300002-300032 ms) was the gateway's drawTimeout (5 min) expiring while
  it read a vendor's answer that had already begun: sendWith passed the
  bare context error up. The vendor really took over 5 minutes there;
  this commit doesn't change that limit, it says what happened.

What changed:

- gateway: an images caller that asks response_format "b64_json" gets the
  image itself. One the vendor gave only a URL for is fetched by the
  gateway's own client, through the provider's proxy (p.Via), after the
  draw and outside drawTimeout. One that can't be fetched is answered by
  its URL, as before, never dropped. A caller that asks no format still
  gets the URL. Nothing is sent to the vendor differently.
- gateway.FetchImage: an image download gives up when nothing has come
  for ImageStall (1 min) or it has taken ImageLongest (5 min), and says
  which and how much had come. A slow download goes on while it moves.
  The edits' fetchPicture uses it too.
- gateway: a vendor that began answering but hadn't finished in
  drawTimeout is a 504 "<name> began answering after Ns (200 OK) but
  hadn't sent all of its answer in 5m0s", not a 502 "context deadline
  exceeded".
- imagemcp: asks for b64_json; downloads a URL the gateway still hands it
  with FetchImage; an image it can't have is answered with the vendor's
  URL to download it from, beside the ones saved (an error only when
  none was). Its client to the gateway waits ToolTimeout (11 min), past
  the draw and the fetch together, not 6 minutes.

What was run:

- Reproduced on the old code: the new TestFourKImageByURLIsSaved (a real
  3840x2160 PNG, 9.8 MB, served by URL in 256 KB pieces over ~4 s) with
  the old download's 2-minute cap scaled to 1 s fails with the
  reporter's exact "image 1: context deadline exceeded"; the stalled
  case fails the same way, with no URL in the error.
- Broke each part by hand and watched its test go red: no inline fetch,
  fetch not through the provider's proxy, the bare deadline message, the
  stall timer not reset (a total cap again), a lost image ending the
  call, the MCP not asking b64_json.
- go test ./internal/imagemcp/ ./internal/gateway/ ; the full
  go test -tags nogui ./... ; gofmt -l ; GOOS=linux and GOOS=windows
  go vet on both packages.
- On the Windows box (Windows 11 x64): imagemcp.test.exe all passed, and
  gateway.test.exe for the new tests and the images/ModelScope tests.

Not tried: the reporter's relay itself (never contacted), and a real
4K draw end to end. Why their URL took over 2 minutes from the MCP when
their own download took 7.5 s isn't known from here: a provider proxy
the MCP didn't use is one way it happens; a slow file host is another.
Both are covered now.
2026-10-11 14:59:23 +08:00

879 lines
28 KiB
Go

// Package imagemcp is `magpie mcp image`: a stdio MCP server an agent is
// given, when it is picked for it in the Library, to make images with the
// model the Settings' Image generation names, and videos with the first
// video model the gateway has (gateway.AutoVideomaker). It asks the
// gateway, which knows the providers, and saves what comes back in the
// project.
package imagemcp
import (
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/gateway"
)
// Folder is where images go in the project when no path is given, and
// VideoFolder where videos do.
const (
Folder = "generated-images"
VideoFolder = "generated-videos"
)
const (
tool = "generate_image"
videoTool = "generate_video"
resultTool = "generation_result"
)
// answerWithin is how long a tool call waits for its image or video before
// it answers that it is still being made, with the job's id to ask after
// it by. Agents give a tool call as little as 30 s (#575: ZCode's "Tool
// execution timed out after 30000ms", the MCP SDK's 60 s "Request timed
// out") and then call it again, though an image model often takes a
// minute or more: so no call of this server outlasts the shortest of them,
// and a job goes on after its call has answered, saving what it makes.
// progressEvery is how often a call that was given a progressToken is
// told the job is still going, for clients that wait longer on progress.
var (
answerWithin = 20 * time.Second
progressEvery = 5 * time.Second
)
// How often a video is asked after, and how long it is waited for.
var (
videoPollEvery = 3 * time.Second
videoWait = 10 * time.Minute
)
var videoSchema = map[string]any{
"name": videoTool,
"description": "Generate a short video from a text prompt, optionally starting from an image to animate, with a video model set up in Magpie " +
"(a Grok subscription's Grok Imagine, Volcengine Ark Agent Plan's Seedance, another Magpie's, or a provider whose model list marks video models). It takes from ten seconds to a few minutes, then the video is saved as an mp4 file in the project (" + VideoFolder +
"/ unless path says where) and its path is returned; when it is not ready within about 20 seconds, the answer is an id to wait on with " + resultTool + " instead. Describe the subject, the motion and the camera: what moves, how, and what the shot is.",
"inputSchema": map[string]any{
"type": "object",
"properties": map[string]any{
"prompt": map[string]any{"type": "string", "description": "What happens in the video: the subject, its motion, the camera."},
"path": map[string]any{"type": "string", "description": "Where to save it: a file (e.g. assets/intro.mp4) or a folder, relative to the project or absolute. Never written over."},
"seconds": map[string]any{"type": "integer", "minimum": 1, "maximum": 15, "description": "Length in whole seconds (1-15). Default: the model's (about 8)."},
"size": map[string]any{"type": "string", "description": "An aspect ratio (16:9, 9:16, 1:1, 4:3, 3:4, 3:2, 2:3) or WIDTHxHEIGHT (1280x720; its shorter side picks 480p, 720p or 1080p). Default: the model's."},
"image": map[string]any{"type": "string", "description": "An image to animate, the video's first frame: a file path (relative to the project or absolute) or an http(s) URL."},
"reference_images": map[string]any{"type": "array", "items": map[string]any{"type": "string"}, "description": "Images the video's subjects are drawn from: file paths or http(s) URLs."},
},
"required": []string{"prompt"},
},
}
var resultSchema = map[string]any{
"name": resultTool,
"description": "Wait for an image or video that " + tool + " or " + videoTool + " said is still being made, by the id it gave, and get where it was saved. " +
"It answers within about " + strconv.Itoa(int(answerWithin/time.Second)) + " seconds: if it is still not ready, call this again with the same id (don't generate it again).",
"inputSchema": map[string]any{
"type": "object",
"properties": map[string]any{
"id": map[string]any{"type": "string", "description": "The id " + tool + " or " + videoTool + " answered with."},
},
"required": []string{"id"},
},
}
var schema = map[string]any{
"name": tool,
"description": "Generate an image from a text prompt, or edit/combine images given as reference_images, with the image model set in Magpie. " +
"The image is saved as a file in the project (" + Folder + "/ unless path says where) and its path is returned (when it is not ready within about 20 seconds, the answer is an id to wait on with " + resultTool + " instead); " +
"reference it from code or docs by that path. Write a detailed prompt: subject, style, composition, colours, any text to render exactly.",
"inputSchema": map[string]any{
"type": "object",
"properties": map[string]any{
"prompt": map[string]any{"type": "string", "description": "What to draw, or how to change the reference images."},
"path": map[string]any{"type": "string", "description": "Where to save it: a file (e.g. assets/hero.png) or a folder, relative to the project or absolute. With n > 1 a number is added to the name."},
"size": map[string]any{"type": "string", "description": "WIDTHxHEIGHT (1024x1024, 1536x1024, 1024x1536) or an aspect ratio (16:9). Default: the model's."},
"n": map[string]any{"type": "integer", "minimum": 1, "maximum": 4, "description": "How many images (default 1)."},
"reference_images": map[string]any{"type": "array", "items": map[string]any{"type": "string"}, "description": "Images to edit or draw from: file paths or http(s) URLs."},
"quality": map[string]any{"type": "string", "description": "low, medium, high or auto, for models that take it."},
"background": map[string]any{"type": "string", "description": "transparent, opaque or auto, for models that take it."},
},
"required": []string{"prompt"},
},
}
type rpc struct {
ID json.RawMessage `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Error json.RawMessage `json:"error,omitempty"`
}
type server struct {
gateway string
client *http.Client
outMu sync.Mutex
enc *json.Encoder
mu sync.Mutex
agent string // the User-Agent, from the client's name
roots bool // the client can say its roots
pending map[string]chan rpc
nextCall int
jobs map[string]*job
nextJob int
running sync.WaitGroup
}
// job is one image or video being made: it runs to its end whether or not
// the call that started it is still waiting.
type job struct {
id, tool string
started time.Time
done chan struct{}
text string
err error
}
// start runs run as a job of tool.
func (s *server) start(tool string, run func() (string, error)) *job {
s.mu.Lock()
if s.jobs == nil {
s.jobs = map[string]*job{}
}
s.nextJob++
j := &job{id: fmt.Sprintf("%s-%d-%d", strings.TrimPrefix(tool, "generate_"), os.Getpid(), s.nextJob), tool: tool, started: time.Now(), done: make(chan struct{})}
s.jobs[j.id] = j
s.mu.Unlock()
s.running.Add(1)
go func() {
defer s.running.Done()
j.text, j.err = run()
close(j.done)
}()
return j
}
// await waits up to answerWithin for j, telling the client it is still
// going every progressEvery when it gave a progressToken; false when j is
// still running.
func (s *server) await(j *job, progressToken json.RawMessage) bool {
limit := time.NewTimer(answerWithin)
defer limit.Stop()
tick := time.NewTicker(progressEvery)
defer tick.Stop()
for {
select {
case <-j.done:
return true
case <-limit.C:
return false
case <-tick.C:
if len(progressToken) > 0 && string(progressToken) != "null" {
s.send(map[string]any{"jsonrpc": "2.0", "method": "notifications/progress", "params": map[string]any{
"progressToken": progressToken, "progress": int(time.Since(j.started) / time.Second),
"message": "still being made (" + j.id + ")"}})
}
}
}
}
// stillGoing is the answer of a call whose job outlasted it.
func stillGoing(j *job) string {
what, where := "image", Folder
if j.tool == videoTool {
what, where = "video", VideoFolder
}
return fmt.Sprintf("The %s is still being made (%d s so far; it can take a few minutes). It goes on without this call and will be saved in the project (%s/ unless path said where). "+
"Don't ask for it again: call %s with id %q to wait for it and get its path.\n", what, int(time.Since(j.started)/time.Second), where, resultTool, j.id)
}
// ToolTimeout is how long a job of this server may run: generate_video
// waits up to videoWait for its video, and a minute more. A call answers
// within answerWithin all the same; the agents whose config takes it
// (Codex, Goose) are still given it, as a call's ceiling.
func ToolTimeout() time.Duration { return videoWait + time.Minute }
// Run serves MCP on stdin/stdout until stdin closes.
func Run(args []string) error {
if len(args) > 0 && args[0] != "image" {
return fmt.Errorf("unknown MCP server %q (there is: image)", args[0])
}
if len(args) > 1 {
w, err := options(args[1:])
if err != nil {
return err
}
wsl = w
}
base := strings.TrimRight(os.Getenv("MAGPIE_GATEWAY"), "/")
if base == "" {
base = gateway.URL()
}
// the gateway draws for up to 5 minutes and may then fetch what the
// vendor drew for up to 5 more (gateway.ImageLongest): a call to it
// is cut only past both
s := &server{gateway: base, client: &http.Client{Timeout: ToolTimeout()}, enc: json.NewEncoder(os.Stdout), agent: gateway.DrawAgent, pending: map[string]chan rpc{}}
err := s.serve(os.Stdin)
// what is still being made is saved, though the agent has gone
done := make(chan struct{})
go func() { s.running.Wait(); close(done) }()
select {
case <-done:
case <-time.After(ToolTimeout()):
}
return err
}
func (s *server) send(v any) {
s.outMu.Lock()
_ = s.enc.Encode(v)
s.outMu.Unlock()
}
func (s *server) serve(r io.Reader) error {
in := bufio.NewScanner(r)
in.Buffer(make([]byte, 64<<10), 16<<20)
for in.Scan() {
var m rpc
if json.Unmarshal(in.Bytes(), &m) != nil {
continue
}
if m.Method == "" {
// the answer to a request of ours (roots/list)
s.mu.Lock()
ch := s.pending[string(m.ID)]
delete(s.pending, string(m.ID))
s.mu.Unlock()
if ch != nil {
ch <- m
}
continue
}
if len(m.ID) == 0 {
continue // a notification
}
if m.Method == "initialize" {
// before anything after it: the agent's name goes on its calls
s.handle(m)
continue
}
go s.handle(m)
}
return in.Err()
}
func (s *server) handle(req rpc) {
var result any
var rpcErr any
switch req.Method {
case "initialize":
var p struct {
Capabilities struct {
Roots json.RawMessage `json:"roots"`
} `json:"capabilities"`
ClientInfo struct {
Name string `json:"name"`
Version string `json:"version"`
} `json:"clientInfo"`
}
json.Unmarshal(req.Params, &p)
s.mu.Lock()
s.roots = len(p.Capabilities.Roots) > 0 && string(p.Capabilities.Roots) != "null"
if name := strings.ReplaceAll(strings.TrimSpace(p.ClientInfo.Name), " ", "-"); name != "" {
// the call is the agent's: usage names it, not magpie
if p.ClientInfo.Version != "" {
name += "/" + p.ClientInfo.Version
}
s.agent = name + " " + gateway.DrawAgent
}
s.mu.Unlock()
result = map[string]any{
"protocolVersion": "2025-06-18",
"capabilities": map[string]any{"tools": map[string]any{}},
"serverInfo": map[string]any{"name": "magpie-image", "version": "1"},
}
case "ping":
result = map[string]any{}
case "tools/list":
result = map[string]any{"tools": []any{schema, videoSchema, resultSchema}}
case "tools/call":
var p struct {
Name string `json:"name"`
Arguments json.RawMessage `json:"arguments"`
Meta struct {
ProgressToken json.RawMessage `json:"progressToken"`
} `json:"_meta"`
}
if err := json.Unmarshal(req.Params, &p); err != nil || (p.Name != tool && p.Name != videoTool && p.Name != resultTool) {
rpcErr = map[string]any{"code": -32602, "message": "unknown tool " + p.Name}
break
}
var j *job
if p.Name == resultTool {
var a struct {
ID string `json:"id"`
}
json.Unmarshal(p.Arguments, &a)
s.mu.Lock()
j = s.jobs[strings.TrimSpace(a.ID)]
s.mu.Unlock()
if j == nil {
result = map[string]any{"content": []any{map[string]any{"type": "text", "text": fmt.Sprintf("no image or video is being made as %q (magpie-image may have been restarted): look in the project's %s/ or %s/, or generate it again", a.ID, Folder, VideoFolder)}}, "isError": true}
break
}
} else {
generate := s.generate
if p.Name == videoTool {
generate = s.film
}
j = s.start(p.Name, func() (string, error) { return generate(p.Arguments) })
}
if !s.await(j, p.Meta.ProgressToken) {
result = map[string]any{"content": []any{map[string]any{"type": "text", "text": stillGoing(j)}}}
break
}
if j.err != nil {
result = map[string]any{"content": []any{map[string]any{"type": "text", "text": j.err.Error()}}, "isError": true}
break
}
result = map[string]any{"content": []any{map[string]any{"type": "text", "text": j.text}}}
default:
rpcErr = map[string]any{"code": -32601, "message": "method not found"}
}
out := map[string]any{"jsonrpc": "2.0", "id": req.ID}
if rpcErr != nil {
out["error"] = rpcErr
} else {
out["result"] = result
}
s.send(out)
}
type args struct {
Prompt string `json:"prompt"`
Path string `json:"path"`
Size string `json:"size"`
N int `json:"n"`
References []string `json:"reference_images"`
Quality string `json:"quality"`
Background string `json:"background"`
}
// generate asks the gateway for the images and saves them; what it says is
// where they are.
func (s *server) generate(raw json.RawMessage) (string, error) {
var a args
if err := json.Unmarshal(raw, &a); err != nil {
return "", err
}
if strings.TrimSpace(a.Prompt) == "" {
return "", errors.New("prompt is required")
}
a.N = max(1, min(a.N, 4))
project := s.project()
// the images themselves, not URLs to fetch: the gateway fetches one a
// vendor keeps through the vendor's own proxy, the way it just reached
// it (#1576)
req := map[string]any{"prompt": a.Prompt, "n": a.N, "response_format": "b64_json"}
for k, v := range map[string]string{"size": a.Size, "quality": a.Quality, "background": a.Background} {
if v != "" {
req[k] = v
}
}
endpoint := "/v1/images/generations"
if len(a.References) > 0 {
endpoint = "/v1/images/edits"
var images []map[string]string
for _, ref := range a.References {
src, err := reference(project, ref)
if err != nil {
return "", err
}
images = append(images, map[string]string{"image_url": src})
}
req["images"] = images
}
body, _ := json.Marshal(req)
r, _ := http.NewRequest(http.MethodPost, s.gateway+endpoint, bytes.NewReader(body))
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Authorization", "Bearer "+gateway.Token)
s.mu.Lock()
r.Header.Set("User-Agent", s.agent)
s.mu.Unlock()
res, err := s.client.Do(r)
if err != nil {
return "", fmt.Errorf("magpie isn't answering at %s (is it running?): %w", s.gateway, err)
}
defer res.Body.Close()
data, _ := io.ReadAll(io.LimitReader(res.Body, 256<<20))
var out struct {
Model string `json:"model"`
Data []struct {
B64 string `json:"b64_json"`
URL string `json:"url"`
Mime string `json:"mime_type"`
Revised string `json:"revised_prompt"`
} `json:"data"`
Text string `json:"text"`
Error *struct {
Message string `json:"message"`
} `json:"error"`
}
json.Unmarshal(data, &out)
if res.StatusCode != 200 {
msg := strings.TrimSpace(string(data))
if out.Error != nil && out.Error.Message != "" {
msg = out.Error.Message
}
return "", fmt.Errorf("no image: %s", msg)
}
if len(out.Data) == 0 {
return "", errors.New("no image came back")
}
// an image that was drawn, and paid for, is never dropped unsaid: one
// that can't be had is answered by where the vendor keeps it, beside
// those saved (#1576)
var saved, revised, lost []string
for i, d := range out.Data {
img, mt := []byte(nil), d.Mime
switch {
case d.B64 != "":
if img, err = base64.StdEncoding.DecodeString(d.B64); err != nil {
lost = append(lost, fmt.Sprintf("image %d came back broken: %v", i+1, err))
continue
}
case d.URL != "":
if img, mt, err = gateway.FetchImage(context.Background(), http.DefaultClient, d.URL); err != nil {
lost = append(lost, fmt.Sprintf("image %d was drawn but couldn't be downloaded (%v); the vendor keeps it at %s for a while: download it from there", i+1, err, d.URL))
continue
}
default:
continue
}
// what the bytes are, over what the vendor says: a model asked for
// a PNG may well send a JPEG
if sniffed := http.DetectContentType(img); strings.HasPrefix(sniffed, "image/") {
mt = sniffed
}
path, err := target(project, a.Path, a.Prompt, extOf(mt), i, len(out.Data))
if err != nil {
return "", err
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return "", err
}
if err := os.WriteFile(path, img, 0o644); err != nil {
return "", err
}
saved = append(saved, path)
if d.Revised != "" {
revised = append(revised, d.Revised)
}
}
if len(saved) == 0 && len(lost) > 0 {
return "", errors.New(strings.Join(lost, "\n"))
}
var b strings.Builder
fmt.Fprintf(&b, "Generated with %s, saved to:\n", out.Model)
for _, p := range saved {
fmt.Fprintf(&b, "- %s\n", shownPath(p))
}
for _, l := range lost {
fmt.Fprintf(&b, "Not saved: %s\n", l)
}
if len(revised) > 0 {
fmt.Fprintf(&b, "The model drew from this prompt: %s\n", revised[0])
}
if t := strings.TrimSpace(out.Text); t != "" {
fmt.Fprintf(&b, "The model said: %s\n", t)
}
return b.String(), nil
}
// project is the folder images are saved in: the client's first root when
// it can say, else the folder the agent started magpie in. The home folder
// or / isn't a project: ~/Pictures/Magpie is used then (the distro's, for
// an agent in WSL).
func (s *server) project() string {
if dir := s.root(); dir != "" {
return dir
}
dir, _ := os.Getwd()
if wsl != nil && wsl.home != "" {
if d := wsl.shown(dir); d == "" || d == "/" || strings.TrimSuffix(d, "/") == strings.TrimSuffix(wsl.home, "/") {
return wsl.local(strings.TrimSuffix(wsl.home, "/") + "/Pictures/Magpie")
}
}
home, _ := os.UserHomeDir()
if dir == "" || dir == "/" || dir == home || filepath.Dir(dir) == dir {
return filepath.Join(home, "Pictures", "Magpie")
}
return dir
}
// root asks the client for its roots, the first of which on disk is the
// project; "" when it has none or doesn't answer.
func (s *server) root() string {
s.mu.Lock()
if !s.roots {
s.mu.Unlock()
return ""
}
s.nextCall++
id := fmt.Sprintf(`"magpie-roots-%d"`, s.nextCall)
ch := make(chan rpc, 1)
s.pending[id] = ch
s.mu.Unlock()
s.send(map[string]any{"jsonrpc": "2.0", "id": json.RawMessage(id), "method": "roots/list"})
select {
case m := <-ch:
var r struct {
Roots []struct {
URI string `json:"uri"`
} `json:"roots"`
}
json.Unmarshal(m.Result, &r)
for _, root := range r.Roots {
if u, err := url.Parse(root.URI); err == nil && u.Scheme == "file" && u.Path != "" {
if wsl != nil {
return wsl.local(u.Path)
}
return filePath(u)
}
}
case <-time.After(3 * time.Second):
s.mu.Lock()
delete(s.pending, id)
s.mu.Unlock()
}
return ""
}
// filePath is the file a file:// URI names: on Windows file:///C:/p is C:\p
// (not \C:\p, a path nowhere) and file://server/share/p is \\server\share\p.
func filePath(u *url.URL) string {
p := u.Path
if runtime.GOOS == "windows" {
if u.Host != "" && u.Host != "localhost" {
return `\\` + u.Host + filepath.FromSlash(p)
}
if len(p) >= 3 && p[0] == '/' && p[2] == ':' {
p = p[1:]
}
}
return filepath.FromSlash(p)
}
// reference is ref as the gateway takes it: a URL as it is, a file as a
// data URL.
func reference(project, ref string) (string, error) {
if strings.HasPrefix(ref, "http://") || strings.HasPrefix(ref, "https://") || strings.HasPrefix(ref, "data:") {
return ref, nil
}
path := strings.TrimPrefix(ref, "file://")
if u, err := url.Parse(ref); err == nil && u.Scheme == "file" {
path = filePath(u)
if wsl != nil {
path = u.Path
}
}
if wsl != nil {
path = wsl.local(path)
}
if !filepath.IsAbs(path) {
path = filepath.Join(project, path)
}
b, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("reference image: %w", err)
}
mt := mime.TypeByExtension(strings.ToLower(filepath.Ext(path)))
if !strings.HasPrefix(mt, "image/") {
mt = http.DetectContentType(b)
}
if !strings.HasPrefix(mt, "image/") {
return "", fmt.Errorf("reference image %s isn't an image (%s)", ref, mt)
}
return "data:" + mt + ";base64," + base64.StdEncoding.EncodeToString(b), nil
}
var unslug = regexp.MustCompile(`[^a-z0-9]+`)
// target is where the i-th of n images goes: path as a file (numbered when
// n > 1) or a folder, else generated-images/<words of the prompt>-<time>.
// A name that is taken is given a number, never written over.
func target(project, path, prompt, ext string, i, n int) (string, error) {
dir, name := filepath.Join(project, folderFor(ext)), ""
if path != "" {
p := path
if wsl != nil {
p = wsl.local(p)
}
if strings.HasPrefix(p, "~/") {
home, _ := os.UserHomeDir()
p = filepath.Join(home, p[2:])
} else if !filepath.IsAbs(p) {
p = filepath.Join(project, p)
}
if st, err := os.Stat(p); (err == nil && st.IsDir()) || strings.HasSuffix(path, "/") || filepath.Ext(p) == "" {
dir = p
} else {
dir, name = filepath.Dir(p), filepath.Base(p)
}
}
if name == "" {
slug := strings.Trim(unslug.ReplaceAllString(strings.ToLower(prompt), "-"), "-")
if len(slug) > 40 {
slug = strings.TrimRight(slug[:40], "-")
}
if slug == "" {
slug = "image"
}
name = slug + "-" + time.Now().Format("20060102-150405") + ext
}
base, e := strings.TrimSuffix(name, filepath.Ext(name)), filepath.Ext(name)
// the image is saved as what it is: hero.png that came back a JPEG is
// hero.jpg, and the answer says so; a name with no image extension,
// hero.v2, is given one
switch l := strings.ToLower(e); {
case l == ".jpeg" && ext == ".jpg", l == ext:
case imageExt[l], videoExt[l]:
e = ext
default:
base, e = name, ext
}
if n > 1 {
base = fmt.Sprintf("%s-%d", base, i+1)
}
out := filepath.Join(dir, base+e)
for k := 2; ; k++ {
if _, err := os.Stat(out); errors.Is(err, os.ErrNotExist) {
return out, nil
}
out = filepath.Join(dir, fmt.Sprintf("%s-%d%s", base, k, e))
}
}
// imageExt are the extensions of the formats an image comes back in, videoExt
// those a video may be given.
var (
imageExt = map[string]bool{".png": true, ".jpg": true, ".jpeg": true, ".webp": true, ".gif": true}
videoExt = map[string]bool{".mp4": true, ".mov": true, ".webm": true, ".m4v": true}
)
// folderFor is where a file of ext goes when no path says.
func folderFor(ext string) string {
if videoExt[ext] {
return VideoFolder
}
return Folder
}
func extOf(mt string) string {
switch mt {
case "image/jpeg":
return ".jpg"
case "image/webp":
return ".webp"
case "image/gif":
return ".gif"
}
return ".png"
}
type filmArgs struct {
Prompt string `json:"prompt"`
Path string `json:"path"`
Seconds any `json:"seconds"`
Size string `json:"size"`
Image string `json:"image"`
References []string `json:"reference_images"`
}
// wholeSeconds is a clip length as an agent may give it: 6, 6.0 or "6".
func wholeSeconds(v any) (int, error) {
switch n := v.(type) {
case nil:
return 0, nil
case float64:
if n != float64(int(n)) || n < 1 {
return 0, fmt.Errorf("seconds is a whole number from 1 to 15, not %v", n)
}
return int(n), nil
case string:
if strings.TrimSpace(n) == "" {
return 0, nil
}
i, err := strconv.Atoi(strings.TrimSpace(n))
if err != nil || i < 1 {
return 0, fmt.Errorf("seconds is a whole number from 1 to 15, not %q", n)
}
return i, nil
}
return 0, fmt.Errorf("seconds is a whole number from 1 to 15, not %v", v)
}
// call asks the gateway, as the agent.
func (s *server) call(method, path string, body []byte) ([]byte, int, error) {
r, _ := http.NewRequest(method, s.gateway+path, bytes.NewReader(body))
if body != nil {
r.Header.Set("Content-Type", "application/json")
}
r.Header.Set("Authorization", "Bearer "+gateway.Token)
s.mu.Lock()
r.Header.Set("User-Agent", s.agent)
s.mu.Unlock()
res, err := s.client.Do(r)
if err != nil {
return nil, 0, fmt.Errorf("magpie isn't answering at %s (is it running?): %w", s.gateway, err)
}
defer res.Body.Close()
data, _ := io.ReadAll(io.LimitReader(res.Body, 512<<20))
return data, res.StatusCode, nil
}
// gatewaySaid is what the gateway's error answer says, or its body.
func gatewaySaid(data []byte) string {
var e struct {
Error *struct {
Message string `json:"message"`
} `json:"error"`
}
if json.Unmarshal(data, &e) == nil && e.Error != nil && e.Error.Message != "" {
return e.Error.Message
}
return strings.TrimSpace(string(data))
}
// film asks the gateway to make a video, waits for it and saves it; what
// it says is where it is.
func (s *server) film(raw json.RawMessage) (string, error) {
var a filmArgs
if err := json.Unmarshal(raw, &a); err != nil {
return "", err
}
if strings.TrimSpace(a.Prompt) == "" {
return "", errors.New("prompt is required")
}
secs, err := wholeSeconds(a.Seconds)
if err != nil {
return "", err
}
project := s.project()
req := map[string]any{"prompt": a.Prompt}
if secs > 0 {
req["seconds"] = strconv.Itoa(secs)
}
if a.Size != "" {
req["size"] = a.Size
}
if a.Image != "" {
src, err := reference(project, a.Image)
if err != nil {
return "", err
}
req["input_reference"] = map[string]string{"image_url": src}
}
if len(a.References) > 0 {
var refs []map[string]string
for _, ref := range a.References {
src, err := reference(project, ref)
if err != nil {
return "", err
}
refs = append(refs, map[string]string{"image_url": src})
}
req["reference_images"] = refs
}
body, _ := json.Marshal(req)
data, code, err := s.call(http.MethodPost, "/v1/videos", body)
if err != nil {
return "", err
}
if code != 200 {
return "", fmt.Errorf("no video: %s", gatewaySaid(data))
}
var v struct {
ID string `json:"id"`
Status string `json:"status"`
Model string `json:"model"`
Seconds string `json:"seconds"`
Error *struct {
Message string `json:"message"`
} `json:"error"`
}
if json.Unmarshal(data, &v) != nil || v.ID == "" {
return "", fmt.Errorf("no video: the gateway's answer has no id: %s", gatewaySaid(data))
}
id, deadline := v.ID, time.Now().Add(videoWait)
for v.Status != "completed" {
if v.Status == "failed" {
msg := "it gave no reason"
if v.Error != nil && v.Error.Message != "" {
msg = v.Error.Message
}
return "", fmt.Errorf("the video failed: %s", msg)
}
if time.Now().After(deadline) {
return "", fmt.Errorf("the video (%s) wasn't ready after %s; ask the gateway for GET /v1/videos/%s later", id, videoWait, id)
}
time.Sleep(videoPollEvery)
data, code, err = s.call(http.MethodGet, "/v1/videos/"+id, nil)
if err != nil {
return "", err
}
if code != 200 {
return "", fmt.Errorf("asking after the video (%s): %s", id, gatewaySaid(data))
}
prev := v.Model
v.Error = nil
if json.Unmarshal(data, &v) != nil {
return "", fmt.Errorf("asking after the video (%s): the answer isn't JSON", id)
}
if v.Model == "" {
v.Model = prev
}
}
mp4, code, err := s.call(http.MethodGet, "/v1/videos/"+id+"/content", nil)
if err != nil {
return "", err
}
if code != 200 {
return "", fmt.Errorf("fetching the video (%s): %s", id, gatewaySaid(mp4))
}
if ct := http.DetectContentType(mp4); !strings.HasPrefix(ct, "video/") {
return "", fmt.Errorf("what came back for the video (%s) isn't a video (%s)", id, ct)
}
path, err := target(project, a.Path, a.Prompt, ".mp4", 0, 1)
if err != nil {
return "", err
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return "", err
}
if err := os.WriteFile(path, mp4, 0o644); err != nil {
return "", err
}
length := ""
if v.Seconds != "" {
length = v.Seconds + "-second "
}
return fmt.Sprintf("Generated a %svideo with %s, saved to:\n- %s\n", length, v.Model, shownPath(path)), nil
}