Asked for in chat ("Magpie Browser MCP 也要支持录屏哦"), like
claude-in-chrome's gif_creator. The browser tool gets two actions:
- record_start: from now on, after each action that can change the page
(not tabs, snapshot, text, console, screenshot, tab_close), the gateway
takes a picture of the session's window, 800 wide, with the action and
what it was done to written in a strip under it (the url, ref, key or
drag; never what was typed).
- record_stop: the frames as an animated GIF, a second a frame and the
last three, each frame after the first only the rectangle that changed.
It is kept an hour behind the image server's tokened files link and
named in FilesMeta, so `magpie tool` saves it; the text says to curl it.
The gateway does it all through browser_screenshot, so every extension
version records and nobody has to reload theirs. At most 300 frames;
a recording nobody stops goes after 30 minutes. A picture an action's
screenshot: true asks for is the same one the frame is made from.
Run on mote (macOS, Chrome for Testing 1234 headless):
- GOOS=darwin/linux/windows go vet ./internal/gateway, gofmt -l: clean.
- MAGPIE_BROWSER_CHROME=... go test ./internal/gateway -count=1
-timeout 45m: ok (794s).
- New: TestBrowserRecordsAGIF (real Chromium: navigate, type, snapshot,
navigate → 3 frames, GIF fetched from the link, 800 wide),
TestBrowserGIFFramesHoldWhatChanged, TestBrowserRecSayKeepsTypedTextOut,
TestBrowserRecordStopWithNothing.
Broken by hand: with browserRecWorth always false, TestBrowserRecordsAGIF
is red ("no frames").
Not tried: a real headful Chrome, Edge or Arc; Windows and Linux browsers.
150 lines
5.4 KiB
Go
150 lines
5.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"image"
|
|
"image/color"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/browserext"
|
|
)
|
|
|
|
// Every action is one of the extension's tools, and the one schema names
|
|
// every argument the eighteen take, so none is lost in the merge.
|
|
func TestBrowserActionsCoverTheTools(t *testing.T) {
|
|
src, err := browserext.File("background.js")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(browserActionOrder) != len(browserActions) {
|
|
t.Fatalf("%d actions listed, %d known", len(browserActionOrder), len(browserActions))
|
|
}
|
|
used := map[string]bool{}
|
|
for _, name := range browserActionOrder {
|
|
a, ok := browserActions[name]
|
|
if !ok {
|
|
t.Fatalf("%s is listed but unknown", name)
|
|
}
|
|
if a.tool == browserRecordTool {
|
|
continue // the gateway's own
|
|
}
|
|
if !strings.Contains(string(src), " async "+a.tool+"(session, args) {") {
|
|
t.Errorf("%s's %s isn't in background.js", name, a.tool)
|
|
}
|
|
used[a.tool] = true
|
|
}
|
|
props := browserOneTool()["inputSchema"].(map[string]any)["properties"].(map[string]any)
|
|
desc := browserOneTool()["description"].(string)
|
|
for _, tl := range browserTools() {
|
|
m := tl.(map[string]any)
|
|
name := m["name"].(string)
|
|
if !used[name] {
|
|
t.Errorf("no action carries out %s", name)
|
|
}
|
|
for arg := range m["inputSchema"].(map[string]any)["properties"].(map[string]any) {
|
|
if arg == "action" || arg == "go" { // they are actions of their own
|
|
continue
|
|
}
|
|
if props[arg] == nil {
|
|
t.Errorf("%s's %s isn't in browser's schema", name, arg)
|
|
}
|
|
}
|
|
}
|
|
for _, name := range browserActionOrder {
|
|
if !strings.Contains(desc, name) {
|
|
t.Errorf("browser's description doesn't name %s", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The point of one tool: its listing is a fraction of the eighteen's.
|
|
func TestBrowserToolListIsSmall(t *testing.T) {
|
|
one, _ := json.Marshal([]any{browserOneTool()})
|
|
all, _ := json.Marshal(browserTools())
|
|
if len(one)*2 > len(all) {
|
|
t.Fatalf("browser's listing is %d bytes, the eighteen's %d: no saving", len(one), len(all))
|
|
}
|
|
t.Logf("browser: %d bytes, the eighteen: %d", len(one), len(all))
|
|
}
|
|
|
|
// A browser call reaches the extension as its own tool with the arguments
|
|
// the action stands for; its answer names actions, not the eighteen; and
|
|
// screenshot: true adds a small picture of the window.
|
|
func TestBrowserToolCallsTheExtensionsTool(t *testing.T) {
|
|
fresh(t)
|
|
s := New()
|
|
srv := httptest.NewServer(lanGuard(s.Handler()))
|
|
t.Cleanup(srv.Close)
|
|
type got struct {
|
|
tool string
|
|
args map[string]any
|
|
}
|
|
calls := make(chan got, 8)
|
|
big := image.NewRGBA(image.Rect(0, 0, 2880, 1800)) // a Retina window
|
|
for i := range big.Pix {
|
|
big.Pix[i] = 0xff
|
|
}
|
|
big.Set(10, 10, color.Black)
|
|
var raw bytes.Buffer
|
|
png.Encode(&raw, big)
|
|
fakeExtension(t, srv.URL, func(_, _, tool string, args map[string]any) any {
|
|
calls <- got{tool, args}
|
|
if tool == "browser_screenshot" {
|
|
return map[string]any{"content": []any{map[string]any{"type": "image", "data": base64.StdEncoding.EncodeToString(raw.Bytes()), "mimeType": "image/png"}}}
|
|
}
|
|
return map[string]any{"content": []any{map[string]any{"type": "text", "text": "Opened t1. Answer it with browser_dialog; call browser_tabs with action new."}}}
|
|
})
|
|
browserConnected(t, s)
|
|
h := lanGuard(s.Handler())
|
|
|
|
m := readImageReply(t, browserPost(h, "127.0.0.1:5000", imageToolCall("browser", map[string]any{"action": "tab_new", "url": "example.com"})))
|
|
c := <-calls
|
|
if c.tool != "browser_tabs" || c.args["action"] != "new" || c.args["url"] != "example.com" || m.Result.IsError {
|
|
t.Fatalf("tab_new reached the extension as %+v: %+v", c, m)
|
|
}
|
|
if say := m.Result.Content[0].Text; strings.Contains(say, "browser_") || !strings.Contains(say, "the dialog action") || !strings.Contains(say, "the tab_new action") {
|
|
t.Fatalf("the answer names the eighteen: %q", say)
|
|
}
|
|
if len(m.Result.Content) != 1 {
|
|
t.Fatalf("a picture nobody asked for: %+v", m.Result.Content)
|
|
}
|
|
|
|
m = readImageReply(t, browserPost(h, "127.0.0.1:5000", imageToolCall("browser", map[string]any{"action": "back", "screenshot": true})))
|
|
c = <-calls
|
|
if c.tool != "browser_navigate" || c.args["go"] != "back" || c.args["screenshot"] != nil || c.args["action"] != nil {
|
|
t.Fatalf("back reached the extension as %+v", c)
|
|
}
|
|
if c = <-calls; c.tool != "browser_screenshot" {
|
|
t.Fatalf("then %+v, not a screenshot", c)
|
|
}
|
|
if len(m.Result.Content) != 2 || m.Result.Content[1].Type != "image" || m.Result.Content[1].MimeType != "image/jpeg" {
|
|
t.Fatalf("no picture with the answer: %+v", m.Result.Content)
|
|
}
|
|
b, _ := base64.StdEncoding.DecodeString(m.Result.Content[1].Data)
|
|
cfg, err := jpeg.DecodeConfig(bytes.NewReader(b))
|
|
if err != nil || cfg.Width != browserShotWidth || cfg.Height != 640 {
|
|
t.Fatalf("the picture is %+v (%v), want %dx640", cfg, err, browserShotWidth)
|
|
}
|
|
|
|
m = readImageReply(t, browserPost(h, "127.0.0.1:5000", imageToolCall("browser", map[string]any{"action": "format_disk"})))
|
|
if !m.Result.IsError || !strings.HasPrefix(m.Result.Content[0].Text, "BAD_ARGS: action is one of tabs,") {
|
|
t.Fatalf("an unknown action: %+v", m)
|
|
}
|
|
// a client that listed the eighteen before still calls them
|
|
readImageReply(t, browserPost(h, "127.0.0.1:5000", imageToolCall("browser_snapshot", nil)))
|
|
if c = <-calls; c.tool != "browser_snapshot" {
|
|
t.Fatalf("browser_snapshot reached the extension as %+v", c)
|
|
}
|
|
select {
|
|
case c = <-calls:
|
|
t.Fatalf("an extra call %+v", c)
|
|
default:
|
|
}
|
|
}
|