/** The same Hub handshake as Gradio 5.49.1's frontend, scoped to its parent. */ declare global {interface Window {supports_zerogpu_headers?:boolean}} const HUB='https://huggingface.co'; const SUPPORT='supports-zerogpu-headers'; let handshake:Promise|undefined; export function initializeHubQuota(){ if(handshake)return handshake; if(window.parent===window||!location.hostname.endsWith('.hf.space'))return Promise.resolve(); handshake=new Promise(resolve=>{ const receive=(event:MessageEvent)=>{ if(event.source!==window.parent||event.origin!==HUB||event.data!==SUPPORT)return; window.supports_zerogpu_headers=true; window.removeEventListener('message',receive);clearTimeout(timer);resolve(); }; const timer=window.setTimeout(()=>{window.removeEventListener('message',receive);handshake=undefined;resolve();},2000); window.addEventListener('message',receive); window.parent.postMessage(SUPPORT,HUB); }); return handshake; } export async function connectEngine(csrf:string){ await initializeHubQuota(); const {Client}=await import('@gradio/client'); // The official client obtains fresh Hub-signed ZeroGPU headers for each submit. // These headers only select compute quota; they never grant contribution rights. return Client.connect(location.origin+'/engine/',{headers:{'X-CSRF-Token':csrf},events:['data','status']}); }