@mindstudio-ai/interface 0.1.19 → 0.1.22

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.
package/README.md CHANGED
@@ -459,9 +459,41 @@ Authentication is cookie-based (`HttpOnly`, `Secure`, `SameSite=Lax`). The SDK n
459
459
 
460
460
  ## Error reporting
461
461
 
462
- Uncaught errors and unhandled promise rejections are automatically captured and shipped to the platform for bucketing + dashboards. No setup required — install happens automatically on page load (next tick after the SDK module loads). Reports include the error, a stack, and a breadcrumb trail of recent navigations + network calls for context.
462
+ Uncaught errors and unhandled promise rejections are automatically captured and shipped to the platform for bucketing + dashboards. No setup required — install happens when the SDK module loads. Reports include the error, a stack, and a breadcrumb trail of recent navigations + network calls for context.
463
463
 
464
- Opt out per app via bootstrap config:
464
+ ### React crashes need two lines
465
+
466
+ React does not tell the window about an error one of its error boundaries caught — `onCaughtError` defaults to `console.error` and nothing else. **An app with an error boundary and no root hooks reports no render crashes at all**, and those are the ones that white-screen an app. Wire React 19's root hooks and they come through with the component stack attached:
467
+
468
+ ```tsx
469
+ import { telemetry } from '@mindstudio-ai/interface';
470
+
471
+ createRoot(document.getElementById('root')!, {
472
+ onUncaughtError: telemetry.reactErrorHandler(),
473
+ onCaughtError: telemetry.reactErrorHandler(),
474
+ }).render(
475
+ <ErrorBoundary>
476
+ <App />
477
+ </ErrorBoundary>,
478
+ );
479
+ ```
480
+
481
+ Crashes reported this way are marked **unhandled** — a boundary existing is not evidence the app recovered. If yours renders a real fallback the user can carry on from, say so: `telemetry.reactErrorHandler({ handled: true })`.
482
+
483
+ ### Reporting something you caught
484
+
485
+ ```ts
486
+ try {
487
+ await api.submitOrder(order);
488
+ } catch (err) {
489
+ telemetry.captureException(err);
490
+ setError('Could not submit that order.');
491
+ }
492
+ ```
493
+
494
+ Manual captures are marked handled by default, so they sort below real crashes in the dashboard.
495
+
496
+ Opt out per app via bootstrap config — this covers `captureException` too:
465
497
 
466
498
  ```ts
467
499
  window.__MINDSTUDIO__.telemetry = { errors: false };
@@ -0,0 +1,4 @@
1
+ var b=class extends Error{code;status;constructor(t,n,r){super(t),this.name="MindStudioInterfaceError",this.code=n,this.status=r}};var oe="/_/telemetry/";var w=[];function h(e){w.push(e),w.length>20&&w.shift()}function P(){return w.slice()}function N(e){return e.includes(oe)}function se(e){return typeof e=="string"?e:e instanceof URL?e.href:e.url||String(e)}var E="",A=new Set;function O(e){return A.add(e),()=>{A.delete(e)}}function _(e){e!==E&&(h({type:"navigation",from:E,to:e,timestamp:Date.now()}),E=e,A.forEach(t=>{try{t(e)}catch{}}))}function ie(){let e=window;if(e.__ms_history_patched)return;e.__ms_history_patched=!0,E=location.href;let t=history.pushState.bind(history);history.pushState=function(r,o,s){t(r,o,s);try{_(location.href)}catch{}};let n=history.replaceState.bind(history);history.replaceState=function(r,o,s){n(r,o,s);try{_(location.href)}catch{}},window.addEventListener("popstate",()=>{try{_(location.href)}catch{}}),window.addEventListener("hashchange",()=>{try{_(location.href)}catch{}})}function ae(e){if(!window.fetch||window.fetch.__ms_patched)return;let n=window.fetch.bind(window),r=function(o,s){let u=se(o);if(N(u))return n(o,s);let y=(s?.method||(typeof o!="string"&&!(o instanceof URL)?o.method:"")||"GET").toUpperCase(),f=Date.now();return n(o,s).then(i=>{let p={type:"fetch",method:y,url:u,status:i.status,ok:i.ok,duration:Date.now()-f,timestamp:f};if(!i.ok&&e)try{i.clone().text().then(d=>{p.body=d.slice(0,1024),h(p)}).catch(()=>h(p))}catch{h(p)}else h(p);return i},i=>{throw h({type:"fetch",method:y,url:u,ok:!1,duration:Date.now()-f,timestamp:f,error:i instanceof Error?i.message:String(i??"fetch failed")}),i})};r.__ms_patched=!0,window.fetch=r}function ce(){if(XMLHttpRequest.prototype.open.__ms_patched)return;let t=XMLHttpRequest.prototype.open,n=XMLHttpRequest.prototype.send;XMLHttpRequest.prototype.open=function(r,o,...s){return this.__ms_method=(r||"GET").toUpperCase(),this.__ms_url=String(o),t.apply(this,[r,o,...s])},XMLHttpRequest.prototype.send=function(r){let o=this.__ms_method||"GET",s=this.__ms_url||"";if(N(s))return n.apply(this,[r]);let u=Date.now();return this.addEventListener("loadend",function(){h({type:"xhr",method:o,url:s,status:this.status,ok:this.status>=200&&this.status<300,duration:Date.now()-u,timestamp:u})},{once:!0}),n.apply(this,[r])},XMLHttpRequest.prototype.open.__ms_patched=!0}var D=!1;function U(e){if(!D){D=!0;try{ie()}catch{}try{ae(e.captureResponseBodies)}catch{}try{ce()}catch{}}}var X="/_/telemetry/errors",ue=1e3,le=50,de=6e4,H=!1,c=[],L=null,B=0;function fe(e,t){let n=t.split(`
2
+ `)[1]??"";return`${e}|${n.trim()}`}function pe(e){let t=fe(e.message,e.stack);for(let n of c)if(n._fp===t){n.count=(n.count??1)+1;return}c.length>=le||(e._fp=t,c.push(e),me())}function me(){L||c.length===0||(L=setTimeout(()=>{L=null,he()},ue))}function F(){let e=c;return c=[],e.map(({_fp:t,...n})=>n)}async function he(){if(c.length===0)return;if(Date.now()<B){c=[];return}let e=(()=>{try{return a()}catch{return null}})();if(!e){c=[];return}let t=F();try{let n=await fetch(g(X),{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.token}`},body:JSON.stringify({events:t})});if(n.status===429){let r=n.headers.get("Retry-After"),o=r?parseInt(r,10):NaN,s=Number.isFinite(o)&&o>0?o*1e3:de;B=Date.now()+s}}catch{}}function q(){if(c.length===0)return;if(Date.now()<B){c=[];return}let e=null;try{e=a()}catch{c=[];return}let t=F();try{fetch(g(X),{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.token}`},body:JSON.stringify({events:t}),keepalive:!0})}catch{}}function ge(e,t){if(e instanceof Error)return{message:e.message||t,stack:e.stack??""};if(e&&typeof e=="object"){let n=e;return{message:typeof n.message=="string"&&n.message?n.message:String(e),stack:typeof n.stack=="string"?n.stack:""}}return{message:String(e??t),stack:""}}function k(e,t){try{let n=a();if(n.telemetry?.errors===!1)return;let r=ge(e,t.fallbackMessage??"Unknown error");pe({releaseId:n.releaseId,url:location.href,userAgent:navigator.userAgent,timestamp:Date.now(),mechanism:t.mechanism,handled:t.handled,message:t.message||r.message,stack:r.stack,...t.componentStack?{componentStack:t.componentStack}:{},...t.source?{source:t.source}:{},...typeof t.line=="number"?{line:t.line}:{},...typeof t.column=="number"?{column:t.column}:{},breadcrumbs:P()})}catch{}}function ye(e){k(e.error,{mechanism:"browser.onerror",handled:!1,message:e.message,source:e.filename||void 0,line:typeof e.lineno=="number"?e.lineno:void 0,column:typeof e.colno=="number"?e.colno:void 0})}function ve(e){k(e.reason,{mechanism:"browser.onunhandledrejection",handled:!1,fallbackMessage:"Unhandled rejection"})}function be(){if(H||typeof window>"u")return;let e;try{e=a()}catch{return}e.telemetry?.errors!==!1&&(H=!0,U({captureResponseBodies:!!e.telemetryCaptureResponseBodies}),window.addEventListener("error",ye),window.addEventListener("unhandledrejection",ve),window.addEventListener("pagehide",q),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&q()}))}function j(){try{be()}catch{}}var Ne={captureException(e,t){k(e,{mechanism:t?.mechanism??"manual",handled:t?.handled??!0,...t?.componentStack?{componentStack:t.componentStack}:{}})},reactErrorHandler(e){return(t,n)=>{k(t,{mechanism:"react.error_handler",handled:e?.handled??!1,...n?.componentStack?{componentStack:n.componentStack}:{}}),e?.onError?.(t,n)}}};var S;function a(){if(S)return S;let e=globalThis.__MINDSTUDIO__;if(!e)throw new b("@mindstudio-ai/interface requires the MindStudio platform context. This page must be loaded inside MindStudio (window.__MINDSTUDIO__ is missing).","not_initialized");if(!e.token||!e.releaseId||!e.methods){let t=["token","releaseId","methods"].filter(n=>!e[n]).join(", ");throw new b(`MindStudio platform context is incomplete. Missing: ${t}`,"invalid_config")}return S=e,j(),setTimeout(()=>{$()},0),S}function _e(){let e=globalThis.__MINDSTUDIO__?.basePath;if(typeof e!="string")return"";let t=e.replace(/\/+$/,"");return t?t.startsWith("/")?t:`/${t}`:""}function g(e){return`${_e()}${e}`}function Xe(e){let t=a();e.token!==void 0&&(t.token=e.token),e.user!==void 0&&(t.user=e.user),e.methods!==void 0&&(t.methods=e.methods),e.visitorId!==void 0&&(t.visitorId=e.visitorId)}var we={initialBackoffMs:1e3,maxBackoffMs:1e4,stableMs:3e4,retry503DefaultMs:3e4};function z(e,t=we){let n=!1,r="idle",o=null,s=null,u=t.initialBackoffMs,y=()=>{let d=u;return u=Math.min(u*2,t.maxBackoffMs),d+Math.random()*250},f=d=>{s&&clearTimeout(s),s=setTimeout(()=>{s=null,n&&p()},d)},i=()=>{if(!(!n&&r==="stopped")){if(n=!1,r="stopped",s&&(clearTimeout(s),s=null),o){try{o.abort()}catch{}o=null}e.onStop?.()}};async function p(){if(!n)return;r="connecting";let d;try{d=await e.prepare()}catch{i();return}if(d==="stop"||!n){i();return}o=new AbortController;let ee=o.signal;try{let m=await fetch(d.url,{method:"GET",headers:{...d.headers,Accept:"text/event-stream"},signal:ee});if(!n)return;if(m.status===503){let v=parseInt(m.headers.get("Retry-After")??"",10),M=Number.isFinite(v)&&v>0?v*1e3:t.retry503DefaultMs;f(M);return}if(!m.ok||!m.body){(e.onStatus?.(m.status)??"retry")==="stop"?i():f(y());return}let R=Date.now();r="open",e.onOpen?.();let te=m.body.getReader(),ne=new TextDecoder,T="";for(;n;){let{done:v,value:M}=await te.read();if(v)break;if(!e.onLine)continue;T+=ne.decode(M,{stream:!0});let C=T.split(`
3
+ `);T=C.pop()??"";for(let re of C)e.onLine(re.replace(/\r$/,""))}n&&(Date.now()-R>=t.stableMs&&(u=t.initialBackoffMs),r="connecting",f(y()))}catch(m){let R=m?.name;n&&R!=="AbortError"&&(r="connecting",f(y()))}}return{start(){n||(n=!0,p())},stop:i,get state(){return r}}}var V="/_/telemetry/events",Ee=1e3,ke=100,Se=6e4,l=[],I=null,x=0,W=!1;function Z(e){W||l.length>=ke||(l.push(e),Re())}function Re(){I||l.length===0||(I=setTimeout(()=>{I=null,Te()},Ee))}function K(){let e=l;return l=[],e}async function Te(){if(l.length===0)return;if(Date.now()<x){l=[];return}let e;try{e=a()}catch{l=[];return}let t=K();try{let n=await fetch(g(V),{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.token}`},body:JSON.stringify({events:t})});if(n.status===429){let r=n.headers.get("Retry-After"),o=r?parseInt(r,10):NaN,s=Number.isFinite(o)&&o>0?o*1e3:Se;x=Date.now()+s}}catch{}}function Y(){if(l.length===0)return;if(Date.now()<x){l=[];return}let e;try{e=a()}catch{l=[];return}let t=K();try{fetch(g(V),{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.token}`},body:JSON.stringify({events:t}),keepalive:!0})}catch{}}function Me(e){if(!e)return;let t={};for(let[n,r]of Object.entries(e))(typeof r=="string"||typeof r=="number"||typeof r=="boolean")&&(t[n]=r);return Object.keys(t).length>0?t:void 0}function G(e){try{let t=a();Z({type:"pageview",releaseId:t.releaseId,url:e,referrer:document.referrer,userAgent:navigator.userAgent,language:navigator.language,screen:{w:window.innerWidth,h:window.innerHeight},timestamp:Date.now()})}catch{}}function Ae(e,t){try{let n=a();Z({type:"event",releaseId:n.releaseId,name:e,url:location.href,timestamp:Date.now(),props:Me(t)})}catch{}}var Le="/_/telemetry/presence",Q=z({prepare:()=>{let e;try{e=a()}catch{return"stop"}return{url:g(Le),headers:{Authorization:`Bearer ${e.token}`}}},onStatus:e=>e===401||e===204?"stop":"retry"});function Be(){Q.stop()}var Ge={track:Ae},J=!1;function Ie(){if(J||typeof window>"u")return;let e;try{e=a()}catch{return}if(e.telemetry?.analytics===!1)return;J=!0;let t=()=>{W=!0,Y(),Be()};window.addEventListener("pagehide",t),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&Y()}),O(n=>{G(n)}),G(location.href),Q.start()}function $(){try{Ie()}catch{}}export{b as a,z as b,Ge as c,Ne as d,a as e,_e as f,g,Xe as h};
4
+ //# sourceMappingURL=chunk-5GEUHKC7.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors.ts","../src/telemetry-breadcrumbs.ts","../src/telemetry-errors.ts","../src/config.ts","../src/sse-loop.ts","../src/telemetry-analytics.ts"],"sourcesContent":["/**\n * Error class for the @mindstudio-ai/interface SDK.\n *\n * Thrown by `createClient()` method calls on HTTP errors, by `platform`\n * actions on failures, and by the config reader when bootstrap globals\n * are missing.\n */\nexport class MindStudioInterfaceError extends Error {\n /** Machine-readable error code (e.g. 'method_not_found', 'not_initialized'). */\n readonly code: string;\n\n /** HTTP status code, when the error originated from an API response. */\n readonly status?: number;\n\n constructor(message: string, code: string, status?: number) {\n super(message);\n this.name = 'MindStudioInterfaceError';\n this.code = code;\n this.status = status;\n }\n}\n","/**\n * Breadcrumbs — a ring buffer of recent navigations + network calls\n * attached to outgoing error reports for debugging context.\n *\n * Installed by `installErrorMonitoring()` in `telemetry-errors.ts`.\n * Three monkeypatches feed the buffer:\n *\n * - `history.pushState`/`replaceState`/`popstate` → `navigation`\n * - `window.fetch` → `fetch`\n * - `XMLHttpRequest.prototype.open`/`send` → `xhr`\n *\n * Each patch is idempotent — guarded by a `__ms_*_patched` flag so\n * re-imports (HMR, double bundle inclusion, etc.) are no-ops.\n *\n * The SDK's own telemetry endpoint is excluded from capture so error\n * reports don't feed themselves through the breadcrumb pipeline.\n */\n\nconst TELEMETRY_ENDPOINT_FRAGMENT = '/_/telemetry/';\nconst MAX_BREADCRUMBS = 20;\nconst MAX_BODY_BYTES = 1024;\n\n/**\n * A single breadcrumb entry. Discriminated union — server validates the\n * `type` field; unknown types are dropped at ingest.\n */\nexport type Breadcrumb =\n | {\n type: 'navigation';\n from: string;\n to: string;\n timestamp: number;\n }\n | {\n type: 'fetch';\n method: string;\n url: string;\n status?: number;\n ok: boolean;\n duration: number;\n timestamp: number;\n error?: string;\n /** Truncated response body for failed requests, when capture is enabled. */\n body?: string;\n }\n | {\n type: 'xhr';\n method: string;\n url: string;\n status?: number;\n ok: boolean;\n duration: number;\n timestamp: number;\n };\n\nconst buffer: Breadcrumb[] = [];\n\nfunction push(entry: Breadcrumb): void {\n buffer.push(entry);\n if (buffer.length > MAX_BREADCRUMBS) {\n buffer.shift();\n }\n}\n\n/**\n * Return a shallow copy of the current breadcrumb buffer.\n * Called when an error is captured to snapshot context at that moment.\n */\nexport function getBreadcrumbs(): Breadcrumb[] {\n return buffer.slice();\n}\n\nfunction shouldSkipUrl(url: string): boolean {\n return url.includes(TELEMETRY_ENDPOINT_FRAGMENT);\n}\n\nfunction resolveUrl(input: RequestInfo | URL): string {\n if (typeof input === 'string') {\n return input;\n }\n if (input instanceof URL) {\n return input.href;\n }\n return input.url || String(input);\n}\n\nlet lastHref = '';\n\nconst navListeners = new Set<(url: string) => void>();\n\n/**\n * Subscribe to navigation events. Fires on every history change\n * (`pushState`, `replaceState`, `popstate`, `hashchange`) with the new URL.\n *\n * Used by the analytics module to emit pageview events without\n * installing a second set of history patches.\n *\n * @returns Unsubscribe function.\n */\nexport function onNavigation(cb: (url: string) => void): () => void {\n navListeners.add(cb);\n return () => {\n navListeners.delete(cb);\n };\n}\n\nfunction emitNavigation(to: string): void {\n if (to === lastHref) {\n return;\n }\n push({\n type: 'navigation',\n from: lastHref,\n to,\n timestamp: Date.now(),\n });\n lastHref = to;\n navListeners.forEach((cb) => {\n try {\n cb(to);\n } catch {\n // listener errors must not break navigation\n }\n });\n}\n\nfunction installHistory(): void {\n const w = window as unknown as Record<string, unknown>;\n if (w.__ms_history_patched) {\n return;\n }\n w.__ms_history_patched = true;\n\n lastHref = location.href;\n\n const originalPushState = history.pushState.bind(history);\n history.pushState = function (\n data: unknown,\n unused: string,\n url?: string | URL | null,\n ): void {\n originalPushState(data, unused, url);\n try {\n emitNavigation(location.href);\n } catch {\n // never crash navigation\n }\n };\n\n const originalReplaceState = history.replaceState.bind(history);\n history.replaceState = function (\n data: unknown,\n unused: string,\n url?: string | URL | null,\n ): void {\n originalReplaceState(data, unused, url);\n try {\n emitNavigation(location.href);\n } catch {\n // never crash navigation\n }\n };\n\n window.addEventListener('popstate', () => {\n try {\n emitNavigation(location.href);\n } catch {\n // swallow\n }\n });\n\n window.addEventListener('hashchange', () => {\n try {\n emitNavigation(location.href);\n } catch {\n // swallow\n }\n });\n}\n\nfunction installFetch(captureResponseBodies: boolean): void {\n if (!window.fetch) {\n return;\n }\n const fetchFn = window.fetch as typeof fetch & { __ms_patched?: boolean };\n if (fetchFn.__ms_patched) {\n return;\n }\n\n const originalFetch = window.fetch.bind(window);\n\n const patched: typeof fetch = function (\n input: RequestInfo | URL,\n init?: RequestInit,\n ): Promise<Response> {\n const url = resolveUrl(input);\n\n if (shouldSkipUrl(url)) {\n return originalFetch(input, init);\n }\n\n const method = (\n init?.method ||\n (typeof input !== 'string' && !(input instanceof URL)\n ? input.method\n : '') ||\n 'GET'\n ).toUpperCase();\n const startTime = Date.now();\n\n return originalFetch(input, init).then(\n (response) => {\n const entry: Breadcrumb = {\n type: 'fetch',\n method,\n url,\n status: response.status,\n ok: response.ok,\n duration: Date.now() - startTime,\n timestamp: startTime,\n };\n\n if (!response.ok && captureResponseBodies) {\n try {\n response\n .clone()\n .text()\n .then((body) => {\n entry.body = body.slice(0, MAX_BODY_BYTES);\n push(entry);\n })\n .catch(() => push(entry));\n } catch {\n push(entry);\n }\n } else {\n push(entry);\n }\n\n return response;\n },\n (err: unknown) => {\n push({\n type: 'fetch',\n method,\n url,\n ok: false,\n duration: Date.now() - startTime,\n timestamp: startTime,\n error:\n err instanceof Error ? err.message : String(err ?? 'fetch failed'),\n });\n throw err;\n },\n );\n };\n\n (patched as typeof fetch & { __ms_patched: boolean }).__ms_patched = true;\n window.fetch = patched;\n}\n\nfunction installXhr(): void {\n const openProto = XMLHttpRequest.prototype.open as ((\n ...args: unknown[]\n ) => void) & { __ms_patched?: boolean };\n if (openProto.__ms_patched) {\n return;\n }\n\n const originalOpen = XMLHttpRequest.prototype.open;\n const originalSend = XMLHttpRequest.prototype.send;\n\n XMLHttpRequest.prototype.open = function (\n this: XMLHttpRequest & {\n __ms_method?: string;\n __ms_url?: string;\n },\n method: string,\n url: string | URL,\n ...rest: unknown[]\n ): void {\n this.__ms_method = (method || 'GET').toUpperCase();\n this.__ms_url = String(url);\n return originalOpen.apply(this, [method, url, ...rest] as Parameters<\n typeof originalOpen\n >);\n } as typeof XMLHttpRequest.prototype.open;\n\n XMLHttpRequest.prototype.send = function (\n this: XMLHttpRequest & {\n __ms_method?: string;\n __ms_url?: string;\n },\n body?: Document | XMLHttpRequestBodyInit | null,\n ): void {\n const method = this.__ms_method || 'GET';\n const url = this.__ms_url || '';\n\n if (shouldSkipUrl(url)) {\n return originalSend.apply(this, [body] as Parameters<\n typeof originalSend\n >);\n }\n\n const startTime = Date.now();\n\n this.addEventListener(\n 'loadend',\n function (this: XMLHttpRequest) {\n push({\n type: 'xhr',\n method,\n url,\n status: this.status,\n ok: this.status >= 200 && this.status < 300,\n duration: Date.now() - startTime,\n timestamp: startTime,\n });\n },\n { once: true },\n );\n\n return originalSend.apply(this, [body] as Parameters<typeof originalSend>);\n } as typeof XMLHttpRequest.prototype.send;\n\n (\n XMLHttpRequest.prototype.open as typeof XMLHttpRequest.prototype.open & {\n __ms_patched: boolean;\n }\n ).__ms_patched = true;\n}\n\nlet _breadcrumbsInstalled = false;\n\n/**\n * Install all three breadcrumb sources. Idempotent.\n *\n * @param opts.captureResponseBodies — when `true`, failed `fetch`\n * responses have their bodies read and attached (truncated to ~1KB).\n * Backend strips this field unless the per-app setting is enabled.\n */\nexport function installBreadcrumbs(opts: {\n captureResponseBodies: boolean;\n}): void {\n if (_breadcrumbsInstalled) {\n return;\n }\n _breadcrumbsInstalled = true;\n\n try {\n installHistory();\n } catch {\n // history patches are best-effort\n }\n try {\n installFetch(opts.captureResponseBodies);\n } catch {\n // fetch patches are best-effort\n }\n try {\n installXhr();\n } catch {\n // xhr patches are best-effort\n }\n}\n","/**\n * Error reporting — crashes batched and shipped to `/_/telemetry/errors`.\n *\n * ## Producers\n *\n * The window is not the design, it is one producer. Everything enters\n * through {@link capture}, and every entrant declares a MECHANISM (which\n * producer reported it) and whether it was HANDLED (did the app degrade\n * gracefully, or did the user hit a wall):\n *\n * | mechanism | source | handled |\n * | ------------------------------ | ----------------------------- | ------- |\n * | `browser.onerror` | window `error` event | false |\n * | `browser.onunhandledrejection` | window rejection event | false |\n * | `react.error_handler` | {@link reactErrorHandler} | false |\n * | `manual` | {@link captureException} | true |\n *\n * This matters because React never tells the window about an error one of\n * its boundaries caught — `onCaughtError` defaults to `console.error` and\n * nothing else — so a render crash in an app with a boundary is invisible\n * to the window listeners. React 19 apps wire the root hooks instead:\n *\n * ```ts\n * createRoot(el, {\n * onUncaughtError: telemetry.reactErrorHandler(),\n * onCaughtError: telemetry.reactErrorHandler(),\n * });\n * ```\n *\n * ## Transport\n *\n * Installed by `maybeInstallTelemetry()`, which `getConfig()` calls on\n * first successful bootstrap read — synchronously at import when the\n * platform bootstrap is present (see the eager-init block in\n * `src/index.ts`), because React's first render task would otherwise run\n * before the listeners existed. Falls back to lazy install on first SDK\n * use if eager init was skipped (SSR / tests / no bootstrap).\n *\n * 1. Producers enqueue into a pending batch (max 50 — the server cap).\n * 2. Events with the same fingerprint within a batch collapse into a\n * single entry with `count: N` (bandwidth optimization).\n * 3. The batch flushes ~1s after the first push via `POST`.\n * 4. On `pagehide` / `visibilitychange='hidden'`, the buffer drains\n * via `fetch` with `keepalive: true` so the Bearer header survives.\n * 5. On `429`, the SDK pauses sends for the `Retry-After` window.\n * 6. Any other transport failure is swallowed — telemetry must never\n * crash the host app.\n *\n * Opt out of every producer, including the manual one, via\n * `window.__MINDSTUDIO__.telemetry = { errors: false }`.\n */\n\nimport { getConfig, withBase } from './config.js';\nimport {\n installBreadcrumbs,\n getBreadcrumbs,\n type Breadcrumb,\n} from './telemetry-breadcrumbs.js';\n\nconst ENDPOINT = '/_/telemetry/errors';\nconst FLUSH_INTERVAL_MS = 1000;\nconst MAX_BATCH_SIZE = 50;\nconst DEFAULT_RETRY_AFTER_MS = 60_000;\n\n/**\n * Which producer reported a crash. Server-side vocabulary; an unrecognized\n * string is stored as `unknown` rather than dropped.\n */\nexport type ErrorMechanism =\n | 'browser.onerror'\n | 'browser.onunhandledrejection'\n | 'react.error_handler'\n | 'manual';\n\ninterface ErrorEvent {\n releaseId: string;\n url: string;\n userAgent: string;\n timestamp: number;\n mechanism: string;\n handled: boolean;\n message: string;\n stack: string;\n componentStack?: string;\n source?: string;\n line?: number;\n column?: number;\n breadcrumbs: Breadcrumb[];\n count?: number;\n /** Internal: fingerprint for within-batch dedupe. Stripped before send. */\n _fp?: string;\n}\n\nlet _installed = false;\nlet pending: ErrorEvent[] = [];\nlet flushTimer: ReturnType<typeof setTimeout> | null = null;\nlet suppressUntil = 0;\n\nfunction fingerprint(message: string, stack: string): string {\n const firstStackLine = stack.split('\\n')[1] ?? '';\n return `${message}|${firstStackLine.trim()}`;\n}\n\nfunction enqueue(event: ErrorEvent): void {\n const fp = fingerprint(event.message, event.stack);\n\n for (const existing of pending) {\n if (existing._fp === fp) {\n existing.count = (existing.count ?? 1) + 1;\n return;\n }\n }\n\n if (pending.length >= MAX_BATCH_SIZE) {\n return;\n }\n\n event._fp = fp;\n pending.push(event);\n scheduleFlush();\n}\n\nfunction scheduleFlush(): void {\n if (flushTimer || pending.length === 0) {\n return;\n }\n flushTimer = setTimeout(() => {\n flushTimer = null;\n void flush();\n }, FLUSH_INTERVAL_MS);\n}\n\nfunction takeBatch(): ErrorEvent[] {\n const batch = pending;\n pending = [];\n return batch.map(({ _fp, ...rest }) => {\n void _fp;\n return rest;\n });\n}\n\nasync function flush(): Promise<void> {\n if (pending.length === 0) {\n return;\n }\n\n if (Date.now() < suppressUntil) {\n pending = [];\n return;\n }\n\n const config = (() => {\n try {\n return getConfig();\n } catch {\n return null;\n }\n })();\n if (!config) {\n pending = [];\n return;\n }\n\n const events = takeBatch();\n\n try {\n const res = await fetch(withBase(ENDPOINT), {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${config.token}`,\n },\n body: JSON.stringify({ events }),\n });\n\n if (res.status === 429) {\n const retryAfterHeader = res.headers.get('Retry-After');\n const retryAfterSec = retryAfterHeader\n ? parseInt(retryAfterHeader, 10)\n : NaN;\n const waitMs =\n Number.isFinite(retryAfterSec) && retryAfterSec > 0\n ? retryAfterSec * 1000\n : DEFAULT_RETRY_AFTER_MS;\n suppressUntil = Date.now() + waitMs;\n }\n } catch {\n // network / serialization failure — drop silently\n }\n}\n\nfunction drainOnUnload(): void {\n if (pending.length === 0) {\n return;\n }\n if (Date.now() < suppressUntil) {\n pending = [];\n return;\n }\n\n let config: ReturnType<typeof getConfig> | null = null;\n try {\n config = getConfig();\n } catch {\n pending = [];\n return;\n }\n\n const events = takeBatch();\n\n try {\n void fetch(withBase(ENDPOINT), {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${config.token}`,\n },\n body: JSON.stringify({ events }),\n keepalive: true,\n });\n } catch {\n // best effort\n }\n}\n\n/**\n * Read a message + stack off anything a producer might hand us: an Error,\n * a rejection reason, a string, a DOM exception, `undefined`.\n */\nfunction describe(\n error: unknown,\n fallbackMessage: string,\n): { message: string; stack: string } {\n if (error instanceof Error) {\n return {\n message: error.message || fallbackMessage,\n stack: error.stack ?? '',\n };\n }\n if (error && typeof error === 'object') {\n const shaped = error as { message?: unknown; stack?: unknown };\n return {\n message:\n typeof shaped.message === 'string' && shaped.message\n ? shaped.message\n : String(error),\n stack: typeof shaped.stack === 'string' ? shaped.stack : '',\n };\n }\n return { message: String(error ?? fallbackMessage), stack: '' };\n}\n\n/**\n * The one door into the queue. Every producer goes through here, so the\n * errors opt-out, the bootstrap read and the never-throw guarantee are all\n * enforced in one place.\n */\nfunction capture(\n error: unknown,\n options: {\n mechanism: string;\n handled: boolean;\n fallbackMessage?: string;\n /** Overrides for producers that know better than `error` does. */\n message?: string;\n componentStack?: string | null;\n source?: string;\n line?: number;\n column?: number;\n },\n): void {\n try {\n const config = getConfig();\n if (config.telemetry?.errors === false) {\n return;\n }\n\n const described = describe(\n error,\n options.fallbackMessage ?? 'Unknown error',\n );\n\n enqueue({\n releaseId: config.releaseId,\n url: location.href,\n userAgent: navigator.userAgent,\n timestamp: Date.now(),\n mechanism: options.mechanism,\n handled: options.handled,\n message: options.message || described.message,\n stack: described.stack,\n ...(options.componentStack\n ? { componentStack: options.componentStack }\n : {}),\n ...(options.source ? { source: options.source } : {}),\n ...(typeof options.line === 'number' ? { line: options.line } : {}),\n ...(typeof options.column === 'number' ? { column: options.column } : {}),\n breadcrumbs: getBreadcrumbs(),\n });\n } catch {\n // never crash on capture\n }\n}\n\nfunction captureError(e: globalThis.ErrorEvent): void {\n capture(e.error, {\n mechanism: 'browser.onerror',\n handled: false,\n // The event's own message survives a cross-origin stack strip; the\n // error object may not be there at all.\n message: e.message,\n source: e.filename || undefined,\n line: typeof e.lineno === 'number' ? e.lineno : undefined,\n column: typeof e.colno === 'number' ? e.colno : undefined,\n });\n}\n\nfunction captureRejection(e: PromiseRejectionEvent): void {\n capture(e.reason, {\n mechanism: 'browser.onunhandledrejection',\n handled: false,\n fallbackMessage: 'Unhandled rejection',\n });\n}\n\n/**\n * Install error capture + breadcrumb collection. Idempotent.\n *\n * Reads `window.__MINDSTUDIO__.telemetry?.errors` — if explicitly\n * `false`, no listeners are attached and the function is a no-op.\n *\n * Otherwise attaches:\n * - `window.error` + `unhandledrejection`\n * - `pagehide` + `visibilitychange === 'hidden'` (for buffer drain)\n * - Breadcrumb sources via {@link installBreadcrumbs}\n */\nexport function installErrorMonitoring(): void {\n if (_installed) {\n return;\n }\n if (typeof window === 'undefined') {\n return;\n }\n\n let config: ReturnType<typeof getConfig>;\n try {\n config = getConfig();\n } catch {\n return;\n }\n\n if (config.telemetry?.errors === false) {\n return;\n }\n\n _installed = true;\n\n installBreadcrumbs({\n captureResponseBodies: !!config.telemetryCaptureResponseBodies,\n });\n\n window.addEventListener('error', captureError);\n window.addEventListener('unhandledrejection', captureRejection);\n\n // Drain on the way out, but stay live: `pagehide` fires when a mobile\n // browser backgrounds the tab, and the page can come back from the\n // back/forward cache and run for hours more. Latching \"unloaded\" here\n // meant every crash after the first backgrounding was dropped.\n window.addEventListener('pagehide', drainOnUnload);\n document.addEventListener('visibilitychange', () => {\n if (document.visibilityState === 'hidden') {\n drainOnUnload();\n }\n });\n}\n\n/**\n * Wrapper that swallows any error from {@link installErrorMonitoring}\n * so a misbehaving telemetry layer can never break `getConfig()`.\n *\n * @internal Called from config.ts on every `getConfig()` — idempotency\n * is enforced inside `installErrorMonitoring`.\n */\nexport function maybeInstallTelemetry(): void {\n try {\n installErrorMonitoring();\n } catch {\n // telemetry must never crash the host app\n }\n}\n\n//////////////////////////////////////////////////////////////////////////////\n// Public API\n//////////////////////////////////////////////////////////////////////////////\n\n/** What React 19's `onCaughtError` / `onUncaughtError` pass as their 2nd arg. */\nexport interface ReactErrorInfo {\n componentStack?: string | null;\n}\n\nexport interface CaptureOptions {\n /**\n * Which producer this came from. Defaults to `manual`. Values outside\n * {@link ErrorMechanism} are accepted but stored as `unknown`.\n */\n mechanism?: ErrorMechanism | string;\n /**\n * Did the app degrade gracefully? Defaults to `true` for a manual\n * capture — you caught it, so something other than a dead page happened.\n */\n handled?: boolean;\n /** React component tree, when you have one. */\n componentStack?: string | null;\n}\n\nexport interface ReactErrorHandlerOptions {\n /**\n * Whether the boundary that caught this rendered a real fallback.\n * Defaults to `false`: a boundary existing is not evidence the app\n * recovered, and the common root boundary is a dead end.\n */\n handled?: boolean;\n /** Called after the report is queued, with React's own arguments. */\n onError?: (error: unknown, errorInfo?: ReactErrorInfo) => void;\n}\n\nexport interface Telemetry {\n captureException(error: unknown, options?: CaptureOptions): void;\n reactErrorHandler(\n options?: ReactErrorHandlerOptions,\n ): (error: unknown, errorInfo?: ReactErrorInfo) => void;\n}\n\n/**\n * Crash reporting for cases the window can't see.\n *\n * @example Report something you caught yourself\n * ```ts\n * try {\n * await api.submitOrder(order);\n * } catch (err) {\n * telemetry.captureException(err);\n * setError('Could not submit that order.');\n * }\n * ```\n *\n * @example Wire React 19's root hooks (required to see render crashes)\n * ```tsx\n * createRoot(document.getElementById('root')!, {\n * onUncaughtError: telemetry.reactErrorHandler(),\n * onCaughtError: telemetry.reactErrorHandler(),\n * }).render(<App />);\n * ```\n */\nexport const telemetry: Telemetry = {\n captureException(error, options) {\n capture(error, {\n mechanism: options?.mechanism ?? 'manual',\n handled: options?.handled ?? true,\n ...(options?.componentStack\n ? { componentStack: options.componentStack }\n : {}),\n });\n },\n\n reactErrorHandler(options) {\n return (error, errorInfo) => {\n capture(error, {\n mechanism: 'react.error_handler',\n handled: options?.handled ?? false,\n ...(errorInfo?.componentStack\n ? { componentStack: errorInfo.componentStack }\n : {}),\n });\n options?.onError?.(error, errorInfo);\n };\n },\n};\n","/**\n * Bootstrap configuration reader.\n *\n * Reads and validates `window.__MINDSTUDIO__` — the globals injected by\n * the platform before the app's JavaScript runs. The result is cached\n * after first read.\n *\n * All other modules in the SDK call `getConfig()` internally. App code\n * should never need to call this directly — use `createClient()`, `auth`,\n * and `platform` instead.\n */\n\nimport { MindStudioInterfaceError } from './errors.js';\nimport { maybeInstallAnalytics } from './telemetry-analytics.js';\nimport { maybeInstallTelemetry } from './telemetry-errors.js';\nimport type { AppUser, BootstrapConfig } from './types.js';\n\nlet _config: BootstrapConfig | undefined;\n\n/**\n * Read and validate the bootstrap configuration from `window.__MINDSTUDIO__`.\n *\n * Caches the result after first successful read. Throws if the globals\n * are missing or incomplete — this means the page isn't running inside\n * the MindStudio platform.\n *\n * @returns The validated bootstrap config\n * @throws {MindStudioInterfaceError} if `window.__MINDSTUDIO__` is missing or invalid\n */\nexport function getConfig(): BootstrapConfig {\n if (_config) {\n return _config;\n }\n\n const raw = (globalThis as Record<string, unknown>).__MINDSTUDIO__ as\n | Partial<BootstrapConfig>\n | undefined;\n\n if (!raw) {\n throw new MindStudioInterfaceError(\n '@mindstudio-ai/interface requires the MindStudio platform context. ' +\n 'This page must be loaded inside MindStudio (window.__MINDSTUDIO__ is missing).',\n 'not_initialized',\n );\n }\n\n // Validate required fields (user can be null for unauthenticated sessions)\n if (!raw.token || !raw.releaseId || !raw.methods) {\n const missing = ['token', 'releaseId', 'methods']\n .filter((k) => !raw[k as keyof BootstrapConfig])\n .join(', ');\n\n throw new MindStudioInterfaceError(\n `MindStudio platform context is incomplete. Missing: ${missing}`,\n 'invalid_config',\n );\n }\n\n _config = raw as BootstrapConfig;\n\n // Auto-install telemetry surfaces on first successful config read.\n // Both are idempotent + try/caught — telemetry must never break getConfig().\n //\n // Errors install synchronously: the listeners have to exist before React's\n // first render task, which is the whole reason index.ts calls this eagerly.\n // Analytics waits a tick — its first pageview reads the URL, and taking it\n // after the app's router has had a chance to normalize it is the more\n // accurate reading, quite apart from keeping a presence connection off the\n // critical path.\n maybeInstallTelemetry();\n setTimeout(() => {\n maybeInstallAnalytics();\n }, 0);\n\n return _config;\n}\n\n/**\n * The mount prefix this app is served under on the current host, normalized\n * to '' (served at the root — its own subdomain/custom host) or a\n * leading-slash, no-trailing-slash path like '/demos/vector-databases'\n * (served under a parent app's mount).\n *\n * Reads the raw bootstrap directly and never throws, so telemetry and\n * unload-time beacons can call it unconditionally. Use it as the router\n * basename: `createBrowserRouter(routes, { basename: platform.basePath || '/' })`.\n */\nexport function getBasePath(): string {\n const raw = (\n (globalThis as Record<string, unknown>).__MINDSTUDIO__ as\n | Partial<BootstrapConfig>\n | undefined\n )?.basePath;\n if (typeof raw !== 'string') {\n return '';\n }\n const trimmed = raw.replace(/\\/+$/, '');\n if (!trimmed) {\n return '';\n }\n return trimmed.startsWith('/') ? trimmed : `/${trimmed}`;\n}\n\n/**\n * Prefix a root-relative `/_/...` path with the mount basePath. Identity when\n * the app serves at the root — every platform call in the SDK goes through\n * this so mounted apps talk to their own `/_/` namespace on the parent host.\n */\nexport function withBase(path: string): string {\n return `${getBasePath()}${path}`;\n}\n\n/**\n * Update the cached config in-place after an auth state transition.\n *\n * The cached config is a mutable reference — all SDK modules read it\n * on-demand via `getConfig()`, so mutations propagate immediately to\n * method invocation, agent chat, uploads, etc.\n *\n * @internal Not exported from the package — used by the auth module only.\n */\nexport function updateConfig(updates: {\n token?: string;\n user?: AppUser | null;\n methods?: Record<string, string>;\n visitorId?: string;\n}): void {\n const config = getConfig();\n if (updates.token !== undefined) {\n config.token = updates.token;\n }\n if (updates.user !== undefined) {\n config.user = updates.user;\n }\n if (updates.methods !== undefined) {\n config.methods = updates.methods;\n }\n if (updates.visitorId !== undefined) {\n config.visitorId = updates.visitorId;\n }\n}\n","/**\n * Long-lived SSE connection loop — the reconnect discipline shared by the\n * presence heartbeat (telemetry-analytics.ts) and the app-events client\n * (events.ts). Extracted from the presence client rather than copied, so the\n * hard-won rules can't drift between consumers:\n *\n * - Exponential backoff with jitter, capped, and a STABLE-CONNECTION\n * threshold before the backoff resets — resetting on a bare 200 lets a\n * stream that dies instantly reconnect every ~1s forever, so a connection\n * only earns the reset by staying open.\n * - `Retry-After` honored on 503 (the server's capacity signal).\n * - `fetch` + reader, never `EventSource`, so credentials ride the\n * `Authorization` header instead of a URL.\n *\n * Policy stays in the consumer: `prepare` builds each attempt's request (and\n * can stop the loop), `onStatus` decides what a non-OK response means, and\n * `onLine` interprets the stream. Presence discards lines and stops on\n * 401/204; events parses frames and re-mints its grant.\n */\n\nexport interface SseLoopOptions {\n /**\n * Build the next attempt's request. Called before EVERY connect, so a\n * consumer can refresh credentials per attempt. Return `'stop'` to end the\n * loop (e.g. config gone, credential refusal).\n */\n prepare: () =>\n | Promise<{ url: string; headers: Record<string, string> } | 'stop'>\n | { url: string; headers: Record<string, string> }\n | 'stop';\n /**\n * Policy for any unreadable response — an error status, or a bodyless\n * success like 204. `'retry'` schedules a backoff reconnect; `'stop'` ends\n * the loop. 503 never reaches this hook (the loop honors Retry-After\n * itself). Default: `'retry'`.\n */\n onStatus?: (status: number) => 'retry' | 'stop';\n /** One complete line from the stream, `\\n`-split, terminator stripped. */\n onLine?: (line: string) => void;\n /** The stream opened (2xx with a body). Fires on every (re)connect. */\n onOpen?: () => void;\n /** The loop ended for good — `stop()`, a `'stop'` policy, or `prepare` said so. */\n onStop?: () => void;\n}\n\nexport interface SseLoop {\n start(): void;\n stop(): void;\n readonly state: 'idle' | 'connecting' | 'open' | 'stopped';\n}\n\n/** @internal Timing knobs, injectable so tests don't wait wall-clock. */\nexport interface SseLoopTiming {\n initialBackoffMs: number;\n maxBackoffMs: number;\n /** Minimum open-stream lifetime that earns a backoff reset. */\n stableMs: number;\n /** Wait after a 503 with no usable Retry-After. */\n retry503DefaultMs: number;\n}\n\nconst DEFAULT_TIMING: SseLoopTiming = {\n initialBackoffMs: 1000,\n maxBackoffMs: 10_000,\n stableMs: 30_000,\n retry503DefaultMs: 30_000,\n};\n\nexport function createSseLoop(\n options: SseLoopOptions,\n timing: SseLoopTiming = DEFAULT_TIMING,\n): SseLoop {\n let active = false;\n let state: SseLoop['state'] = 'idle';\n let abort: AbortController | null = null;\n let reconnectTimer: ReturnType<typeof setTimeout> | null = null;\n let backoffMs = timing.initialBackoffMs;\n\n const nextBackoff = (): number => {\n const base = backoffMs;\n backoffMs = Math.min(backoffMs * 2, timing.maxBackoffMs);\n return base + Math.random() * 250;\n };\n\n const scheduleReconnect = (ms: number): void => {\n if (reconnectTimer) {\n clearTimeout(reconnectTimer);\n }\n reconnectTimer = setTimeout(() => {\n reconnectTimer = null;\n if (active) {\n void connect();\n }\n }, ms);\n };\n\n const halt = (): void => {\n if (!active && state === 'stopped') {\n return;\n }\n active = false;\n state = 'stopped';\n if (reconnectTimer) {\n clearTimeout(reconnectTimer);\n reconnectTimer = null;\n }\n if (abort) {\n try {\n abort.abort();\n } catch {\n // swallow\n }\n abort = null;\n }\n options.onStop?.();\n };\n\n async function connect(): Promise<void> {\n if (!active) {\n return;\n }\n state = 'connecting';\n\n let request: { url: string; headers: Record<string, string> } | 'stop';\n try {\n request = await options.prepare();\n } catch {\n // prepare() throwing is a consumer bug or a deliberate refusal it\n // should have expressed as 'stop' — either way, looping on it is worse.\n halt();\n return;\n }\n if (request === 'stop' || !active) {\n halt();\n return;\n }\n\n abort = new AbortController();\n const signal = abort.signal;\n\n try {\n const res = await fetch(request.url, {\n method: 'GET',\n headers: { ...request.headers, Accept: 'text/event-stream' },\n signal,\n });\n\n if (!active) {\n return;\n }\n\n if (res.status === 503) {\n const retryAfterSec = parseInt(\n res.headers.get('Retry-After') ?? '',\n 10,\n );\n const waitMs =\n Number.isFinite(retryAfterSec) && retryAfterSec > 0\n ? retryAfterSec * 1000\n : timing.retry503DefaultMs;\n scheduleReconnect(waitMs);\n return;\n }\n\n // Anything unreadable — an error status, or a bodyless success like 204\n // (which the dev-tunnel presence mock uses to mean \"don't reconnect\") —\n // goes through the consumer's policy hook.\n if (!res.ok || !res.body) {\n if ((options.onStatus?.(res.status) ?? 'retry') === 'stop') {\n halt();\n } else {\n scheduleReconnect(nextBackoff());\n }\n return;\n }\n\n // Note the connect time — but don't reset the backoff yet. Only a\n // stream that stays open long enough to be healthy earns the reset.\n const connectedAt = Date.now();\n state = 'open';\n options.onOpen?.();\n\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n while (active) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n if (!options.onLine) {\n continue; // drain-and-discard consumer (presence)\n }\n buffer += decoder.decode(value, { stream: true });\n const lines = buffer.split('\\n');\n buffer = lines.pop() ?? '';\n for (const line of lines) {\n options.onLine(line.replace(/\\r$/, ''));\n }\n }\n\n if (active) {\n if (Date.now() - connectedAt >= timing.stableMs) {\n backoffMs = timing.initialBackoffMs;\n }\n state = 'connecting';\n scheduleReconnect(nextBackoff());\n }\n } catch (err) {\n const name = (err as { name?: string } | null)?.name;\n if (active && name !== 'AbortError') {\n state = 'connecting';\n scheduleReconnect(nextBackoff());\n }\n }\n }\n\n return {\n start() {\n if (active) {\n return;\n }\n active = true;\n void connect();\n },\n stop: halt,\n get state() {\n return state;\n },\n };\n}\n","/**\n * Analytics — Plausible/Fathom-style visitor analytics.\n *\n * Auto-tracks pageviews via the existing history patches in\n * `telemetry-breadcrumbs.ts` and exposes a small public API:\n *\n * ```ts\n * import { analytics } from '@mindstudio-ai/interface';\n *\n * // Custom events (optional — pageviews track automatically)\n * analytics.track('vendor_submitted', { vendorType: 'restaurant' });\n * ```\n *\n * Server-side derivation handles geo (IP → country), device class\n * (UA → mobile/tablet/desktop), referrers, UTM parsing, and\n * sessionization. The SDK ships nothing for those — just raw\n * pageview events with enough context for backend enrichment.\n *\n * ## How it works\n *\n * Installed by `maybeInstallAnalytics()`, which is called by `getConfig()`\n * in `config.ts`. `getConfig()` runs automatically on the next tick after\n * SDK import when the platform bootstrap is present (see eager-init block\n * in `src/index.ts`), so pageview tracking + presence are live at page\n * load without app code needing to invoke anything. Falls back to lazy\n * install on first SDK use if eager init was skipped.\n *\n * Once installed:\n *\n * 1. Subscribes to navigation events from `telemetry-breadcrumbs.ts`\n * and emits a pageview on each change.\n * 2. Fires an initial pageview for the first-load URL.\n * 3. Batches events (~1s debounce, 100/batch cap) and POSTs to\n * `/_/telemetry/events`.\n * 4. On `pagehide` / `visibilitychange='hidden'`, drains via\n * `fetch` with `keepalive: true`.\n * 5. On `429`, pauses sends for the `Retry-After` window.\n *\n * ## Privacy posture\n *\n * Live visitor counts are intentionally **not** exposed to app code.\n * Aggregate visitor metrics (live count included) are surfaced only\n * through the platform dashboard to the app owner — visitors should\n * not be able to learn about other visitors' presence through the\n * SDK. If an app explicitly wants user-facing presence as a designed\n * feature (e.g. a multiplayer experience), it must build that on its\n * own application-level abstraction with its own consent semantics,\n * not on platform telemetry.\n *\n * Opt-out via `window.__MINDSTUDIO__.telemetry = { analytics: false }`.\n */\n\nimport { getConfig, withBase } from './config.js';\nimport { onNavigation } from './telemetry-breadcrumbs.js';\nimport { createSseLoop } from './sse-loop.js';\n\n// ---------------------------------------------------------------------------\n// Public types\n// ---------------------------------------------------------------------------\n\n/**\n * Flat-primitive prop values for custom events.\n *\n * Backend caps: max 10 keys, key length ~50 chars, value length ~500 chars.\n * Non-primitive values (objects, arrays, null) are stripped client-side\n * before send to save bandwidth — backend would silently drop them anyway.\n */\nexport type CustomEventProps = Record<string, string | number | boolean>;\n\n/**\n * The analytics namespace — pageview auto-tracking + custom events.\n *\n * Pageviews are auto-tracked from history changes; no public API for\n * them. Opt out entirely via `window.__MINDSTUDIO__.telemetry.analytics = false`.\n */\nexport interface AnalyticsClient {\n /**\n * Record a custom event with optional flat-primitive props.\n *\n * Non-primitive prop values (objects, arrays, null, undefined) are\n * stripped client-side before send. Server further caps name length\n * to 200 chars, props to 10 keys, key length to 50, value length to 500.\n *\n * Best-effort — never throws, never blocks. If the SDK isn't\n * initialized yet, the call is silently dropped.\n */\n track(event: string, props?: CustomEventProps): void;\n}\n\n// ---------------------------------------------------------------------------\n// Internal event types\n// ---------------------------------------------------------------------------\n\ntype PageviewEvent = {\n type: 'pageview';\n releaseId: string;\n url: string;\n referrer: string;\n userAgent: string;\n language: string;\n screen: { w: number; h: number };\n timestamp: number;\n};\n\ntype CustomEvent = {\n type: 'event';\n releaseId: string;\n name: string;\n url: string;\n timestamp: number;\n props?: CustomEventProps;\n};\n\ntype AnalyticsEvent = PageviewEvent | CustomEvent;\n\n// ---------------------------------------------------------------------------\n// Batched event transport\n// ---------------------------------------------------------------------------\n\nconst EVENTS_ENDPOINT = '/_/telemetry/events';\nconst FLUSH_INTERVAL_MS = 1000;\nconst MAX_BATCH_SIZE = 100;\nconst DEFAULT_RETRY_AFTER_MS = 60_000;\n\nlet pending: AnalyticsEvent[] = [];\nlet flushTimer: ReturnType<typeof setTimeout> | null = null;\nlet suppressUntil = 0;\nlet unloaded = false;\n\nfunction enqueue(event: AnalyticsEvent): void {\n if (unloaded) {\n return;\n }\n if (pending.length >= MAX_BATCH_SIZE) {\n return;\n }\n pending.push(event);\n scheduleFlush();\n}\n\nfunction scheduleFlush(): void {\n if (flushTimer || pending.length === 0) {\n return;\n }\n flushTimer = setTimeout(() => {\n flushTimer = null;\n void flush();\n }, FLUSH_INTERVAL_MS);\n}\n\nfunction takeBatch(): AnalyticsEvent[] {\n const batch = pending;\n pending = [];\n return batch;\n}\n\nasync function flush(): Promise<void> {\n if (pending.length === 0) {\n return;\n }\n\n if (Date.now() < suppressUntil) {\n pending = [];\n return;\n }\n\n let config;\n try {\n config = getConfig();\n } catch {\n pending = [];\n return;\n }\n\n const events = takeBatch();\n\n try {\n const res = await fetch(withBase(EVENTS_ENDPOINT), {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${config.token}`,\n },\n body: JSON.stringify({ events }),\n });\n\n if (res.status === 429) {\n const retryAfterHeader = res.headers.get('Retry-After');\n const retryAfterSec = retryAfterHeader\n ? parseInt(retryAfterHeader, 10)\n : NaN;\n const waitMs =\n Number.isFinite(retryAfterSec) && retryAfterSec > 0\n ? retryAfterSec * 1000\n : DEFAULT_RETRY_AFTER_MS;\n suppressUntil = Date.now() + waitMs;\n }\n } catch {\n // network / serialization failure — drop silently\n }\n}\n\nfunction drainOnUnload(): void {\n if (pending.length === 0) {\n return;\n }\n if (Date.now() < suppressUntil) {\n pending = [];\n return;\n }\n\n let config;\n try {\n config = getConfig();\n } catch {\n pending = [];\n return;\n }\n\n const events = takeBatch();\n\n try {\n void fetch(withBase(EVENTS_ENDPOINT), {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${config.token}`,\n },\n body: JSON.stringify({ events }),\n keepalive: true,\n });\n } catch {\n // best effort\n }\n}\n\n// ---------------------------------------------------------------------------\n// Custom event prop sanitization\n// ---------------------------------------------------------------------------\n\nfunction sanitizeProps(\n input?: Record<string, unknown>,\n): CustomEventProps | undefined {\n if (!input) {\n return undefined;\n }\n const out: CustomEventProps = {};\n for (const [k, v] of Object.entries(input)) {\n if (\n typeof v === 'string' ||\n typeof v === 'number' ||\n typeof v === 'boolean'\n ) {\n out[k] = v;\n }\n }\n return Object.keys(out).length > 0 ? out : undefined;\n}\n\n// ---------------------------------------------------------------------------\n// Pageview emit\n// ---------------------------------------------------------------------------\n\nfunction emitPageview(url: string): void {\n try {\n const config = getConfig();\n enqueue({\n type: 'pageview',\n releaseId: config.releaseId,\n url,\n referrer: document.referrer,\n userAgent: navigator.userAgent,\n language: navigator.language,\n screen: { w: window.innerWidth, h: window.innerHeight },\n timestamp: Date.now(),\n });\n } catch {\n // capture must never crash\n }\n}\n\n// ---------------------------------------------------------------------------\n// Public `track`\n// ---------------------------------------------------------------------------\n\nfunction track(event: string, props?: CustomEventProps): void {\n try {\n const config = getConfig();\n enqueue({\n type: 'event',\n releaseId: config.releaseId,\n name: event,\n url: location.href,\n timestamp: Date.now(),\n props: sanitizeProps(props as Record<string, unknown> | undefined),\n });\n } catch {\n // never crash\n }\n}\n\n// ---------------------------------------------------------------------------\n// Silent presence connection\n//\n// The SDK opens a long-lived SSE connection to `/_/telemetry/presence`\n// while the tab is alive. The connection itself is the presence signal:\n// while it's open, the visitor is tracked as \"online\" in the server-side\n// Redis set; when the tab closes (or network drops), the connection\n// closes and the server immediately knows the visitor is gone.\n//\n// Any data the server pushes over this connection is silently discarded.\n// The visitor-facing live count is intentionally NOT exposed by the SDK.\n// The owner-facing dashboard reads counts via a separate authenticated\n// internal endpoint, never through the SDK.\n//\n// Reading-and-discarding the stream is required to keep the underlying\n// TCP buffer flowing — the open connection itself is what matters, not\n// the data flowing over it.\n// ---------------------------------------------------------------------------\n\nconst PRESENCE_ENDPOINT = '/_/telemetry/presence';\n\n// The connection machinery — backoff + jitter, the stable-connection rule,\n// 503 Retry-After, abort lifecycle — lives in sse-loop.ts, shared with the\n// app-events client (events.ts). Only presence's POLICY lives here: stop for\n// good on 401 (session bad — nothing to do until something refreshes config)\n// and on 204 (presence stubbed/disabled — the dev-tunnel mock relies on\n// EventSource semantics, where a 204 means \"abandon and do NOT reconnect\").\n// No onLine handler: lines are drained and discarded inside the loop, since\n// the open connection is the presence signal and the visitor-facing SDK\n// deliberately exposes no counts.\nconst presenceLoop = createSseLoop({\n prepare: () => {\n let config;\n try {\n config = getConfig();\n } catch {\n return 'stop';\n }\n return {\n url: withBase(PRESENCE_ENDPOINT),\n headers: { Authorization: `Bearer ${config.token}` },\n };\n },\n onStatus: (status) => (status === 401 || status === 204 ? 'stop' : 'retry'),\n});\n\nfunction closePresenceConnection(): void {\n presenceLoop.stop();\n}\n\n// ---------------------------------------------------------------------------\n// Public client\n// ---------------------------------------------------------------------------\n\nexport const analytics: AnalyticsClient = { track };\n\n// ---------------------------------------------------------------------------\n// Install + opt-out\n// ---------------------------------------------------------------------------\n\nlet _installed = false;\n\n/**\n * Install pageview auto-tracking + transport. Idempotent.\n *\n * Reads `window.__MINDSTUDIO__.telemetry?.analytics` — if explicitly\n * `false`, the SDK installs nothing and the function is a no-op.\n * `track()` calls also no-op in that case since enqueued events\n * have no flush path established.\n */\nexport function installAnalytics(): void {\n if (_installed) {\n return;\n }\n if (typeof window === 'undefined') {\n return;\n }\n\n let config;\n try {\n config = getConfig();\n } catch {\n return;\n }\n\n if (config.telemetry?.analytics === false) {\n return;\n }\n\n _installed = true;\n\n // Unload listeners (attached once)\n const handleUnload = (): void => {\n unloaded = true;\n drainOnUnload();\n closePresenceConnection();\n };\n window.addEventListener('pagehide', handleUnload);\n document.addEventListener('visibilitychange', () => {\n if (document.visibilityState === 'hidden') {\n drainOnUnload();\n }\n });\n\n // Auto-pageviews on every history change\n onNavigation((url) => {\n emitPageview(url);\n });\n\n // Initial pageview\n emitPageview(location.href);\n\n // Silent presence heartbeat — the open connection itself signals\n // \"this visitor is online\" to the server. No data is consumed.\n presenceLoop.start();\n}\n\n/**\n * Wrapper that swallows any error from {@link installAnalytics} so a\n * misbehaving telemetry layer can never break `getConfig()`.\n *\n * @internal Called from config.ts on every `getConfig()` — idempotency\n * is enforced inside `installAnalytics`.\n */\nexport function maybeInstallAnalytics(): void {\n try {\n installAnalytics();\n } catch {\n // telemetry must never crash the host app\n }\n}\n"],"mappings":"AAOO,IAAMA,EAAN,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iB,CACnB,MAAQ,CAER,CACF","names":["MindStudioInterfaceError","message","code","status","TELEMETRY_ENDPOINT_FRAGMENT","buffer","push","entry","getBreadcrumbs","shouldSkipUrl","url","TELEMETRY_ENDPOINT_FRAGMENT","resolveUrl","input","lastHref","navListeners","onNavigation","cb","emitNavigation","to","installHistory","w","originalPushState","data","unused","originalReplaceState","installFetch","captureResponseBodies","originalFetch","patched","init","method","startTime","response","body","err","installXhr","originalOpen","originalSend","rest","_breadcrumbsInstalled","installBreadcrumbs","opts","ENDPOINT","FLUSH_INTERVAL_MS","MAX_BATCH_SIZE","DEFAULT_RETRY_AFTER_MS","_installed","pending","flushTimer","suppressUntil","fingerprint","message","stack","firstStackLine","enqueue","event","fp","existing","scheduleFlush","flush","takeBatch","batch","_fp","rest","config","getConfig","events","res","withBase","retryAfterHeader","retryAfterSec","waitMs","drainOnUnload","describe","error","fallbackMessage","shaped","capture","options","described","getBreadcrumbs","captureError","captureRejection","installErrorMonitoring","installBreadcrumbs","maybeInstallTelemetry","telemetry","errorInfo","_config","getConfig","raw","MindStudioInterfaceError","missing","k","maybeInstallTelemetry","maybeInstallAnalytics","getBasePath","trimmed","withBase","path","updateConfig","updates","config","DEFAULT_TIMING","createSseLoop","options","timing","active","state","abort","reconnectTimer","backoffMs","nextBackoff","base","scheduleReconnect","ms","connect","halt","request","signal","res","retryAfterSec","waitMs","connectedAt","reader","decoder","buffer","done","value","lines","line","err","name","EVENTS_ENDPOINT","FLUSH_INTERVAL_MS","MAX_BATCH_SIZE","DEFAULT_RETRY_AFTER_MS","pending","flushTimer","suppressUntil","unloaded","enqueue","event","scheduleFlush","flush","takeBatch","batch","config","getConfig","events","res","withBase","retryAfterHeader","retryAfterSec","waitMs","drainOnUnload","sanitizeProps","input","out","k","v","emitPageview","url","track","props","PRESENCE_ENDPOINT","presenceLoop","createSseLoop","status","closePresenceConnection","analytics","_installed","installAnalytics","handleUnload","onNavigation","maybeInstallAnalytics"]}
package/dist/index.d.ts CHANGED
@@ -1209,6 +1209,117 @@ interface AnalyticsClient {
1209
1209
  }
1210
1210
  declare const analytics: AnalyticsClient;
1211
1211
 
1212
+ /**
1213
+ * Error reporting — crashes batched and shipped to `/_/telemetry/errors`.
1214
+ *
1215
+ * ## Producers
1216
+ *
1217
+ * The window is not the design, it is one producer. Everything enters
1218
+ * through {@link capture}, and every entrant declares a MECHANISM (which
1219
+ * producer reported it) and whether it was HANDLED (did the app degrade
1220
+ * gracefully, or did the user hit a wall):
1221
+ *
1222
+ * | mechanism | source | handled |
1223
+ * | ------------------------------ | ----------------------------- | ------- |
1224
+ * | `browser.onerror` | window `error` event | false |
1225
+ * | `browser.onunhandledrejection` | window rejection event | false |
1226
+ * | `react.error_handler` | {@link reactErrorHandler} | false |
1227
+ * | `manual` | {@link captureException} | true |
1228
+ *
1229
+ * This matters because React never tells the window about an error one of
1230
+ * its boundaries caught — `onCaughtError` defaults to `console.error` and
1231
+ * nothing else — so a render crash in an app with a boundary is invisible
1232
+ * to the window listeners. React 19 apps wire the root hooks instead:
1233
+ *
1234
+ * ```ts
1235
+ * createRoot(el, {
1236
+ * onUncaughtError: telemetry.reactErrorHandler(),
1237
+ * onCaughtError: telemetry.reactErrorHandler(),
1238
+ * });
1239
+ * ```
1240
+ *
1241
+ * ## Transport
1242
+ *
1243
+ * Installed by `maybeInstallTelemetry()`, which `getConfig()` calls on
1244
+ * first successful bootstrap read — synchronously at import when the
1245
+ * platform bootstrap is present (see the eager-init block in
1246
+ * `src/index.ts`), because React's first render task would otherwise run
1247
+ * before the listeners existed. Falls back to lazy install on first SDK
1248
+ * use if eager init was skipped (SSR / tests / no bootstrap).
1249
+ *
1250
+ * 1. Producers enqueue into a pending batch (max 50 — the server cap).
1251
+ * 2. Events with the same fingerprint within a batch collapse into a
1252
+ * single entry with `count: N` (bandwidth optimization).
1253
+ * 3. The batch flushes ~1s after the first push via `POST`.
1254
+ * 4. On `pagehide` / `visibilitychange='hidden'`, the buffer drains
1255
+ * via `fetch` with `keepalive: true` so the Bearer header survives.
1256
+ * 5. On `429`, the SDK pauses sends for the `Retry-After` window.
1257
+ * 6. Any other transport failure is swallowed — telemetry must never
1258
+ * crash the host app.
1259
+ *
1260
+ * Opt out of every producer, including the manual one, via
1261
+ * `window.__MINDSTUDIO__.telemetry = { errors: false }`.
1262
+ */
1263
+ /**
1264
+ * Which producer reported a crash. Server-side vocabulary; an unrecognized
1265
+ * string is stored as `unknown` rather than dropped.
1266
+ */
1267
+ type ErrorMechanism = 'browser.onerror' | 'browser.onunhandledrejection' | 'react.error_handler' | 'manual';
1268
+ /** What React 19's `onCaughtError` / `onUncaughtError` pass as their 2nd arg. */
1269
+ interface ReactErrorInfo {
1270
+ componentStack?: string | null;
1271
+ }
1272
+ interface CaptureOptions {
1273
+ /**
1274
+ * Which producer this came from. Defaults to `manual`. Values outside
1275
+ * {@link ErrorMechanism} are accepted but stored as `unknown`.
1276
+ */
1277
+ mechanism?: ErrorMechanism | string;
1278
+ /**
1279
+ * Did the app degrade gracefully? Defaults to `true` for a manual
1280
+ * capture — you caught it, so something other than a dead page happened.
1281
+ */
1282
+ handled?: boolean;
1283
+ /** React component tree, when you have one. */
1284
+ componentStack?: string | null;
1285
+ }
1286
+ interface ReactErrorHandlerOptions {
1287
+ /**
1288
+ * Whether the boundary that caught this rendered a real fallback.
1289
+ * Defaults to `false`: a boundary existing is not evidence the app
1290
+ * recovered, and the common root boundary is a dead end.
1291
+ */
1292
+ handled?: boolean;
1293
+ /** Called after the report is queued, with React's own arguments. */
1294
+ onError?: (error: unknown, errorInfo?: ReactErrorInfo) => void;
1295
+ }
1296
+ interface Telemetry {
1297
+ captureException(error: unknown, options?: CaptureOptions): void;
1298
+ reactErrorHandler(options?: ReactErrorHandlerOptions): (error: unknown, errorInfo?: ReactErrorInfo) => void;
1299
+ }
1300
+ /**
1301
+ * Crash reporting for cases the window can't see.
1302
+ *
1303
+ * @example Report something you caught yourself
1304
+ * ```ts
1305
+ * try {
1306
+ * await api.submitOrder(order);
1307
+ * } catch (err) {
1308
+ * telemetry.captureException(err);
1309
+ * setError('Could not submit that order.');
1310
+ * }
1311
+ * ```
1312
+ *
1313
+ * @example Wire React 19's root hooks (required to see render crashes)
1314
+ * ```tsx
1315
+ * createRoot(document.getElementById('root')!, {
1316
+ * onUncaughtError: telemetry.reactErrorHandler(),
1317
+ * onCaughtError: telemetry.reactErrorHandler(),
1318
+ * }).render(<App />);
1319
+ * ```
1320
+ */
1321
+ declare const telemetry: Telemetry;
1322
+
1212
1323
  /**
1213
1324
  * App events client — server→client realtime.
1214
1325
  *
@@ -1258,6 +1369,12 @@ declare const analytics: AnalyticsClient;
1258
1369
  */
1259
1370
  /** One published event, delivered on a channel the grant names. */
1260
1371
  interface AppEvent {
1372
+ /**
1373
+ * Platform-stamped publish id. One publish = one id; if your grant covers
1374
+ * several of the published channels you receive it once per channel, so the
1375
+ * dedupe key is `id + channel`.
1376
+ */
1377
+ id: string;
1261
1378
  channel: string;
1262
1379
  data: unknown;
1263
1380
  /** Publish time, milliseconds since epoch. */
@@ -1278,6 +1395,14 @@ interface EventsConnectOptions {
1278
1395
  * at-most-once, so this is the correctness half of the contract.
1279
1396
  */
1280
1397
  onConnect?: () => void;
1398
+ /**
1399
+ * The server dropped `count` events for this connection because it couldn't
1400
+ * keep up (socket backpressure). Treat it like a reconnect: refetch current
1401
+ * state, same as `onConnect`. Without a handler the gap is silent — the
1402
+ * at-most-once contract already requires reconcile-on-connect, so this is
1403
+ * an extra hint, not a new obligation.
1404
+ */
1405
+ onGap?: (count: number) => void;
1281
1406
  /** Terminal failures only (a `getToken` refusal). Reconnects are silent. */
1282
1407
  onError?: (error: Error) => void;
1283
1408
  }
@@ -1286,6 +1411,21 @@ interface EventsSubscription {
1286
1411
  close(): void;
1287
1412
  /** `connecting` | `open` | `closed` */
1288
1413
  readonly state: 'connecting' | 'open' | 'closed';
1414
+ /**
1415
+ * Publish an ephemeral event on channels the grant carries **publish**
1416
+ * capability for (`events.grant(channels, { publish: [...] })` in the
1417
+ * backend SDK). This is the client-direct fast path — no backend method
1418
+ * runs; the platform fans out directly, so it's the right shape for
1419
+ * cursors, typing indicators, and live stroke batches.
1420
+ *
1421
+ * Fire-and-forget with automatic coalescing: call it per input event (per
1422
+ * mousemove is fine) and the SDK batches to ~40ms flushes. Delivery is
1423
+ * at-most-once and NOTHING here is retried — a stale cursor is worse than a
1424
+ * missing one. Payloads cap at 8k serialized; put a client `seq` in the
1425
+ * payload if receivers need cross-batch ordering. Truth still lives in your
1426
+ * database: commit durable state through a method, signal through this.
1427
+ */
1428
+ publish(channels: string | string[], data: unknown): void;
1289
1429
  }
1290
1430
  declare function connect(options: EventsConnectOptions): EventsSubscription;
1291
1431
  /**
@@ -1359,4 +1499,4 @@ type Breadcrumb = {
1359
1499
  timestamp: number;
1360
1500
  };
1361
1501
 
1362
- export { type AbortablePromise, type AgentChatClient, type AgentChatEvent, type AnalyticsClient, type AppEvent, type AppUser, type Auth, type AuthStatus, type BootstrapConfig, type Breadcrumb, type Country, type CustomEventProps, type EventsConnectOptions, type EventsSubscription, type InvokeOptions, type Message, MindStudioInterfaceError, type SendMessageCallbacks, type SendMessageOptions, type SendMessageResult, type SignInWithRemyOptions, type Thread, type ThreadListPage, type ThreadSummary, type UploadFileOptions, type UploadToken, analytics, auth, createAgentChatClient, createClient, events, platform };
1502
+ export { type AbortablePromise, type AgentChatClient, type AgentChatEvent, type AnalyticsClient, type AppEvent, type AppUser, type Auth, type AuthStatus, type BootstrapConfig, type Breadcrumb, type CaptureOptions, type Country, type CustomEventProps, type ErrorMechanism, type EventsConnectOptions, type EventsSubscription, type InvokeOptions, type Message, MindStudioInterfaceError, type ReactErrorHandlerOptions, type ReactErrorInfo, type SendMessageCallbacks, type SendMessageOptions, type SendMessageResult, type SignInWithRemyOptions, type Telemetry, type Thread, type ThreadListPage, type ThreadSummary, type UploadFileOptions, type UploadToken, analytics, auth, createAgentChatClient, createClient, events, platform, telemetry };