Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

18.0 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Page fetch/XHR wrappers run inside boot; count in-flight requests, route own-site calls via panel."""

BROWSER_NETWORK_JS = r"""
        const nativeFetch = window.fetch.bind(window);
        const NativeXHR = window.XMLHttpRequest;
        // Start times of the page's requests in flight, so the agent can wait for content still coming.
        // The wall clock, which the panel shares, captured before a page script can replace it.
        const clock = Date.now.bind(Date);
        const inflight = new Set();
        const track = () => { const entry = { at: clock() }; inflight.add(entry); return () => inflight.delete(entry); };
        // Only those started since the agent's action: a long poll the page already had open isn't its.
        Object.defineProperty(window, Symbol.for("unsloth.browser.pending"), {
          value: (since) => { let n = 0; for (const r of inflight) if (r.at >= since) n++; return n; },
        });
        // fetch() resolves at the headers; a list is drawn only once its body is read, so a read counts too.
        // The page calls it in the same microtask run as fetch resolves, before any agent command arrives.
        // A body streamed through response.body.getReader() is not counted.
        for (const name of ["arrayBuffer", "blob", "bytes", "formData", "json", "text"]) {
          const read = Response.prototype[name];
          if (typeof read !== "function") continue;
          Response.prototype[name] = function (...args) {
            const reading = read.apply(this, args);
            return reading.finally(track());
          };
        }

        // To the browser the sandbox's origin is opaque, so even the page's own site is another origin,
        // and an API of it that sends no CORS headers can't be read. Requests to the page's origin go
        // through the panel to Unsloth's backend (POST /api/browser/request), which makes them as the
        // same-origin requests they are. Requests to other sites stay the browser's. When the page's
        // site answers any origin (cfg.anyOrigin), its writes and XHRs stay the browser's too, streamed
        // and unbuffered; a fetch GET still falls back to the panel for an API that doesn't.
        const pageOrigin = (() => { try { return cfg.url ? new URL(cfg.url).origin : null; } catch { return null; } })();
        const ownSite = (url) => { try { return pageOrigin !== null && new URL(url).origin === pageOrigin; } catch { return false; } };
        const replies = new Map();
        let lastRequest = 0;
        window.addEventListener("message", (event) => {
          const data = event.data;
          if (event.source !== parent || !data || data.type !== "unsloth:browser-command" || data.command !== "response") return;
          replies.get(data.id)?.(data);
        }, true);
        // The panel's { status, statusText, url, headers, body }. A failure rejects as fetch's network
        // error does; an abort rejects with the signal's reason, and the panel stops the request.
        const relay = (request, signal) => new Promise((resolve, reject) => {
          if (signal.aborted) { reject(signal.reason); return; }
          const id = "q" + ++lastRequest;
          const settle = () => { replies.delete(id); signal.removeEventListener("abort", onAbort); };
          const onAbort = () => { settle(); post({ type: "requestAbort", id }); reject(signal.reason); };
          signal.addEventListener("abort", onAbort);
          replies.set(id, (reply) => {
            settle();
            if (reply.ok === true) resolve(reply);
            else reject(new TypeError("Failed to fetch"));
          });
          post({ type: "request", id, ...request });
        });
        const bodiless = (method) => method === "GET" || method === "HEAD";
        // A GET or HEAD tries the browser first: where the site already answers the sandbox's origin
        // (an API sending ACAO null or *), streams, long polls and large bodies stay as they were. One
        // the browser can't read goes to the panel, and that address goes there directly from then on.
        const needsRelay = new Set();
        const address = (url) => { const parts = new URL(url); return parts.origin + parts.pathname; };
        const fromBrowser = async (request) => {
          if (!bodiless(request.method) || needsRelay.has(address(request.url))) return null;
          try { return await nativeFetch(request); }
          catch (error) {
            if (request.signal.aborted) throw error;
            needsRelay.add(address(request.url));
            return null;
          }
        };
        const NULL_BODY_STATUSES = new Set([101, 103, 204, 205, 304]);
        // The panel's limit (frame-message.ts, routes/browser.py); strings counted at 3 bytes a unit.
        const MAX_RELAY_BODY = 1024 * 1024;
        const bodySize = (body) => typeof body === "string" ? body.length * 3
          : body instanceof Blob ? body.size
          : body instanceof ArrayBuffer || ArrayBuffer.isView(body) ? body.byteLength
          // Multipart adds a boundary and headers per entry: 512 bytes each, plus names, is ample.
          : body instanceof FormData ? [...body.entries()].reduce((total, [name, value]) => total + 512 + name.length * 3
            + (typeof value === "string" ? value.length * 3 : value.size + (value.name?.length ?? 0) * 3), 0)
          : body instanceof URLSearchParams ? body.toString().length * 3
          : body instanceof Document ? (body.documentElement?.outerHTML?.length ?? 0) * 3
          : 0;
        const withoutHash = (url) => url.split("#")[0];
        // What a fetched response has and a constructed one lacks, kept through clone(): "basic" on
        // the page's origin, "cors" once a redirect led through another site.
        const asFetched = (response, url, redirected, type) => {
          const clone = response.clone;
          return Object.defineProperties(response, {
            url: { value: url },
            redirected: { value: redirected },
            type: { value: type },
            clone: { value: () => asFetched(clone.call(response), url, redirected, type) },
          });
        };
        // The body of a copy, read only up to the relay's limit: undefined past it, so a large upload
        // is never buffered whole before it goes to the browser.
        const readCapped = async (request) => {
          const reader = request.clone().body?.getReader();
          if (!reader) return new ArrayBuffer(0);
          // The request's signal doesn't reach a copy's reader: an abort cancels it here, so a stalled
          // stream can't keep the fetch pending.
          const signal = request.signal;
          const onAbort = () => reader.cancel(signal.reason).catch(() => {});
          signal.addEventListener("abort", onAbort);
          const chunks = [];
          let size = 0;
          try {
            for (;;) {
              if (signal.aborted) throw signal.reason;
              const { done, value } = await reader.read();
              if (signal.aborted) throw signal.reason;
              if (done) break;
              size += value.byteLength;
              if (size > MAX_RELAY_BODY) { reader.cancel().catch(() => {}); return undefined; }
              chunks.push(value);
            }
          } finally {
            signal.removeEventListener("abort", onAbort);
          }
          const bytes = new Uint8Array(size);
          let at = 0;
          for (const chunk of chunks) { bytes.set(chunk, at); at += chunk.byteLength; }
          return bytes.buffer;
        };
        const relayFetch = async (request) => {
          const body = bodiless(request.method) ? null : await readCapped(request);
          // Past the relay's body limit a write stays the browser's, as before relaying existed.
          if (body === undefined) return nativeFetch(request);
          const reply = await relay(
            { url: request.url, method: request.method, headers: [...request.headers], body, redirect: request.redirect, integrity: request.integrity },
            request.signal,
          );
          // redirect: "manual" stopped at a redirect, which a page sees only as an opaque one.
          if (reply.opaqueRedirect === true) return asFetched(Response.error(), withoutHash(request.url), false, "opaqueredirect");
          const headers = new Headers();
          for (const [name, value] of reply.headers) try { headers.append(name, value); } catch {}
          const content = request.method === "HEAD" || NULL_BODY_STATUSES.has(reply.status) ? null : reply.body;
          let response;
          // A reason phrase a Response refuses is left out; HTTP/2 has none anyway.
          try { response = new Response(content, { status: reply.status, statusText: reply.statusText, headers }); }
          catch { response = new Response(content, { status: reply.status, headers }); }
          const url = withoutHash(reply.url);
          return asFetched(response, url, reply.redirected === true, reply.cors === true ? "cors" : "basic");
        };
        window.fetch = (input, init) => {
          const done = track();
          // Relative addresses resolve against the page's <base>, as the browser's would. Other inputs
          // (a Request from another frame) are left to the browser.
          let url = null;
          try {
            if (input instanceof Request) url = input.url;
            else if (typeof input === "string" || input instanceof URL) url = new URL(input, document.baseURI).href;
          } catch {}
          if (!ownSite(url)) return nativeFetch(input, init).finally(done);
          let request;
          try { request = new Request(input instanceof Request ? input : url, init); } catch (error) { done(); return Promise.reject(error); }
          if (cfg.anyOrigin && !bodiless(request.method)) return nativeFetch(request).finally(done);
          return fromBrowser(request).then((response) => response ?? relayFetch(request)).finally(done);
        };

        // An XHR to the page's site stays OPENED while the panel makes it, then the browser loads the
        // answer from a blob: URL, so events, responseType and decoding are the browser's own; status,
        // headers and URL read as the site's. A synchronous XHR can't wait, so it stays the browser's.
        const relayed = new WeakMap();
        const headerMap = (list) => {
          const merged = new Map();
          for (const [name, value] of list) {
            const key = name.toLowerCase();
            merged.set(key, merged.has(key) ? merged.get(key) + ", " + value : value);
          }
          return merged;
        };
        const nativeReadyState = (xhr) => Reflect.get(NativeXHR.prototype, "readyState", xhr);
        // The site's answer, once the browser has loaded its headers.
        const answered = (xhr) => {
          const state = relayed.get(xhr);
          return state?.reply && nativeReadyState(xhr) >= 2 ? state.reply : null;
        };
        window.XMLHttpRequest = class extends NativeXHR {
          constructor() {
            super();
            // send() fires loadstart; the blob: load's own would be a second. Added before any of the
            // page's listeners, so stopping it here keeps it from all of them.
            this.addEventListener("loadstart", (event) => {
              if (event.isTrusted && relayed.get(this)?.reply) event.stopImmediatePropagation();
            }, true);
          }
          // open() ends a request in flight without a loadend, so its count ends here too.
          open(...args) {
            this._done?.();
            relayed.get(this)?.controller?.abort();
            relayed.delete(this);
            super.open(...args);
            const url = args.length > 1 ? resolve(String(args[1])) : null;
            const async = args.length < 3 || Boolean(args[2]);
            if (async && ownSite(url) && !cfg.anyOrigin) relayed.set(this, { method: String(args[0]).toUpperCase(), url, headers: [] });
          }
          setRequestHeader(name, value) {
            super.setRequestHeader(name, value);
            relayed.get(this)?.headers.push([String(name), String(value)]);
          }
          send(body) {
            const state = relayed.get(this);
            // As the browser's: one already sent, or ended, can't be sent again before open().
            if (state && (state.controller || state.readyState !== undefined)) {
              throw new DOMException("The object's state must be OPENED.", "InvalidStateError");
            }
            const done = track();
            // Listening first: a synchronous request fires loadend inside send().
            this.addEventListener("loadend", done, { once: true });
            if (state && bodySize(body) <= MAX_RELAY_BODY) this.#relay(state, body);
            else {
              relayed.delete(this);
              // A send the browser refuses (one already in flight) leaves that one counted.
              try { super.send(body); } catch (error) { this.removeEventListener("loadend", done); done(); throw error; }
            }
            if (this._done !== done) this._done?.();
            this._done = done;
          }
          #relay(state, body) {
            const controller = (state.controller = new AbortController());
            // XHR's request error steps: DONE, then readystatechange, the error's event and loadend; an
            // abort leaves it UNSENT.
            const fail = (type) => {
              if (relayed.get(this) !== state || state.reply || state.readyState !== undefined) return;
              clearTimeout(timer);
              controller.abort();
              state.readyState = 4;
              this.dispatchEvent(new Event("readystatechange"));
              this.dispatchEvent(new ProgressEvent(type));
              this.dispatchEvent(new ProgressEvent("loadend"));
              if (type === "abort") state.readyState = 0;
            };
            const timer = this.timeout > 0 ? setTimeout(() => fail("timeout"), this.timeout) : 0;
            state.fail = fail;
            this.dispatchEvent(new ProgressEvent("loadstart"));
            // The body as bytes, with the Content-Type the browser would give it.
            const bytes = bodiless(state.method) || body == null ? Promise.resolve(null) : (async () => {
              // XHR serializes a Document, which Fetch's bodies don't take, as UTF-8 HTML or XML.
              const html = body instanceof Document && body.contentType === "text/html";
              const content = !(body instanceof Document) ? body
                : html ? (body.doctype ? "<!DOCTYPE " + body.doctype.name + ">" : "") + (body.documentElement?.outerHTML ?? "")
                : new XMLSerializer().serializeToString(body);
              const request = new Request(state.url, { method: "POST", body: content });
              const type = !(body instanceof Document) ? request.headers.get("content-type")
                : (html ? "text/html" : "application/xml") + ";charset=UTF-8";
              const own = state.headers.filter(([name]) => name.toLowerCase() === "content-type");
              if (!own.length) { if (type) state.headers.push(["Content-Type", type]); }
              // Sent as UTF-8, so the browser relabels the page's charset as UTF-8, other parameters kept.
              else if (typeof content === "string" || content instanceof URLSearchParams) {
                for (const header of own) header[1] = header[1].replace(/(;\s*charset=)("?)[^;"]*/i, "$1$2UTF-8");
              }
              return request.arrayBuffer();
            })();
            // The browser's answer, when it can read one, is read whole like the panel's.
            const browserReply = async () => {
              let request;
              try { request = new Request(state.url, { method: state.method, headers: state.headers, signal: controller.signal }); } catch { return null; }
              const response = await fromBrowser(request);
              if (!response) return null;
              return { status: response.status, statusText: response.statusText, url: response.url, headers: [...response.headers], body: await response.arrayBuffer() };
            };
            bytes
              .then(async (content) => (content === null && (await browserReply()))
                || relay({ url: state.url, method: state.method, headers: state.headers, body: content, redirect: "follow" }, controller.signal))
              .then((reply) => {
                if (relayed.get(this) !== state || state.readyState !== undefined) return;
                clearTimeout(timer);
                const type = headerMap(reply.headers).get("content-type");
                const link = URL.createObjectURL(new Blob([reply.body], type ? { type } : {}));
                this.addEventListener("loadend", () => URL.revokeObjectURL(link), { once: true });
                state.reply = reply;
                super.open("GET", link);
                super.send();
              }, () => fail("error"));
          }
          abort() {
            const state = relayed.get(this);
            if (state?.fail && !state.reply) state.fail("abort");
            else super.abort();
          }
          get readyState() { return relayed.get(this)?.readyState ?? super.readyState; }
          get status() { return answered(this)?.status ?? super.status; }
          get statusText() { return answered(this)?.statusText ?? super.statusText; }
          get responseURL() { const reply = answered(this); return reply ? withoutHash(reply.url) : super.responseURL; }
          getResponseHeader(name) {
            const reply = answered(this);
            return reply ? (headerMap(reply.headers).get(String(name).toLowerCase()) ?? null) : super.getResponseHeader(name);
          }
          getAllResponseHeaders() {
            const reply = answered(this);
            if (!reply) return super.getAllResponseHeaders();
            return [...headerMap(reply.headers)]
              .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
              .map(([name, value]) => name + ": " + value + "\r\n")
              .join("");
          }
        };
"""