import { createAgentExperience, AgentWidgetController } from "../ui"; import { AgentWidgetConfig, AgentWidgetInitOptions, AgentWidgetEvent } from "../types"; const ensureTarget = (target: string | HTMLElement): HTMLElement => { if (typeof window === "undefined" || typeof document === "undefined") { throw new Error("Chat widget can only be mounted in a browser environment"); } if (typeof target === "string") { const element = document.querySelector(target); if (!element) { throw new Error(`Chat widget target "${target}" was not found`); } return element; } return target; }; const widgetCssHref = (): string | null => { try { // This works in ESM builds but not in IIFE builds if (typeof import.meta !== "undefined" && import.meta.url) { return new URL("../widget.css", import.meta.url).href; } } catch { // Fallback for IIFE builds where CSS should be loaded separately } return null; }; const mountStyles = (root: ShadowRoot | HTMLElement) => { const href = widgetCssHref(); const adoptExistingStylesheet = () => { if (!(root instanceof ShadowRoot)) { return; } if (root.querySelector('link[data-vanilla-agent]')) { return; } const globalLink = document.head.querySelector( 'link[data-vanilla-agent]' ); if (!globalLink) { return; } const clonedLink = globalLink.cloneNode(true) as HTMLLinkElement; root.insertBefore(clonedLink, root.firstChild); }; if (root instanceof ShadowRoot) { // For shadow DOM, we need to load CSS into the shadow root if (href) { const link = document.createElement("link"); link.rel = "stylesheet"; link.href = href; link.setAttribute("data-vanilla-agent", "true"); root.insertBefore(link, root.firstChild); } else { adoptExistingStylesheet(); } // If href is null (IIFE build), CSS should already be loaded globally } else { // For non-shadow DOM, check if CSS is already loaded const existing = document.head.querySelector( "link[data-vanilla-agent]" ); if (!existing) { if (href) { // ESM build - load CSS dynamically const link = document.createElement("link"); link.rel = "stylesheet"; link.href = href; link.setAttribute("data-vanilla-agent", "true"); document.head.appendChild(link); } // IIFE build - CSS should be loaded via tag before script // If not found, we'll assume it's loaded globally or warn in dev } } }; export type AgentWidgetInitHandle = AgentWidgetController & { host: HTMLElement }; /** * Check if preview mode should block widget initialization. * Returns true if the widget should NOT load (preview mode is enabled but param is missing/falsy). */ const shouldBlockForPreviewMode = (previewQueryParam?: string): boolean => { if (!previewQueryParam) return false; // No preview mode, don't block if (typeof window === 'undefined') return false; // SSR, don't block try { const urlParams = new URLSearchParams(window.location.search); const paramValue = urlParams.get(previewQueryParam); // Block if param is missing, empty, or explicitly "false" const isTruthy = paramValue !== null && paramValue !== '' && paramValue.toLowerCase() !== 'false' && paramValue !== '0'; return !isTruthy; } catch { return false; // If URL parsing fails, don't block } }; export const initAgentWidget = ( options: AgentWidgetInitOptions ): AgentWidgetInitHandle | null => { // Check preview mode early - return null without initializing anything if (shouldBlockForPreviewMode(options.previewQueryParam)) { if (options.config?.debug) { console.log(`[AgentWidget] Preview mode enabled. Widget not initialized. Add ?${options.previewQueryParam}=true to URL to initialize.`); } return null; } const target = ensureTarget(options.target); const host = document.createElement("div"); host.className = "vanilla-agent-host"; // When launcher is disabled (inline embed mode), ensure the host fills its container const launcherEnabled = options.config?.launcher?.enabled ?? true; if (!launcherEnabled) { host.style.height = "100%"; } target.appendChild(host); const useShadow = options.useShadowDom === true; let mount: HTMLElement; let root: ShadowRoot | HTMLElement; if (useShadow) { const shadowRoot = host.attachShadow({ mode: "open" }); root = shadowRoot; mount = document.createElement("div"); mount.id = "vanilla-agent-root"; // When launcher is disabled, ensure mount fills the host if (!launcherEnabled) { mount.style.height = "100%"; mount.style.display = "flex"; mount.style.flexDirection = "column"; mount.style.flex = "1"; mount.style.minHeight = "0"; } shadowRoot.appendChild(mount); mountStyles(shadowRoot); } else { root = host; mount = document.createElement("div"); mount.id = "vanilla-agent-root"; // When launcher is disabled, ensure mount fills the host if (!launcherEnabled) { mount.style.height = "100%"; mount.style.display = "flex"; mount.style.flexDirection = "column"; mount.style.flex = "1"; mount.style.minHeight = "0"; } host.appendChild(mount); mountStyles(host); } let controller = createAgentExperience(mount, options.config, { debugTools: options.debugTools }); options.onReady?.(); const handle: AgentWidgetInitHandle = { ...controller, host, destroy() { controller.destroy(); host.remove(); if (options.windowKey && typeof window !== "undefined") { delete (window as any)[options.windowKey]; } } }; // Store on window if windowKey is provided if (options.windowKey && typeof window !== 'undefined') { (window as any)[options.windowKey] = handle; } return handle; };