@tangle-network/agent-app 0.60.248 → 0.60.249

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/web-react/workspace-prewarm.ts"],"sourcesContent":["/**\n * Prewarm on open: when a member opens a workspace page, ask the product to\n * warm that workspace (resume its box, prepare its profile) so the first turn\n * does not pay for it. The server half is `createWorkspacePrewarmRoute` in\n * `/sandbox`.\n *\n * Fires only from a visible, non-prerendered document. A tab opened in the\n * background fires when it first becomes visible. Requests for the same URL\n * are spaced by `minIntervalMs` across every mount in the tab, so navigating\n * between pages of one workspace sends one request, not one per page. Call\n * `reassert()` on composer focus: a box can suspend while a tab sits open.\n *\n * Failures are silent by design. A warm is an optimization; the turn itself\n * still places the box, so nothing here may surface to the member.\n */\n\nimport { createContext, useCallback, useContext, useEffect } from 'react'\n\nexport interface WorkspacePrewarmRequestOptions {\n /** Minimum gap between requests for the same URL in this tab. Default 60 000 ms. */\n minIntervalMs?: number\n /** Sent to the route so a product can budget page opens apart from composer focus. Default `page-open`. */\n reason?: 'page-open' | 'composer-focus'\n}\n\nexport interface UseWorkspacePrewarmOptions extends WorkspacePrewarmRequestOptions {\n /** The product's prewarm route for this workspace. Empty disables. */\n url: string | null | undefined\n /** False until the member is known to be signed in and allowed to chat. Default true. */\n enabled?: boolean\n}\n\nexport interface WorkspacePrewarmHandle {\n /** Ask again, subject to the same visibility and spacing rules. */\n reassert(): void\n}\n\nconst DEFAULT_MIN_INTERVAL_MS = 60_000\nconst lastSent = new Map<string, number>()\nconst inFlight = new Set<string>()\n\nfunction documentIsVisible(): boolean {\n if (typeof document === 'undefined') return false\n if ((document as Document & { prerendering?: boolean }).prerendering) return false\n return document.visibilityState === 'visible'\n}\n\n/**\n * Send one prewarm request for `url` unless the tab is hidden, a request for it\n * is in flight, or one was sent within `minIntervalMs`. Returns whether it sent.\n */\nexport function requestWorkspacePrewarm(url: string, options: WorkspacePrewarmRequestOptions = {}): boolean {\n if (!url || !documentIsVisible() || inFlight.has(url)) return false\n const now = Date.now()\n const last = lastSent.get(url)\n if (last !== undefined && now - last < (options.minIntervalMs ?? DEFAULT_MIN_INTERVAL_MS)) return false\n lastSent.set(url, now)\n inFlight.add(url)\n void fetch(url, {\n method: 'POST',\n credentials: 'same-origin',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify({ reason: options.reason ?? 'page-open' }),\n })\n .then((response) => response.body?.cancel())\n .catch(() => undefined)\n .finally(() => inFlight.delete(url))\n return true\n}\n\n/** Prewarm `url` when the page opens or becomes visible; `reassert` on composer focus. */\nexport function useWorkspacePrewarm(options: UseWorkspacePrewarmOptions): WorkspacePrewarmHandle {\n const { url, enabled = true, minIntervalMs } = options\n const active = enabled && Boolean(url)\n\n useEffect(() => {\n if (!active || !url || typeof document === 'undefined') return\n const fire = () => {\n requestWorkspacePrewarm(url, { minIntervalMs })\n }\n fire()\n document.addEventListener('visibilitychange', fire)\n return () => document.removeEventListener('visibilitychange', fire)\n }, [active, url, minIntervalMs])\n\n const reassert = useCallback(() => {\n if (active && url) requestWorkspacePrewarm(url, { minIntervalMs, reason: 'composer-focus' })\n }, [active, url, minIntervalMs])\n\n return { reassert }\n}\n\n/**\n * The prewarm handle of the workspace shell around this component. `AgentWorkspaceLayout` provides it when the\n * product passes `prewarm`; `ChatComposer` reasserts through it on focus unless the product passes its own\n * `onFocusWithin`. Outside a shell that warms, `reassert` does nothing.\n */\nexport const WorkspacePrewarmContext = createContext<WorkspacePrewarmHandle | null>(null)\n\nexport function useWorkspacePrewarmHandle(): WorkspacePrewarmHandle | null {\n return useContext(WorkspacePrewarmContext)\n}\n"],"mappings":";AAgBA,SAAS,eAAe,aAAa,YAAY,iBAAiB;AAqBlE,IAAM,0BAA0B;AAChC,IAAM,WAAW,oBAAI,IAAoB;AACzC,IAAM,WAAW,oBAAI,IAAY;AAEjC,SAAS,oBAA6B;AACpC,MAAI,OAAO,aAAa,YAAa,Q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