@runtypelabs/persona 4.33.0 → 4.35.1

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.
Files changed (220) hide show
  1. package/dist/activity-ui.d.cts +3 -15
  2. package/dist/activity-ui.d.ts +3 -15
  3. package/dist/animations/glyph-cycle.d.cts +1 -3
  4. package/dist/animations/glyph-cycle.d.ts +1 -3
  5. package/dist/animations/wipe.d.cts +1 -3
  6. package/dist/animations/wipe.d.ts +1 -3
  7. package/dist/approval-ui.cjs +2 -2
  8. package/dist/approval-ui.d.cts +3 -15
  9. package/dist/approval-ui.d.ts +3 -15
  10. package/dist/approval-ui.js +1 -1
  11. package/dist/artifacts-ui.cjs +1 -1
  12. package/dist/artifacts-ui.d.cts +3 -21
  13. package/dist/artifacts-ui.d.ts +3 -21
  14. package/dist/background-runtime.d.cts +2 -3
  15. package/dist/background-runtime.d.ts +2 -3
  16. package/dist/{chunk-DW7Vg_l2.js → chunk-B1P8w9_X.js} +1 -1
  17. package/dist/{chunk-D6U0E82p.js → chunk-B30Jdgg6.js} +1 -1
  18. package/dist/{chunk-gu34lr-P.js → chunk-Brdx4NCz.js} +1 -1
  19. package/dist/{chunk-6CUrH_jm.js → chunk-Bzo9GaAi.js} +1 -1
  20. package/dist/{chunk-Ds_twaZn.js → chunk-C4v-HNhS.js} +1 -1
  21. package/dist/{chunk-Bkma5hIU.js → chunk-CE4seJxF.js} +1 -1
  22. package/dist/chunk-CNBMBx-Y.js +2 -0
  23. package/dist/{chunk-ql5N2I_t.js → chunk-CWzHNuVK.js} +1 -1
  24. package/dist/{chunk-DbGLjU2P.js → chunk-CYpt7PsO.js} +1 -1
  25. package/dist/chunk-Cd8mp8Sv.js +1 -0
  26. package/dist/chunk-CsfE8WRG.js +1 -0
  27. package/dist/{chunk-DOZmcve8.js → chunk-Csr59G5V.js} +1 -1
  28. package/dist/{chunk-Co3VVwGe.js → chunk-D2pHRNEq.js} +0 -0
  29. package/dist/chunk-D3kZO8u-.js +1 -0
  30. package/dist/chunk-DAthf5H3.js +1 -0
  31. package/dist/{chunk-CXQJG7pb.js → chunk-DZ_aillY.js} +2 -2
  32. package/dist/{chunk-6C23QQdP.js → chunk-DgJV9zbK.js} +1 -1
  33. package/dist/chunk-Dv6KtIT1.js +1 -0
  34. package/dist/chunk-Dws8IrdR.js +1 -0
  35. package/dist/chunk-ThxxD8oK.js +1 -0
  36. package/dist/{chunk-C-EHtpbB.js → chunk-h5aOI_Pa.js} +1 -1
  37. package/dist/chunk-kizhspe5.js +1 -0
  38. package/dist/client-dispatch.js +1 -1
  39. package/dist/client-history.js +1 -1
  40. package/dist/client-stream.js +1 -1
  41. package/dist/{code-generators-DqauYyfX.d.cts → code-generators-DPAqTcAz.d.cts} +0 -2
  42. package/dist/{code-generators-DqauYyfX.d.ts → code-generators-DPAqTcAz.d.ts} +0 -2
  43. package/dist/codegen.cjs +1 -1
  44. package/dist/codegen.d.cts +1 -1
  45. package/dist/codegen.d.ts +1 -1
  46. package/dist/codegen.js +1 -1
  47. package/dist/composer-actions-runtime.cjs +1 -1
  48. package/dist/composer-actions-runtime.d.cts +4 -20
  49. package/dist/composer-actions-runtime.d.ts +4 -20
  50. package/dist/composer-actions-runtime.js +1 -1
  51. package/dist/{composer-input-DnqGCXZz.d.ts → composer-input-BSDNPOqV.d.cts} +1 -7
  52. package/dist/{composer-input-DnqGCXZz.d.cts → composer-input-BSDNPOqV.d.ts} +1 -7
  53. package/dist/{composer-store-6Iwy6WLI.d.ts → composer-store-DC-gAjtN.d.cts} +1 -3
  54. package/dist/{composer-store-6Iwy6WLI.d.cts → composer-store-DC-gAjtN.d.ts} +1 -3
  55. package/dist/config-diagnostics.d.cts +0 -2
  56. package/dist/config-diagnostics.d.ts +0 -2
  57. package/dist/{context-mention-manager-B-rHF98n.d.ts → context-mention-manager-CCpeoGL2.d.cts} +2 -6
  58. package/dist/{context-mention-manager-B-rHF98n.d.cts → context-mention-manager-CCpeoGL2.d.ts} +2 -6
  59. package/dist/context-mentions-inline.cjs +1 -1
  60. package/dist/context-mentions-inline.d.cts +2 -4
  61. package/dist/context-mentions-inline.d.ts +2 -4
  62. package/dist/context-mentions-inline.js +1 -1
  63. package/dist/context-mentions.cjs +2 -2
  64. package/dist/context-mentions.d.cts +3 -9
  65. package/dist/context-mentions.d.ts +3 -9
  66. package/dist/context-mentions.js +1 -1
  67. package/dist/{defaults-BuOjqUAG.d.ts → defaults-tj5RcN8r.d.cts} +1 -5
  68. package/dist/{defaults-BuOjqUAG.d.cts → defaults-tj5RcN8r.d.ts} +1 -5
  69. package/dist/dictation-runtime.cjs +1 -1
  70. package/dist/dictation-runtime.d.cts +4 -13
  71. package/dist/dictation-runtime.d.ts +4 -13
  72. package/dist/dictation-runtime.js +1 -1
  73. package/dist/{dom-context-DHTabIn7.d.cts → dom-context-Bw-_9Evh.d.cts} +0 -2
  74. package/dist/{dom-context-DHTabIn7.d.ts → dom-context-Bw-_9Evh.d.ts} +0 -2
  75. package/dist/event-stream-view.cjs +1 -1
  76. package/dist/event-stream-view.d.cts +3 -16
  77. package/dist/event-stream-view.d.ts +3 -16
  78. package/dist/{fallback-speech-engine-SqEfkGda.d.ts → fallback-speech-engine-gkhbjLiZ.d.cts} +1 -3
  79. package/dist/{fallback-speech-engine-SqEfkGda.d.cts → fallback-speech-engine-gkhbjLiZ.d.ts} +1 -3
  80. package/dist/{feature-preferences-DqAmje-2.d.ts → feature-preferences-CerDWGZ7.d.cts} +1 -5
  81. package/dist/{feature-preferences-DqAmje-2.d.cts → feature-preferences-CerDWGZ7.d.ts} +1 -5
  82. package/dist/forms-ui.d.cts +2 -4
  83. package/dist/forms-ui.d.ts +2 -4
  84. package/dist/{history-confirm-dialog-B3L7QNcO.d.cts → history-confirm-dialog-CrkSB3Ra.d.cts} +2 -14
  85. package/dist/{history-confirm-dialog-B3L7QNcO.d.ts → history-confirm-dialog-CrkSB3Ra.d.ts} +2 -14
  86. package/dist/history-shell.cjs +1 -1
  87. package/dist/history-shell.d.cts +8 -27
  88. package/dist/history-shell.d.ts +8 -27
  89. package/dist/history-shell.js +1 -1
  90. package/dist/history-view.d.cts +1 -1
  91. package/dist/history-view.d.ts +1 -1
  92. package/dist/icons-extra.cjs +1 -1
  93. package/dist/icons-extra.d.cts +0 -2
  94. package/dist/icons-extra.d.ts +0 -2
  95. package/dist/index.cjs +44 -44
  96. package/dist/index.cjs.map +1 -1
  97. package/dist/index.d.cts +10 -10
  98. package/dist/index.d.ts +10 -10
  99. package/dist/index.global.js +1 -1
  100. package/dist/index.global.js.map +1 -1
  101. package/dist/index.js +50 -50
  102. package/dist/index.js.map +1 -1
  103. package/dist/install.global.js +1 -1
  104. package/dist/install.global.js.map +1 -1
  105. package/dist/launcher.global.js +2 -2
  106. package/dist/launcher.global.js.map +1 -1
  107. package/dist/{loading-label-runtime-loader-JecjOfJS.d.ts → loading-label-runtime-loader-fWIRrhUF.d.cts} +1 -3
  108. package/dist/{loading-label-runtime-loader-JecjOfJS.d.cts → loading-label-runtime-loader-fWIRrhUF.d.ts} +1 -3
  109. package/dist/loading-label-runtime.cjs +1 -1
  110. package/dist/loading-label-runtime.d.cts +1 -1
  111. package/dist/loading-label-runtime.d.ts +1 -1
  112. package/dist/loading-label-runtime.js +1 -1
  113. package/dist/markdown-parsers.js +1 -1
  114. package/dist/{message-bubble-CBYtpDsT.d.ts → message-bubble-Czc0lOHr.d.cts} +20 -3
  115. package/dist/{message-bubble-CBYtpDsT.d.cts → message-bubble-Czc0lOHr.d.ts} +20 -3
  116. package/dist/{plugin-kit-C1mRjLgW.d.cts → plugin-kit-C30jE-cw.d.cts} +3 -3
  117. package/dist/{plugin-kit-C1mRjLgW.d.ts → plugin-kit-C30jE-cw.d.ts} +3 -3
  118. package/dist/plugin-kit.cjs +1 -1
  119. package/dist/plugin-kit.d.cts +1 -1
  120. package/dist/plugin-kit.d.ts +1 -1
  121. package/dist/plugin-kit.js +1 -1
  122. package/dist/preferences-runtime.cjs +1 -1
  123. package/dist/preferences-runtime.d.cts +1 -1
  124. package/dist/preferences-runtime.d.ts +1 -1
  125. package/dist/preferences-runtime.js +1 -1
  126. package/dist/{preview-_UJwwt2L.d.cts → preview-D-osV3FU.d.cts} +2 -6
  127. package/dist/{preview-_UJwwt2L.d.ts → preview-D-osV3FU.d.ts} +2 -6
  128. package/dist/{registry-B5ZV3K-h.d.ts → registry-B6w91tx0.d.cts} +1 -8
  129. package/dist/{registry-B5ZV3K-h.d.cts → registry-B6w91tx0.d.ts} +1 -8
  130. package/dist/{session-Bys8_XuB.d.cts → session-BUQrs3Of.d.cts} +10 -93
  131. package/dist/{session-Bys8_XuB.d.ts → session-BUQrs3Of.d.ts} +10 -93
  132. package/dist/session-reconnect.d.cts +2 -4
  133. package/dist/session-reconnect.d.ts +2 -4
  134. package/dist/smart-dom-reader.d.cts +2 -8
  135. package/dist/smart-dom-reader.d.ts +2 -8
  136. package/dist/testing.d.cts +0 -2
  137. package/dist/testing.d.ts +0 -2
  138. package/dist/theme-editor-preview.cjs +50 -50
  139. package/dist/theme-editor-preview.d.cts +1 -1
  140. package/dist/theme-editor-preview.d.ts +1 -1
  141. package/dist/theme-editor-preview.js +25 -25
  142. package/dist/theme-editor.cjs +25 -5
  143. package/dist/theme-editor.d.cts +2 -22
  144. package/dist/theme-editor.d.ts +2 -22
  145. package/dist/theme-editor.js +1 -1
  146. package/dist/theme-reference.cjs +1 -1
  147. package/dist/theme-reference.d.cts +2 -4
  148. package/dist/theme-reference.d.ts +2 -4
  149. package/dist/theme-reference.js +1 -1
  150. package/dist/{tool-loading-animation-d5qAQcwv.d.ts → tool-loading-animation-B7XUf8TN.d.cts} +1 -7
  151. package/dist/{tool-loading-animation-d5qAQcwv.d.cts → tool-loading-animation-B7XUf8TN.d.ts} +1 -7
  152. package/dist/{tooltip-BkW-8MnD.d.cts → tooltip-DbXkAuJL.d.cts} +0 -2
  153. package/dist/{tooltip-BkW-8MnD.d.ts → tooltip-DbXkAuJL.d.ts} +0 -2
  154. package/dist/{turn-activity-ui-q6fHVnx-.d.cts → turn-activity-ui-CohuNMHT.d.cts} +2 -12
  155. package/dist/{turn-activity-ui-q6fHVnx-.d.ts → turn-activity-ui-CohuNMHT.d.ts} +2 -12
  156. package/dist/turn-activity-ui.d.cts +1 -1
  157. package/dist/turn-activity-ui.d.ts +1 -1
  158. package/dist/{types-B_mj21jg.d.cts → types-9i0xcAMT.d.cts} +128 -29
  159. package/dist/{types-B_mj21jg.d.ts → types-9i0xcAMT.d.ts} +128 -29
  160. package/dist/ui-extras.js +3 -3
  161. package/dist/voice-client-tools.d.cts +1 -3
  162. package/dist/voice-client-tools.d.ts +1 -3
  163. package/dist/voice-runtime.cjs +1 -1
  164. package/dist/voice-runtime.d.cts +2 -12
  165. package/dist/voice-runtime.d.ts +2 -12
  166. package/dist/voice-runtime.js +2 -2
  167. package/dist/voice-worklet-player.d.cts +2 -6
  168. package/dist/voice-worklet-player.d.ts +2 -6
  169. package/dist/{webmcp-runtime-entry-ClEO3qdI.d.ts → webmcp-runtime-entry-XNaXa-9h.d.cts} +9 -5
  170. package/dist/{webmcp-runtime-entry-ClEO3qdI.d.cts → webmcp-runtime-entry-XNaXa-9h.d.ts} +9 -5
  171. package/dist/webmcp-runtime.cjs +1 -1
  172. package/dist/webmcp-runtime.d.cts +1 -1
  173. package/dist/webmcp-runtime.d.ts +1 -1
  174. package/dist/webmcp-runtime.js +1 -1
  175. package/dist/widget.css +1 -1
  176. package/package.json +5 -5
  177. package/src/client-dispatch.ts +13 -0
  178. package/src/client.ts +37 -2
  179. package/src/client.webmcp-require-confirm.test.ts +212 -0
  180. package/src/components/composer-model-picker.ts +0 -14
  181. package/src/components/composer-overflow-menu.ts +0 -15
  182. package/src/components/composer-tokens.ts +0 -20
  183. package/src/components/event-stream-view.test.ts +5 -0
  184. package/src/components/message-bubble.ts +26 -6
  185. package/src/components/scroll-to-bottom.test.ts +135 -0
  186. package/src/components/scroll-to-bottom.ts +50 -13
  187. package/src/components/user-message-collapse.ts +117 -0
  188. package/src/defaults.ts +6 -0
  189. package/src/generated/runtype-openapi-contract.ts +13 -0
  190. package/src/plugin-kit.test.ts +101 -0
  191. package/src/plugin-kit.ts +29 -1
  192. package/src/session.ts +6 -2
  193. package/src/session.webmcp.test.ts +32 -0
  194. package/src/styles/widget.css +56 -0
  195. package/src/theme-reference.ts +2 -2
  196. package/src/types/theme.ts +29 -0
  197. package/src/types.ts +84 -5
  198. package/src/ui.composer-models.test.ts +5 -7
  199. package/src/ui.composer-overflow.test.ts +14 -21
  200. package/src/ui.scroll-additive.test.ts +219 -117
  201. package/src/ui.scroll.test.ts +176 -13
  202. package/src/ui.ts +230 -107
  203. package/src/ui.turn-activity.test.ts +7 -7
  204. package/src/ui.user-message-collapse.test.ts +540 -0
  205. package/src/utils/auto-follow.test.ts +0 -38
  206. package/src/utils/auto-follow.ts +0 -35
  207. package/src/utils/context-mention-controller.test.ts +56 -0
  208. package/src/utils/theme.test.ts +23 -0
  209. package/src/utils/tokens.ts +16 -0
  210. package/src/webmcp-bridge.test.ts +102 -0
  211. package/src/webmcp-runtime-entry.ts +29 -1
  212. package/dist/chunk-BAkaTZpd.js +0 -1
  213. package/dist/chunk-BEDTp-dm.js +0 -1
  214. package/dist/chunk-C7-m0EmI.js +0 -1
  215. package/dist/chunk-CNhThrLX.js +0 -1
  216. package/dist/chunk-CVMIncEY.js +0 -1
  217. package/dist/chunk-CzLEhoB_.js +0 -2
  218. package/dist/chunk-DVgzoVCB.js +0 -1
  219. package/dist/chunk-d54BJdKd.js +0 -1
  220. package/dist/chunk-dLJWKfOb.js +0 -1
@@ -1,2 +1,2 @@
1
- const e=(e,t)=>({ok:!1,reason:e,message:t}),t=e=>e.startsWith(`webmcp:`)?e.slice(7):e,n={warn(e,...t){typeof console<`u`&&typeof console.warn==`function`&&console.warn(`[Persona/WebMCP] ${e}`,...t)}};var r=class{#e;#t;#n=!1;#r=null;#i=!1;#a;constructor(e,t){this.config=e,this.#a=t,this.#e=e.onConfirm??null,this.#t=3e4}setConfirmHandler(e){this.#e=e}isOperational(){return this.config.enabled!==!0||!this.#n?!1:this.#l()!==null}async snapshotForDispatch(){if(await this.#s(),this.config.enabled!==!0)return[];let e=this.#l();if(!e)return[];let t;try{t=(await e.getTools()).filter(h)}catch(e){return n.warn(`getTools() threw: shipping an empty WebMCP snapshot.`,e),[]}this.#a.recordToolDisplayTitles(t);let r=typeof location<`u`?location.origin:``;return t.filter(e=>this.#d(e.name)).map(e=>{let t=o(e.annotations);return{name:e.name,description:e.description,parametersSchema:s(e.inputSchema),origin:`webmcp`,...r?{pageOrigin:r}:{},...t?{annotations:t}:{},...t?.untrustedContentHint===!0?{untrustedContentHint:!0}:{}}})}async executeToolCall(e,t,n,r){let i=await this.#o(e,t,n,!1,r);return i.ok?c(i.raw):u(i.message)}async executeVoiceToolCall(e,t,n={}){let r=await this.#o(e,t,n.signal,n.autoConfirmReadOnly===!0);return r.ok?f(r.raw):{status:r.reason===`aborted`?`failed`:r.reason,error:{message:r.message}}}watchToolChanges(e){let t=!1,n=null;return this.#s().then(()=>{if(t||this.config.enabled!==!0)return;let r=this.#l();r&&typeof r.addEventListener==`function`&&(n=r,n.addEventListener(`toolchange`,e))}),()=>{t=!0,n?.removeEventListener(`toolchange`,e),n=null}}async#o(n,r,i,a,s){if(await this.#s(),this.config.enabled!==!0)return e(`failed`,`WebMCP is not enabled on this widget.`);let c=this.#l();if(!c){let t=typeof document<`u`&&!!document.modelContext;return e(`failed`,t?`WebMCP is not operational: document.modelContext is present but does not expose the strict getTools()/executeTool() surface (likely a different or older WebMCP polyfill).`:`WebMCP bridge is not operational on this page (document.modelContext not available).`)}let u=t(n),d;try{d=(await c.getTools()).filter(h)}catch(t){let n=t instanceof Error?t.message:String(t);return e(`failed`,`Failed to read WebMCP registry: ${n}`)}this.#a.recordToolDisplayTitles(d);let f=d.find(e=>e.name===u);if(!f)return e(`stale`,`WebMCP tool not registered on this page: ${u}`);if(!this.#d(u))return e(`stale`,`WebMCP tool not allowed by client allowlist: ${u}`);if(i?.aborted)return e(`aborted`,`Aborted by cancel()`);let p=this.#a.getToolDisplayTitle(u),m=o(f.annotations),g={toolName:u,...s?{toolCallId:s}:{},args:r,description:f.description,...m?{annotations:m}:{},...p?{title:p}:{},...i?{signal:i}:{},reason:`gate`};if(!(a&&m?.readOnlyHint===!0)&&!await this.#u(g))return e(`denied`,`User declined the tool call.`);if(i?.aborted)return e(`aborted`,`Aborted by cancel()`);let _=new AbortController,v=!1,y=setTimeout(()=>{v=!0,_.abort()},this.#t),b=()=>_.abort();i&&(i.aborted?_.abort():i.addEventListener(`abort`,b,{once:!0}));try{return{ok:!0,raw:await c.executeTool(f,l(r),{signal:_.signal})}}catch(t){if(v)return e(`timeout`,`WebMCP tool '${u}' timed out after ${this.#t}ms`);if(i?.aborted)return e(`aborted`,`Aborted by cancel()`);let n=t instanceof Error?t.message:String(t);return e(`failed`,n)}finally{clearTimeout(y),i&&i.removeEventListener(`abort`,b)}}#s(){return this.config.enabled===!0?(this.#r||=this.#c(),this.#r):Promise.resolve()}async#c(){try{if(this.#l()){this.#n=!0;return}let e=await this.#a.loadPolyfill();`initializeWebMCPPolyfill`in e?e.initializeWebMCPPolyfill():e.installWebMCP(),this.#n=!0}catch(e){n.warn(`Failed to load @mcp-b/webmcp-polyfill: WebMCP consumption disabled.`,e),this.#n=!1}}#l(){if(typeof document>`u`)return null;let e=document.modelContext;if(!e||typeof e!=`object`)return null;let t=e;return typeof t.getTools!=`function`||typeof t.executeTool!=`function`?(this.#i||(this.#i=!0,n.warn(`document.modelContext is present but does not expose getTools()/executeTool(): WebMCP consumption is disabled. Another (incompatible or older) WebMCP polyfill likely installed document.modelContext before Persona. Remove it, or use a polyfill implementing the strict standard surface (e.g. @mcp-b/webmcp-polyfill).`)),null):e}async#u(e){let t=this.#e??p;try{return await t(e)}catch(t){return n.warn(`Confirm handler threw for WebMCP tool '${e.toolName}'; declining.`,t),!1}}#d(e){let t=this.config.allowlist;return!t||t.length===0||t.some(t=>i(e,t))}};const i=(e,t)=>{if(t===`*`)return!0;let n=t.replace(/[.+?^${}()|[\]\\]/g,`\\$&`);return RegExp(`^`+n.replace(/\*/g,`.*`)+`$`).test(e)},a={type:`object`,properties:{}},o=e=>{if(!e||typeof e!=`object`)return;let t={};for(let n of[`readOnlyHint`,`destructiveHint`,`consequentialHint`,`untrustedContentHint`,`idempotentHint`,`openWorldHint`,`debugging`]){let r=e[n];typeof r==`boolean`&&(t[n]=r)}return Object.keys(t).length>0?t:void 0},s=e=>{if(e===void 0||e===``)return{...a};if(typeof e==`object`)return e!==null&&!Array.isArray(e)?e:{...a};try{let t=JSON.parse(e);return typeof t==`object`&&t&&!Array.isArray(t)?t:{...a}}catch{return{...a}}},c=e=>{if(e==null)return{content:[{type:`text`,text:``}]};let t;try{t=JSON.parse(e)}catch{return{content:[{type:`text`,text:e}]}}return typeof t==`object`&&t&&Array.isArray(t.content)?t:{content:[{type:`text`,text:typeof t==`string`?t:g(t)}]}},l=e=>{let t;try{t=JSON.stringify(e)??`{}`}catch{t=`{}`}let n=JSON.parse(t);if(typeof n!=`object`||!n)throw TypeError(`WebMCP tool arguments must be an object or array.`);return Object.defineProperty(n,Symbol.toPrimitive,{value:()=>t}),n},u=e=>({isError:!0,content:[{type:`text`,text:e}]}),d=e=>Array.isArray(e)?e.map(e=>e&&typeof e==`object`&&typeof e.text==`string`?e.text:``).filter(Boolean).join(`
1
+ const e=(e,t)=>({ok:!1,reason:e,message:t}),t=e=>e.startsWith(`webmcp:`)?e.slice(7):e,n={warn(e,...t){typeof console<`u`&&typeof console.warn==`function`&&console.warn(`[Persona/WebMCP] ${e}`,...t)}};var r=class{#e;#t;#n=!1;#r=null;#i=!1;#a;constructor(e,t){this.config=e,this.#a=t,this.#e=e.onConfirm??null,this.#t=3e4}setConfirmHandler(e){this.#e=e}isOperational(){return this.config.enabled!==!0||!this.#n?!1:this.#l()!==null}async snapshotForDispatch(){if(await this.#s(),this.config.enabled!==!0)return[];let e=this.#l();if(!e)return[];let t;try{t=(await e.getTools()).filter(h)}catch(e){return n.warn(`getTools() threw: shipping an empty WebMCP snapshot.`,e),[]}this.#a.recordToolDisplayTitles(t);let r=typeof location<`u`?location.origin:``;return t.filter(e=>this.#f(e.name)).map(e=>{let t=o(e.annotations);return{name:e.name,description:e.description,parametersSchema:s(e.inputSchema),origin:`webmcp`,...r?{pageOrigin:r}:{},...t?{annotations:t}:{},...t?.untrustedContentHint===!0?{untrustedContentHint:!0}:{}}})}async executeToolCall(e,t,n,r){let i=await this.#o(e,t,n,!1,r);return i.ok?c(i.raw):u(i.message)}async executeVoiceToolCall(e,t,n={}){let r=await this.#o(e,t,n.signal,n.autoConfirmReadOnly===!0);return r.ok?f(r.raw):{status:r.reason===`aborted`?`failed`:r.reason,error:{message:r.message}}}watchToolChanges(e){let t=!1,n=null;return this.#s().then(()=>{if(t||this.config.enabled!==!0)return;let r=this.#l();r&&typeof r.addEventListener==`function`&&(n=r,n.addEventListener(`toolchange`,e))}),()=>{t=!0,n?.removeEventListener(`toolchange`,e),n=null}}async#o(n,r,i,a,s){if(await this.#s(),this.config.enabled!==!0)return e(`failed`,`WebMCP is not enabled on this widget.`);let c=this.#l();if(!c){let t=typeof document<`u`&&!!document.modelContext;return e(`failed`,t?`WebMCP is not operational: document.modelContext is present but does not expose the strict getTools()/executeTool() surface (likely a different or older WebMCP polyfill).`:`WebMCP bridge is not operational on this page (document.modelContext not available).`)}let u=t(n),d;try{d=(await c.getTools()).filter(h)}catch(t){let n=t instanceof Error?t.message:String(t);return e(`failed`,`Failed to read WebMCP registry: ${n}`)}this.#a.recordToolDisplayTitles(d);let f=d.find(e=>e.name===u);if(!f)return e(`stale`,`WebMCP tool not registered on this page: ${u}`);if(!this.#f(u))return e(`stale`,`WebMCP tool not allowed by client allowlist: ${u}`);if(i?.aborted)return e(`aborted`,`Aborted by cancel()`);let p=this.#a.getToolDisplayTitle(u),m=o(f.annotations),g={toolName:u,...s?{toolCallId:s}:{},args:r,description:f.description,...m?{annotations:m}:{},...p?{title:p}:{},...i?{signal:i}:{},reason:`gate`},_=this.#d(u);if(_&&(g.confirmRequired=!0),(_||!a||m?.readOnlyHint!==!0)&&!await this.#u(g))return e(`denied`,`User declined the tool call.`);if(i?.aborted)return e(`aborted`,`Aborted by cancel()`);let v=new AbortController,y=!1,b=setTimeout(()=>{y=!0,v.abort()},this.#t),x=()=>v.abort();i&&(i.aborted?v.abort():i.addEventListener(`abort`,x,{once:!0}));try{return{ok:!0,raw:await c.executeTool(f,l(r),{signal:v.signal})}}catch(t){if(y)return e(`timeout`,`WebMCP tool '${u}' timed out after ${this.#t}ms`);if(i?.aborted)return e(`aborted`,`Aborted by cancel()`);let n=t instanceof Error?t.message:String(t);return e(`failed`,n)}finally{clearTimeout(b),i&&i.removeEventListener(`abort`,x)}}#s(){return this.config.enabled===!0?(this.#r||=this.#c(),this.#r):Promise.resolve()}async#c(){try{if(this.#l()){this.#n=!0;return}let e=await this.#a.loadPolyfill();`initializeWebMCPPolyfill`in e?e.initializeWebMCPPolyfill():e.installWebMCP(),this.#n=!0}catch(e){n.warn(`Failed to load @mcp-b/webmcp-polyfill: WebMCP consumption disabled.`,e),this.#n=!1}}#l(){if(typeof document>`u`)return null;let e=document.modelContext;if(!e||typeof e!=`object`)return null;let t=e;return typeof t.getTools!=`function`||typeof t.executeTool!=`function`?(this.#i||(this.#i=!0,n.warn(`document.modelContext is present but does not expose getTools()/executeTool(): WebMCP consumption is disabled. Another (incompatible or older) WebMCP polyfill likely installed document.modelContext before Persona. Remove it, or use a polyfill implementing the strict standard surface (e.g. @mcp-b/webmcp-polyfill).`)),null):e}async#u(e){let t=this.#e??p;try{return await t(e)}catch(t){return n.warn(`Confirm handler threw for WebMCP tool '${e.toolName}'; declining.`,t),!1}}#d(e){let t;try{t=this.#a.getRequireConfirmFor?.()}catch{t=null}if(t===null)return!0;let n=t=>Array.isArray(t)&&t.some(t=>typeof t==`string`&&i(e,t));return n(this.config.requireConfirmFor)||n(t)}#f(e){let t=this.config.allowlist;return!t||t.length===0||t.some(t=>i(e,t))}};const i=(e,t)=>{if(t===`*`)return!0;let n=t.replace(/[.+?^${}()|[\]\\]/g,`\\$&`);return RegExp(`^`+n.replace(/\*/g,`.*`)+`$`).test(e)},a={type:`object`,properties:{}},o=e=>{if(!e||typeof e!=`object`)return;let t={};for(let n of[`readOnlyHint`,`destructiveHint`,`consequentialHint`,`untrustedContentHint`,`idempotentHint`,`openWorldHint`,`debugging`]){let r=e[n];typeof r==`boolean`&&(t[n]=r)}return Object.keys(t).length>0?t:void 0},s=e=>{if(e===void 0||e===``)return{...a};if(typeof e==`object`)return e!==null&&!Array.isArray(e)?e:{...a};try{let t=JSON.parse(e);return typeof t==`object`&&t&&!Array.isArray(t)?t:{...a}}catch{return{...a}}},c=e=>{if(e==null)return{content:[{type:`text`,text:``}]};let t;try{t=JSON.parse(e)}catch{return{content:[{type:`text`,text:e}]}}return typeof t==`object`&&t&&Array.isArray(t.content)?t:{content:[{type:`text`,text:typeof t==`string`?t:g(t)}]}},l=e=>{let t;try{t=JSON.stringify(e)??`{}`}catch{t=`{}`}let n=JSON.parse(t);if(typeof n!=`object`||!n)throw TypeError(`WebMCP tool arguments must be an object or array.`);return Object.defineProperty(n,Symbol.toPrimitive,{value:()=>t}),n},u=e=>({isError:!0,content:[{type:`text`,text:e}]}),d=e=>Array.isArray(e)?e.map(e=>e&&typeof e==`object`&&typeof e.text==`string`?e.text:``).filter(Boolean).join(`
2
2
  `):``,f=e=>{if(e==null)return{status:`completed`};let t;try{t=JSON.parse(e)}catch{return{status:`completed`,result:e}}return typeof t==`object`&&t&&t.isError===!0?{status:`failed`,error:{message:d(t.content)||`The page tool reported an error.`}}:t===void 0?{status:`completed`}:{status:`completed`,result:t}},p=async e=>{if(typeof window>`u`||typeof window.confirm!=`function`)return!1;let t=m(e.args),n=`Allow the AI to call ${e.toolName}`+(t?`\n\nArguments:\n${t}`:``)+(e.description?`\n\n${e.description}`:``);return window.confirm(n)},m=e=>{if(e==null)return``;try{let t=JSON.stringify(e,null,2);return t.length>500?t.slice(0,500)+`…`:t}catch{return String(e)}},h=e=>(e.window===void 0||typeof window<`u`&&e.window===window)&&(e.origin===void 0||typeof location<`u`&&e.origin===location.origin),g=e=>{if(e===void 0)return``;try{return JSON.stringify(e)}catch{return String(e)}};export{r as WebMcpBridge};