The owner, 11-05: Magpie Browser interrupts the user's own browsing, and driving Discord with it gets the account flagged by Discord's bot check, which never happened under Claude's Chrome extension. What a site could see, and what changed (extension 0.1.6): - CDP's Runtime domain was on in every tab, which bot checks look for. It is now off until browser_console is asked for (on localhost, 127.x, [::1], *.localhost and *.test it is on from the start, as before). The first console call says it reads the console from then on. - Every read and helper (the clickable scan's getComputedStyle and querySelectorAll on each element, focus, readyState, innerText, scroll position) ran in the page's own world, where a page's patched functions see each call. They now run in an isolated world of the extension's (Page.createIsolatedWorld, DOM.resolveNode in it). browser_evaluate stays in the page's world, since the agent's script is for the page's JavaScript. - A click jumped to an element's exact middle. The mouse now moves along a curved, eased path to a spot drawn around the middle, then presses and lets go at uneven timings. Hover and drag go the same way. - Text went in with Input.insertText, with no key events. It is now typed a key at a time with US key codes, Shift held for capitals and symbols, at a typist's uneven rhythm (35s at most for a whole text). Chinese, Japanese and Korean come through an IME composition, emoji through insertText, and a newline as Shift+Enter so a chat box doesn't send early. A field's old text is cleared with Backspace only when it has some, and submit waits a moment before Enter. - A scroll was one wheel event. It is several notches now. - Actions follow each other at a person's pace (350 to 1600ms apart), and the debugger stays attached for 5 minutes idle, not 2, so Chrome's bar doesn't come and go between calls. The user's tabs: - A call naming one of the user's own tabs (one outside every Magpie group) now opens that page in a background tab of the agent's own, in its group, and the agent is told so; naming it again goes on in that copy. This is how Claude's extension keeps to its own group. - tab_close refuses a tab of the user's. - A tab an action opens always goes in the agent's group, and a page that opens a window of its own gives focus back to the window the user was in. - The tool's instructions said calls go to the tab the user is looking at. They now say the agent works in tabs of its own, that naming the user's tab copies it, that steps take a person's time, and how the console starts. Run at this head (rebased onfe61503e6), on mote with Chromium 1248 (Chrome for Testing, --headless=new): - gofmt -l clean; go vet for darwin, linux and windows on internal/gateway and internal/browserext - go test ./internal/browserext: ok - go test -run TestBrowser ./internal/gateway, with MAGPIE_BROWSER_CHROME: 21 of 21 pass, the six real-browser tests among them - New TestBrowserActsLikeAPerson, on shop.example (mapped to the test's site, so not a developer's host). It checks that the page never sees its getComputedStyle, querySelectorAll, elementFromPoint or focus called. It checks that a click is trusted, off the middle, after 4 or more mousemoves. It checks a keydown per character at uneven gaps, Shift for capitals, and Chinese through compositionend, both in a textarea and in a contenteditable chat box. In that chat box a newline doesn't send and submit does. It also checks that a 600px scroll is 3 or more wheel events, that the console is off until asked for, and that a named user tab is copied into the group and left as it was. Broken by hand, each red in that test: - Runtime always on: console says nothing about starting - a click at the exact middle with one mousemove: "landed on the middle (-0.5, -0.375)" and "moved 1 times" - insertText for typing: 0 keydowns, even gaps, no Shift, no composition - the clickable scan in the page's world: the page saw 27 calls - naming the user's tab acting in it: "doesn't say is the user's own tab" Not tried: - The real Discord or a real account. Whether Discord's check now passes is not something a test here shows. The reasons it is likely to are the differences above, and that they match what Claude's extension does (Runtime only when asked, reads outside the page's world, a move before each click). - Headful (MAGPIE_BROWSER_HEADFUL=1) on mote. TestBrowserLeavesTheUsersTabAlone fails at random steps there on origin/mainfe61503e6too, even right after a plain navigate, because the window counts as hidden on mote's idle display (#5513). - Edge, Windows and Linux Chrome. The user reloads Magpie Browser in chrome://extensions to get 0.1.6.
140 lines
10 KiB
Go
140 lines
10 KiB
Go
package gateway
|
|
|
|
// The extension's tools (internal/browserext/ext/background.js), whose
|
|
// argument names these schemas give. The server lists one tool, browser
|
|
// (browser_one.go), whose actions are these; their names are still taken
|
|
// from a client that listed them before.
|
|
|
|
const browserInstructions = `The browser tool uses the user's own browser (Chrome, Edge or another Chromium browser with Magpie Browser installed): its real tabs, signed in to the sites the user is signed in to. Use it when a task needs the user's sessions or a real browser.
|
|
How to work:
|
|
- Start with tab_new (with a url) or snapshot. You work in tabs of your own, opened behind the user's in your own tab group ("Magpie", or "Magpie 2" and on beside other agents), so the user goes on using their browser undisturbed. Naming one of the user's own tabs opens its page in a tab of yours, signed in the same; theirs is left alone. Leave the tabs that tabs marks ~ to the agents working in them.
|
|
- Clicks, typing and scrolling go at a person's pace, so a step takes a second or two: don't hurry them with repeated calls. console starts reading a tab's console the first time it is asked (except on localhost and other development hosts, read from the start).
|
|
- With two browsers connected (Chrome and Edge, or two profiles), calls go to the last to connect: action browsers lists them, and use_browser picks one.
|
|
- snapshot shows the page as an accessibility tree. Elements you can act on carry a ref (e12); pass it to click, type and the rest. Refs stay the element's while the page keeps it. Every action answers with the page after it, so you rarely need another snapshot.
|
|
- Frames from other sites (a payment form, a sign-in box, an embedded editor) are read and acted on like the rest of the page.
|
|
- A failure starts with its kind and says whether trying again can help: STALE_REF and AMBIGUOUS (take a snapshot, use a ref from it), UNACTIONABLE (the page must change first), BAD_ARGS, WAIT_TIMEOUT, SITE_REFUSED (the site turned the browser away: tell the user), LOAD_FAILED, PAGE_BLOCKED, USER_STOPPED, DIALOG_OPEN, SCRIPT_ERROR, and OUTCOME_UNKNOWN (the page changed mid-action: snapshot before doing it again). A click answered NO_EFFECT changed nothing on the page: check before clicking again.
|
|
- Prefer refs to evaluate. When the layout or an image matters, add screenshot: true to an action (a small picture of the window comes with its answer), or take a screenshot.
|
|
- This is the user's browser: you can close only tabs you opened, don't change their settings, and ask before sending, buying, posting or deleting anything on their behalf. When a site wants a sign-in, a captcha or a 2FA code, ask the user to do it in the tab, then go on.`
|
|
|
|
func browserTabArg() map[string]any {
|
|
return map[string]any{"type": "string", "description": "The tab to act in (t123, from browser_tabs). Without it: the tab this session works in, or a new one of its own behind the tab the user is looking at."}
|
|
}
|
|
|
|
func browserRefArg(what string) map[string]any {
|
|
return map[string]any{"type": "string", "description": what + " (a ref from the latest snapshot, like e12)"}
|
|
}
|
|
|
|
func browserTool(name, title, desc string, readOnly bool, props map[string]any, required ...string) map[string]any {
|
|
props["tab"] = browserTabArg()
|
|
schema := map[string]any{"type": "object", "properties": props}
|
|
if len(required) > 0 {
|
|
schema["required"] = required
|
|
}
|
|
return map[string]any{
|
|
"name": name, "title": title, "description": desc, "inputSchema": schema,
|
|
"annotations": map[string]any{"readOnlyHint": readOnly, "openWorldHint": true},
|
|
}
|
|
}
|
|
|
|
func browserTools() []any {
|
|
str := func(desc string) map[string]any { return map[string]any{"type": "string", "description": desc} }
|
|
boolean := func(desc string) map[string]any { return map[string]any{"type": "boolean", "description": desc} }
|
|
num := func(desc string) map[string]any { return map[string]any{"type": "number", "description": desc} }
|
|
enum := func(desc string, vals ...string) map[string]any {
|
|
return map[string]any{"type": "string", "enum": vals, "description": desc}
|
|
}
|
|
strs := func(desc string) map[string]any {
|
|
return map[string]any{"type": "array", "items": map[string]any{"type": "string"}, "description": desc}
|
|
}
|
|
return []any{
|
|
browserTool("browser_tabs", "Tabs", "List the browser's tabs, open a new one (in this session's Magpie tab group), switch the tab calls go to, or close one. With more than one browser connected: list them (browsers), or pick the one calls go to (use).", false, map[string]any{
|
|
"action": enum("list (default), new, select, close, browsers or use", "list", "new", "select", "close", "browsers", "use"),
|
|
"url": str("For new: the page to open in it."),
|
|
"front": boolean("For new: show it to the user, only when they asked to see it; it opens behind their tab otherwise."),
|
|
"browser": str("For use: the browser, by its id from browsers (b2) or its name (Edge)."),
|
|
}),
|
|
browserTool("browser_navigate", "Navigate", "Open a URL in the tab, or go back, forward or reload. Answers with the page once it has loaded.", false, map[string]any{
|
|
"url": str("The address to open (https:// is added when it has no scheme)."),
|
|
"go": enum("Instead of url: back, forward or reload.", "back", "forward", "reload"),
|
|
}),
|
|
browserTool("browser_snapshot", "Snapshot", "Read the page as an accessibility tree, each element you can act on with a ref.", true, map[string]any{
|
|
"interactive": boolean("Only the elements you can act on, and headings: a much shorter read of a long page."),
|
|
"ref": browserRefArg("Read only this part of the page"),
|
|
"maxChars": num("The most characters to return (default 60000)."),
|
|
}),
|
|
browserTool("browser_click", "Click", "Click an element, as the user would with the mouse.", false, map[string]any{
|
|
"ref": browserRefArg("The element"),
|
|
"double": boolean("Double-click."),
|
|
"button": enum("left (default), right or middle", "left", "right", "middle"),
|
|
"modifiers": map[string]any{"type": "array", "items": map[string]any{"type": "string", "enum": []string{"Alt", "Control", "Meta", "Shift"}}, "description": "Keys held while clicking."},
|
|
}, "ref"),
|
|
browserTool("browser_type", "Type", "Type text into a field or an editable element.", false, map[string]any{
|
|
"ref": browserRefArg("The field"),
|
|
"text": str("What to type."),
|
|
"clear": boolean("Replace what the field holds (default true); false adds to it."),
|
|
"submit": boolean("Press Enter after."),
|
|
"slowly": boolean("Type a key at a time, for a field that reacts to each key (an autocomplete)."),
|
|
}, "ref", "text"),
|
|
browserTool("browser_press", "Press key", "Press a key or a chord: Enter, Escape, Tab, ArrowDown, PageDown, Backspace, Control+a, Meta+Shift+k.", false, map[string]any{
|
|
"key": str("The key or chord."),
|
|
"ref": browserRefArg("Focus this element first"),
|
|
}, "key"),
|
|
browserTool("browser_hover", "Hover", "Move the mouse over an element, to open a menu or a tooltip.", false, map[string]any{
|
|
"ref": browserRefArg("The element"),
|
|
}, "ref"),
|
|
browserTool("browser_select", "Select option", "Choose options in a native select list, by value or label.", false, map[string]any{
|
|
"ref": browserRefArg("The list"),
|
|
"values": strs("The options to choose."),
|
|
}, "ref", "values"),
|
|
browserTool("browser_scroll", "Scroll", "Scroll the page or an element, or bring an element into view (ref with no direction).", false, map[string]any{
|
|
"direction": enum("down (default), up, left or right", "down", "up", "left", "right"),
|
|
"amount": num("Pixels; default most of the window."),
|
|
"ref": browserRefArg("Scroll inside this element, or into view without a direction"),
|
|
}),
|
|
browserTool("browser_drag", "Drag", "Drag one element onto another.", false, map[string]any{
|
|
"from": browserRefArg("What to drag"),
|
|
"to": browserRefArg("Where to drop it"),
|
|
}, "from", "to"),
|
|
browserTool("browser_upload", "Upload files", "Choose files in a file field, or in the picker a button opens.", false, map[string]any{
|
|
"ref": browserRefArg("The file field or the button that opens the picker"),
|
|
"paths": strs("The files' full paths on this computer."),
|
|
}, "ref", "paths"),
|
|
browserTool("browser_wait", "Wait", "Wait for text to show or go, for the address to change, or a number of seconds.", true, map[string]any{
|
|
"text": str("Wait until the page shows this text."),
|
|
"textGone": str("Wait until the page no longer shows this text."),
|
|
"url": str("Wait until the address contains this."),
|
|
"seconds": num("Just wait this long (at most 60)."),
|
|
"timeout": num("Give up after this many seconds (default 10, at most 60)."),
|
|
}),
|
|
browserTool("browser_screenshot", "Screenshot", "Take a screenshot of the window, the whole page or one element.", true, map[string]any{
|
|
"ref": browserRefArg("Only this element"),
|
|
"fullPage": boolean("The whole page, not only what the window shows."),
|
|
"png": boolean("PNG instead of JPEG."),
|
|
}),
|
|
browserTool("browser_evaluate", "Run script", "Run JavaScript in the page and return its JSON result. A function is called (with the element when ref is given); an expression is evaluated.", false, map[string]any{
|
|
"script": str("An expression, or a function like () => document.title or (el) => el.value."),
|
|
"ref": browserRefArg("The element the function is called with"),
|
|
}, "script"),
|
|
browserTool("browser_text", "Page text", "Read the page's visible text, without the tree.", true, map[string]any{
|
|
"maxChars": num("The most characters to return (default 40000)."),
|
|
}),
|
|
browserTool("browser_console", "Console", "Read the tab's console messages, script errors and failed requests since magpie started watching it.", true, map[string]any{
|
|
"level": enum("all (default), errors or network", "all", "errors", "network"),
|
|
"limit": num("The most recent messages to return (default 50)."),
|
|
}),
|
|
browserTool("browser_dialog", "Dialog", "Answer the page's alert, confirm or prompt dialog; with none open, answer the next one.", false, map[string]any{
|
|
"accept": boolean("Accept (default true) or dismiss."),
|
|
"promptText": str("For a prompt: the text to answer with."),
|
|
}),
|
|
}
|
|
}
|
|
|
|
func browserToolNamed(name string) bool {
|
|
for _, t := range browserTools() {
|
|
if t.(map[string]any)["name"] == name {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|