From 339b066d2d4cae32053c2f58407e2ebdce310064 Mon Sep 17 00:00:00 2001 From: Simon <10131203+gaomeng1900@users.noreply.github.com> Date: Wed, 17 Dec 2025 19:31:28 +0800 Subject: [PATCH] fix(UMD): delay init --- packages/page-agent/src/umd.ts | 36 +++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/page-agent/src/umd.ts b/packages/page-agent/src/umd.ts index 3d9bc05..58b7907 100644 --- a/packages/page-agent/src/umd.ts +++ b/packages/page-agent/src/umd.ts @@ -20,21 +20,25 @@ const DEMO_MODEL = 'PAGE-AGENT-FREE-TESTING-RANDOM' const DEMO_BASE_URL = 'https://hwcxiuzfylggtcktqgij.supabase.co/functions/v1/llm-testing-proxy' const DEMO_API_KEY = 'PAGE-AGENT-FREE-TESTING-RANDOM' -const currentScript = document.currentScript as HTMLScriptElement | null -if (currentScript) { - console.log('🚀 page-agent.js detected current script:', currentScript.src) - const url = new URL(currentScript.src) - const model = url.searchParams.get('model') || DEMO_MODEL - const baseURL = url.searchParams.get('baseURL') || DEMO_BASE_URL - const apiKey = url.searchParams.get('apiKey') || DEMO_API_KEY - const language = (url.searchParams.get('lang') as 'zh-CN' | 'en-US') || 'zh-CN' - const config: PageAgentConfig = { model, baseURL, apiKey, language } - window.pageAgent = new PageAgent(config) -} else { - console.log('🚀 page-agent.js no current script detected, using default demo config') - window.pageAgent = new PageAgent() -} +// in case document.x is not ready yet +// @todo give a switch to disable auto-init +setTimeout(() => { + const currentScript = document.currentScript as HTMLScriptElement | null + if (currentScript) { + console.log('🚀 page-agent.js detected current script:', currentScript.src) + const url = new URL(currentScript.src) + const model = url.searchParams.get('model') || DEMO_MODEL + const baseURL = url.searchParams.get('baseURL') || DEMO_BASE_URL + const apiKey = url.searchParams.get('apiKey') || DEMO_API_KEY + const language = (url.searchParams.get('lang') as 'zh-CN' | 'en-US') || 'zh-CN' + const config: PageAgentConfig = { model, baseURL, apiKey, language } + window.pageAgent = new PageAgent(config) + } else { + console.log('🚀 page-agent.js no current script detected, using default demo config') + window.pageAgent = new PageAgent() + } -console.log('🚀 page-agent.js initialized with config:', window.pageAgent.config) + console.log('🚀 page-agent.js initialized with config:', window.pageAgent.config) -window.pageAgent.panel.show() // Show panel + window.pageAgent.panel.show() // Show panel +})