§
    ÎÐj|U  ã                  óÆ   — d Z ddlmZ ddlZddlmZ ddlmZ dZdZ	dZ
dZdZd	Zd
dhZ ee¦  «                             ¦   «         j        d         Zdd„Zdd„Zdd„Zg d¢ZdS )zbRuntime monkey patch that keeps WanGP's main tab responsive across tab switches and backgrounding.é    )ÚannotationsN)ÚPath)ÚCallableTFÚ%process_queues_when_browser_unfocusedzwindow.__gradioFocusQueuePatchzfrontend/index.htmlzfrontend/share.htmlé   ÚreturnÚstrc                 ón   — t           sdS t          rdnd} t          rdnd}t          rdnd}d|› d|› d| › d�S )NÚ ÚtrueÚfalseaê4  
(function () {
  if (typeof window === "undefined" || window.__gradioFocusQueuePatch) {
    return;
  }

  const MAIN_TAB_ID = "media_gen";
  const OFFTAB_DELAY_MS = 25;
  const INTERACTION_KEEPALIVE_MS = 1500;
  const KEEPALIVE_CLASS = "wangp-main-tab-keepalive";
  const KEEPALIVE_STYLE_ID = "wangp-main-tab-keepalive-style";
  const nativeRequestAnimationFrame = window.requestAnimationFrame.bind(window);
  const nativeCancelAnimationFrame = window.cancelAnimationFrame.bind(window);
  const nativeAddEventListener = EventTarget.prototype.addEventListener;
  const nativeRemoveEventListener = EventTarget.prototype.removeEventListener;
  const channel = typeof MessageChannel === "function" ? new MessageChannel() : null;
  const wrappedListeners = new WeakMap();
  const wrappedHandleEventListeners = new WeakMap();
  const syntheticJobs = new Map();
  const backgroundQueue = [];
  let nextSyntheticId = 1;
  let activeOfftabContext = null;

  function isElementVisible(element) {
    if (!element) {
      return false;
    }
    const rect = element.getBoundingClientRect();
    const style = window.getComputedStyle(element);
    return style.display !== "none" && style.visibility !== "hidden" && rect.width > 0 && rect.height > 0;
  }

  function getMainTab() {
    if (patch.mainTab && patch.mainTab.isConnected) {
      return patch.mainTab;
    }
    patch.mainTab = document.querySelector(`[role="tab"][data-tab-id="${MAIN_TAB_ID}"]`);
    return patch.mainTab;
  }

  function getMainTabPanel() {
    const tab = getMainTab();
    const panelId = tab ? tab.getAttribute("aria-controls") : null;
    if (!panelId) {
      patch.mainTabPanel = null;
      patch.mainTabPanelId = null;
      return null;
    }
    if (patch.mainTabPanel && patch.mainTabPanelId === panelId && patch.mainTabPanel.isConnected) {
      return patch.mainTabPanel;
    }
    patch.mainTabPanelId = panelId;
    patch.mainTabPanel = document.getElementById(panelId);
    return patch.mainTabPanel;
  }

  function isMainTabSelected() {
    const tab = getMainTab();
    return !!tab && tab.getAttribute("aria-selected") === "true";
  }

  function isMainTabActive() {
    return isElementVisible(getMainTabPanel());
  }

  function ensureKeepAliveStyle() {
    if (document.getElementById(KEEPALIVE_STYLE_ID)) {
      return;
    }
    const style = document.createElement("style");
    style.id = KEEPALIVE_STYLE_ID;
    style.textContent = `
      .${KEEPALIVE_CLASS} {
        display: flex !important;
        position: absolute !important;
        left: -200vw !important;
        top: 0 !important;
        width: 1px !important;
        height: 1px !important;
        min-height: 0 !important;
        max-height: 1px !important;
        overflow: hidden !important;
        opacity: 0 !important;
        pointer-events: none !important;
        z-index: -1 !important;
        contain: layout style paint !important;
      }
    `;
    document.head.appendChild(style);
  }

  function syncMainTabKeepAlive() {
    const panel = getMainTabPanel();
    if (!panel) {
      return false;
    }
    panel.classList.toggle(KEEPALIVE_CLASS, patch.shouldKeepMainTabAlive());
    patch.mainTabKeepAliveActive = panel.classList.contains(KEEPALIVE_CLASS);
    return patch.mainTabKeepAliveActive;
  }

  function primeMainTabKeepAlive() {
    const panel = getMainTabPanel();
    if (!panel) {
      return false;
    }
    panel.classList.add(KEEPALIVE_CLASS);
    patch.mainTabKeepAliveActive = true;
    return true;
  }

  function primeMainTabInteraction() {
    patch.interactionKeepAliveUntil = Date.now() + INTERACTION_KEEPALIVE_MS;
  }

  function isSameTopLevelTabList(tab) {
    const mainTab = getMainTab();
    if (!tab || !mainTab || tab === mainTab) {
      return false;
    }
    return tab.closest('[role="tablist"]') === mainTab.closest('[role="tablist"]');
  }

  function installTopLevelTabPrime() {
    if (patch.topLevelTabPrimeInstalled) {
      return;
    }
    patch.topLevelTabPrimeInstalled = true;
    const handler = (event) => {
      const tab = event && event.target && event.target.closest ? event.target.closest('[role="tab"]') : null;
      if (!patch.enableOfftabKeepAlive || !isMainTabSelected() || !isSameTopLevelTabList(tab)) {
        return;
      }
      primeMainTabKeepAlive();
    };
    document.addEventListener("pointerdown", handler, true);
    document.addEventListener("click", handler, true);
  }

  function installMainTabInteractionPrime() {
    if (patch.mainTabInteractionPrimeInstalled) {
      return;
    }
    patch.mainTabInteractionPrimeInstalled = true;
    const handler = (event) => {
      if (!patch.enableOfftabKeepAlive) {
        return;
      }
      const panel = getMainTabPanel();
      const target = event && event.target ? event.target : null;
      if (!panel || !target || !panel.contains(target)) {
        return;
      }
      primeMainTabInteraction();
    };
    document.addEventListener("pointerdown", handler, true);
    document.addEventListener("click", handler, true);
  }

  function scheduleMainTabKeepAliveSync() {
    if (patch.keepAliveSyncQueued) {
      return;
    }
    patch.keepAliveSyncQueued = true;
    Promise.resolve().then(() => {
      patch.keepAliveSyncQueued = false;
      patch.mainTabKeepAliveActive = syncMainTabKeepAlive();
    });
  }

  function installMainTabKeepAliveObserver() {
    if (patch.keepAliveObserverInstalled) {
      return;
    }
    patch.keepAliveObserverInstalled = true;
    let observedTab = null, observedPanel = null;
    function invalidateComponentIds() {
      patch.targetComponentIds = null;
      patch.targetComponentIdsPanelId = null;
    }
    function observeMainTab() {
      const tab = getMainTab(), panel = getMainTabPanel();
      if (tab === observedTab && panel === observedPanel) return false;
      observer.disconnect();
      observedTab = tab;
      observedPanel = panel;
      invalidateComponentIds();
      if (tab && panel) {
        observer.observe(tab, {attributes: true, attributeFilter: ["aria-selected", "aria-controls"]});
        observer.observe(panel, {childList: true, subtree: true});
        // Detect replacement of the tab or panel without watching other panels.
        observer.observe(tab.parentElement, {childList: true});
        observer.observe(panel.parentElement, {childList: true});
        scheduleMainTabKeepAliveSync();
      } else {
        observer.observe(document.body || document.documentElement, {childList: true, subtree: true});
      }
      return true;
    }
    const hasComponent = node => node.nodeType === 1 && (node.matches('[id^="component-"]') || node.querySelector('[id^="component-"]'));
    const observer = new MutationObserver(records => {
      if (observeMainTab() || !observedPanel) return;
      if (records.some(record => record.type === "attributes" && record.target === observedTab)) scheduleMainTabKeepAliveSync();
      if (records.some(record => record.type === "childList" && observedPanel.contains(record.target) &&
          [...record.addedNodes, ...record.removedNodes].some(hasComponent))) invalidateComponentIds();
    });
    patch.keepAliveObserver = observer;
    // The bootstrap watch ends as soon as Gradio mounts the main tab.
    observer.observe(document.body || document.documentElement, {childList: true, subtree: true});
    observeMainTab();
  }

  function getTargetComponentIds() {
    if (patch.targetComponentIds !== null && patch.targetComponentIdsPanelId === patch.getMainTabPanelId()) {
      return patch.targetComponentIds;
    }
    const panel = getMainTabPanel();
    if (!panel) {
      return null;
    }
    const componentNodes = [panel, ...panel.querySelectorAll('[id^="component-"]')];
    const componentIds = new Set();
    for (const node of componentNodes) {
      const match = /^component-(\d+)$/.exec(String(node && node.id || ""));
      if (match) {
        componentIds.add(Number(match[1]));
      }
    }
    patch.targetComponentIds = componentIds;
    patch.targetComponentIdsPanelId = patch.getMainTabPanelId();
    return patch.targetComponentIds;
  }

  function isTargetGradioEvent(detail) {
    if (!detail || !Number.isInteger(detail.id)) {
      return false;
    }
    const targetComponentIds = getTargetComponentIds();
    return !!targetComponentIds && targetComponentIds.has(detail.id);
  }

  function shouldPatchBackgroundAnimationFrame(stack, callback) {
    if (!patch.shouldPatchBackground()) {
      return false;
    }
    const stackText = String(stack || "");
    const source = String(callback || "");
    const isBlocksDispatch = stackText.includes("/assets/Blocks-") && source.includes("Jt(");
    const isCoreFlush = stackText.includes("/assets/index-") && source.includes("l.update(") && source.includes("ge.length") && source.includes("f.props[v.prop]=j");
    return isBlocksDispatch || isCoreFlush;
  }

  function scheduleBackgroundAnimationFrame(callback) {
    if (!channel) {
      return nativeRequestAnimationFrame(callback);
    }
    const syntheticId = -nextSyntheticId++;
    const job = { id: syntheticId, kind: "background", callback, canceled: false };
    syntheticJobs.set(syntheticId, job);
    backgroundQueue.push(job);
    channel.port2.postMessage(syntheticId);
    patch.backgroundDispatchCount += 1;
    patch.lastBackgroundDispatch = { timestamp: Date.now() };
    if (patch.verbose) {
      console.debug("[Gradio] background synthetic animation frame", syntheticId);
    }
    return syntheticId;
  }

  function dispatchBackgroundJob(job) {
    if (!job || job.canceled) {
      return;
    }
    syntheticJobs.delete(job.id);
    try {
      job.callback(window.performance.now());
    } catch (error) {
      window.setTimeout(() => {
        throw error;
      }, 0);
    }
  }

  function shouldUseOfftabScheduler(detail) {
    return patch.shouldPatchOfftab() && isTargetGradioEvent(detail);
  }

  function shouldPatchOfftabDeferredCallback(callback) {
    if (!patch.shouldPatchOfftab() || Date.now() > patch.syntheticWindowUntil) {
      return false;
    }
    const source = String(callback || "");
    const isBlocksDispatch = source.includes("Jt(");
    const isCoreFlush = source.includes("l.update(") && source.includes("ge.length") && source.includes("f.props[v.prop]=j");
    return isBlocksDispatch || isCoreFlush;
  }

  function scheduleOfftabAnimationFrame(callback, context) {
    const syntheticId = -nextSyntheticId++;
    const timeoutId = window.setTimeout(() => {
      syntheticJobs.delete(syntheticId);
      try {
        callback(window.performance.now());
      } catch (error) {
        window.setTimeout(() => {
          throw error;
        }, 0);
      }
    }, context.delay);
    syntheticJobs.set(syntheticId, { kind: "offtab", timeoutId });
    patch.offtabDispatchCount += 1;
    patch.lastOfftabDispatch = {
      componentId: context.componentId,
      eventName: context.eventName,
      delay: context.delay,
      timestamp: Date.now(),
    };
    return syntheticId;
  }

  function cancelSyntheticAnimationFrame(syntheticId) {
    const job = syntheticJobs.get(syntheticId);
    if (typeof job === "undefined") {
      return false;
    }
    syntheticJobs.delete(syntheticId);
    if (job.kind === "offtab") {
      window.clearTimeout(job.timeoutId);
    } else {
      job.canceled = true;
    }
    return true;
  }

  function withOfftabScheduler(detail, invoke) {
    const previousContext = activeOfftabContext;
    const context = {
      componentId: detail && Number.isInteger(detail.id) ? detail.id : null,
      eventName: detail && typeof detail.event === "string" ? detail.event : "",
      delay: OFFTAB_DELAY_MS,
    };
    patch.syntheticWindowUntil = Date.now() + Math.max(200, context.delay * 4);
    activeOfftabContext = context;
    try {
      return invoke();
    } finally {
      activeOfftabContext = previousContext;
    }
  }

  function wrapGradioListener(listener) {
    if (wrappedListeners.has(listener)) {
      return wrappedListeners.get(listener);
    }
    const wrapped = function (event) {
      const detail = event && event.detail ? event.detail : null;
      if (!shouldUseOfftabScheduler(detail)) {
        return listener.apply(this, arguments);
      }
      patch.lastTargetedEvent = {
        componentId: detail.id,
        eventName: typeof detail.event === "string" ? detail.event : "",
        timestamp: Date.now(),
      };
      return withOfftabScheduler(detail, () => listener.apply(this, arguments));
    };
    wrappedListeners.set(listener, wrapped);
    return wrapped;
  }

  function wrapGradioHandleEventListener(listener) {
    if (!listener || typeof listener.handleEvent !== "function") {
      return listener;
    }
    if (wrappedHandleEventListeners.has(listener)) {
      return wrappedHandleEventListeners.get(listener);
    }
    const wrapped = {
      handleEvent(event) {
        const detail = event && event.detail ? event.detail : null;
        if (!shouldUseOfftabScheduler(detail)) {
          return listener.handleEvent.call(listener, event);
        }
        patch.lastTargetedEvent = {
          componentId: detail.id,
          eventName: typeof detail.event === "string" ? detail.event : "",
          timestamp: Date.now(),
        };
        return withOfftabScheduler(detail, () => listener.handleEvent.call(listener, event));
      },
    };
    wrappedHandleEventListeners.set(listener, wrapped);
    return wrapped;
  }

  const patch = {
    enableBackgroundScheduler: z,
    enableOfftabKeepAlive: z+,
    forceBackground: false,
    verbose: až  ,
    targetComponentIds: null,
    targetComponentIdsPanelId: null,
    syntheticWindowUntil: 0,
    lastTargetedEvent: null,
    mainTab: null,
    mainTabPanel: null,
    mainTabPanelId: null,
    keepAliveObserver: null,
    keepAliveObserverInstalled: false,
    keepAliveSyncQueued: false,
    topLevelTabPrimeInstalled: false,
    mainTabInteractionPrimeInstalled: false,
    mainTabKeepAliveActive: false,
    interactionKeepAliveUntil: 0,
    backgroundDispatchCount: 0,
    offtabDispatchCount: 0,
    lastBackgroundDispatch: null,
    lastOfftabDispatch: null,
    isBackground() {
      if (this.forceBackground) {
        return true;
      }
      try {
        return document.visibilityState !== "visible" || !document.hasFocus();
      } catch (_error) {
        return false;
      }
    },
    shouldKeepMainTabAlive() {
      return this.enableOfftabKeepAlive && !isMainTabSelected();
    },
    shouldPatchBackground() {
      return this.enableBackgroundScheduler && this.isBackground() && isMainTabActive();
    },
    shouldPatchOfftab() {
      return this.enableOfftabKeepAlive && !this.isBackground() && (this.mainTabKeepAliveActive || Date.now() < this.interactionKeepAliveUntil);
    },
    getMainTabPanelId() {
      const panel = getMainTabPanel();
      return panel ? panel.id : null;
    },
    getTargetComponentIds,
    syncMainTabKeepAlive,
    scheduleMainTabKeepAliveSync,
    primeMainTabKeepAlive,
    primeMainTabInteraction,
  };

  if (channel) {
    channel.port1.onmessage = () => {
      dispatchBackgroundJob(backgroundQueue.shift());
    };
  }

  window.__gradioFocusQueuePatch = patch;
  ensureKeepAliveStyle();
  installMainTabKeepAliveObserver();
  installTopLevelTabPrime();
  installMainTabInteractionPrime();

  window.requestAnimationFrame = function (callback) {
    if (typeof callback !== "function") {
      return nativeRequestAnimationFrame(callback);
    }
    if (activeOfftabContext) {
      return scheduleOfftabAnimationFrame(callback, activeOfftabContext);
    }
    const backgroundActive = patch.shouldPatchBackground();
    const offtabActive = patch.shouldPatchOfftab();
    if (!backgroundActive && !offtabActive) {
      return nativeRequestAnimationFrame(callback);
    }
    const stack = new Error().stack || "";
    if (backgroundActive && shouldPatchBackgroundAnimationFrame(stack, callback)) {
      return scheduleBackgroundAnimationFrame(callback);
    }
    if (offtabActive && shouldPatchOfftabDeferredCallback(callback)) {
      return scheduleOfftabAnimationFrame(callback, {
        componentId: patch.lastTargetedEvent ? patch.lastTargetedEvent.componentId : null,
        eventName: patch.lastTargetedEvent ? patch.lastTargetedEvent.eventName : "",
        delay: OFFTAB_DELAY_MS,
      });
    }
    return nativeRequestAnimationFrame(callback);
  };

  window.cancelAnimationFrame = function (animationFrameId) {
    if (cancelSyntheticAnimationFrame(animationFrameId)) {
      return;
    }
    return nativeCancelAnimationFrame(animationFrameId);
  };

  EventTarget.prototype.addEventListener = function (type, listener, options) {
    if (type === "gradio") {
      if (typeof listener === "function") {
        return nativeAddEventListener.call(this, type, wrapGradioListener(listener), options);
      }
      if (listener && typeof listener.handleEvent === "function") {
        return nativeAddEventListener.call(this, type, wrapGradioHandleEventListener(listener), options);
      }
    }
    return nativeAddEventListener.call(this, type, listener, options);
  };

  EventTarget.prototype.removeEventListener = function (type, listener, options) {
    if (type === "gradio") {
      if (typeof listener === "function" && wrappedListeners.has(listener)) {
        return nativeRemoveEventListener.call(this, type, wrappedListeners.get(listener), options);
      }
      if (listener && typeof listener.handleEvent === "function" && wrappedHandleEventListeners.has(listener)) {
        return nativeRemoveEventListener.call(this, type, wrappedHandleEventListeners.get(listener), options);
      }
    }
    return nativeRemoveEventListener.call(this, type, listener, options);
  };

  console.info("[Gradio] main tab keepalive patch installed");
})();
)Ú%ENABLE_GRADIO_FOCUS_QUEUE_MONKEYPATCHÚ&GRADIO_FOCUS_QUEUE_MONKEYPATCH_VERBOSEÚ$BACKGROUND_SCHEDULER_DEFAULT_ENABLEDÚ OFFTAB_KEEPALIVE_DEFAULT_ENABLED)ÚverboseÚbackground_default_enabledÚofftab_default_enableds      úNC:\Users\johnn\OneDrive\Documents\AI\shared\gradio\gradio_queue_focus_patch.pyÚget_javascriptr      s‡   € Ý0ð ØˆrÝ>ÐKˆfˆfÀG€GÝ+OÐ!\  ÐU\ÐÝ'GÐT˜V˜VÈWÐðGðX !;ðYGð GðZ 3ð[Gð Gð^ ð_Gð Gð Gð Gó    Útemplate_sourcec                ó  — t           | v r| S dt          ¦   «         › d�}d}|                      |¦  «        }|dk    r| d |…         |z   | |d …         z   S |                      d¦  «        }|dk    r| d |…         |z   | |d …         z   S | |z   S )Nz
		<script>
z
		</script>
z<script type="module"éÿÿÿÿz</head>)Ú_PATCH_SENTINELr   Úfind)r   Ú
script_tagÚ
module_tagÚ	insert_atÚ
head_closes        r   Ú_inject_scriptr!   %  s·   € Ý˜/Ð)Ð)ØÐØG¥NÑ$4Ô$4ÐGÐGÐG€JØ(€JØ×$Ò$ ZÑ0Ô0€IØ�B‚€Ø˜z 	˜zÔ*¨ZÑ7¸/È)È*È*Ô:UÑUÐUØ ×%Ò% iÑ0Ô0€JØ�RÒÐØ˜{ 
˜{Ô+¨jÑ8¸?È:È;È;Ô;WÑWÐWØ˜ZÑ'Ð'r   Úboolc                 ól  ‡— t           sdS t          j        rHt          j        d         r6t          t          j        d         ¦  «        j                             ¦   «         nd} t          j        ¦   «                              ¦   «         }|t          k    rt          |j	        vr| dk    rdS dd l
m} t          |dd ¦  «        }t          t          |dd ¦  «        dd ¦  «        }|€dS t          |dd¦  «        rd	S |j        Šˆfd
„}||_        d	|_        ‰|_        |j        j                             ¦   «          d	S )NFr   r   zwgp.pyÚ	templatesÚenvÚloaderÚ_focus_queue_patch_installedTc                ó^   •—  ‰| |¦  «        \  }}}|t           v rt          |¦  «        }|||fS )N)Ú_TARGET_TEMPLATESr!   )ÚenvironmentÚtemplateÚsourceÚfilenameÚuptodateÚoriginal_get_sources        €r   Úpatched_get_sourcez#install.<locals>.patched_get_sourceE  sB   ø€ Ø%8Ð%8¸ÀhÑ%OÔ%OÑ"ˆ�˜(ØÕ(Ð(Ð(Ý# FÑ+Ô+ˆFØ�x Ð)Ð)r   )r   ÚsysÚargvr   ÚnameÚlowerÚcwdÚresolveÚ_PROJECT_ROOTÚparentsÚgradio.routesÚroutesÚgetattrÚ
get_sourcer'   Ú&_focus_queue_patch_original_get_sourcer%   ÚcacheÚclear)Úargv0r5   Úgradio_routesr$   r&   r0   r/   s         @r   ÚinstallrB   3  sB  ø€ Ý0ð ØˆuÝ.1¬hÐN½3¼8ÀA¼;ÐN�D•”˜!”ÑÔÔ"×(Ò(Ñ*Ô*Ð*ÈB€EÝ
Œ(‰*Œ*×
Ò
Ñ
Ô
€CØ
�mÒÐ¥°S´[Ð @Ð @ÀUÈhÒEVÐEVØˆuØ)Ð)Ð)Ð)Ð)Ð)å˜ {°DÑ9Ô9€IÝ•W˜Y¨¨tÑ4Ô4°hÀÑEÔE€FØ€~ØˆuÝˆvÐ5°uÑ=Ô=ð Øˆtà$*Ô$5Ðð*ð *ð *ð *ð *ð +€FÔØ*.€FÔ'Ø4G€FÔ1Ø„MÔ×ÒÑÔÐØˆ4r   )r   r   ÚFOCUS_QUEUE_SERVER_CONFIG_KEYr   r   r   rB   )r   r	   )r   r	   r   r	   )r   r"   )Ú__doc__Ú
__future__r   r1   Úpathlibr   Útypingr   r   r   rC   r   r   r   r)   Ú__file__r6   r8   r7   r   r!   rB   Ú__all__© r   r   ú<module>rK      sð   ðà hÐ hà "Ð "Ð "Ð "Ð "Ð "à 
€
€
€
Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à(,Ð %Ø).Ð &Ø GÐ Ø'+Ð $Ø#'Ð  à2€Ø*Ð,AÐBÐ Ø��X‘”×&Ò&Ñ(Ô(Ô0°Ô3€ðMð Mð Mð Mð`(ð (ð (ð (ðð ð ð ð>ð ð €€€r   