@runtypelabs/persona 3.12.0 → 3.14.0

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
@@ -311,6 +311,8 @@ window.chatController.submitMessage("Test message")
311
311
  window.chatController.startVoiceRecognition()
312
312
  ```
313
313
 
314
+ When using the automatic installer script (`install.global.js`), see [Programmatic access with the installer](#programmatic-access-with-the-installer) for additional approaches including the `onReady` callback and `persona:ready` event.
315
+
314
316
  #### Message Types
315
317
 
316
318
  The widget uses `AgentWidgetMessage` objects to represent messages in the conversation. You can access these through `postprocessMessage` callbacks or by inspecting the session's message array.
@@ -483,6 +485,20 @@ window.dispatchEvent(new CustomEvent('persona:focusInput', {
483
485
 
484
486
  **Instance scoping:** Same as `persona:showEventStream` — use `detail.instanceId` to target a specific widget. Without `instanceId`, all instances receive the event.
485
487
 
488
+ #### `persona:ready`
489
+
490
+ Dispatched on `window` by the automatic installer script (`install.global.js`) after the widget is initialized. The `event.detail` contains the `AgentWidgetInitHandle` (the same object returned by `initAgentWidget()`).
491
+
492
+ ```ts
493
+ window.addEventListener('persona:ready', (e) => {
494
+ const handle = e.detail;
495
+ handle.on('message:sent', (msg) => console.log(msg));
496
+ handle.open();
497
+ });
498
+ ```
499
+
500
+ > **Note:** This event is only dispatched by the automatic installer script. Direct calls to `initAgentWidget()` return the handle synchronously and do not fire this event.
501
+
486
502
  ### Controller Events
487
503
 
488
504
  The widget controller exposes an event system for reacting to chat events. Use `controller.on(eventName, callback)` to subscribe and `controller.off(eventName, callback)` to unsubscribe.
@@ -1249,6 +1265,13 @@ The easiest way is to use the automatic installer script. It handles loading CSS
1249
1265
  - `target` - CSS selector or element where widget mounts (default: `"body"`)
1250
1266
  - `config` - Widget configuration object (see Configuration reference)
1251
1267
  - `autoInit` - Automatically initialize after loading (default: `true`)
1268
+ - `clientToken` - Client token for authentication (alternative to proxy `apiUrl`)
1269
+ - `flowId` - Flow ID for client token authentication
1270
+ - `apiUrl` - API URL for the chat endpoint (can also be set inside `config`)
1271
+ - `previewQueryParam` - Query parameter key that gates widget loading; widget only loads when the parameter is present and truthy
1272
+ - `useShadowDom` - Use Shadow DOM for style isolation (default: `false`)
1273
+ - `windowKey` - If provided, stores the widget handle on `window[windowKey]` for programmatic access
1274
+ - `onReady` - Callback fired with the widget handle after initialization; signature: `(handle) => void`
1252
1275
 
1253
1276
  **Example with version pinning:**
1254
1277
 
@@ -1265,6 +1288,62 @@ The easiest way is to use the automatic installer script. It handles loading CSS
1265
1288
  <script src="https://cdn.jsdelivr.net/npm/@runtypelabs/persona@0.1.0/dist/install.global.js"></script>
1266
1289
  ```
1267
1290
 
1291
+ #### Programmatic access with the installer
1292
+
1293
+ The installer is fully asynchronous (it waits for framework hydration, then loads CSS and JS). To interact with the widget after it initializes, use one of these approaches:
1294
+
1295
+ **`onReady` callback** — best when config and access logic live in the same script:
1296
+
1297
+ ```html
1298
+ <script>
1299
+ window.siteAgentConfig = {
1300
+ clientToken: 'YOUR_TOKEN',
1301
+ windowKey: 'myChat',
1302
+ onReady(handle) {
1303
+ handle.on('message:sent', (e) => console.log('sent:', e));
1304
+ handle.on('message:received', (e) => console.log('received:', e));
1305
+ }
1306
+ };
1307
+ </script>
1308
+ <script src="https://cdn.jsdelivr.net/npm/@runtypelabs/persona@latest/dist/install.global.js"></script>
1309
+ ```
1310
+
1311
+ **`persona:ready` event** — best for decoupled integration (e.g. tag managers, separate scripts):
1312
+
1313
+ ```html
1314
+ <script>
1315
+ window.addEventListener('persona:ready', (e) => {
1316
+ const handle = e.detail;
1317
+ handle.on('message:sent', (e) => console.log('sent:', e));
1318
+ });
1319
+ </script>
1320
+
1321
+ <!-- Can be in a different script, tag manager snippet, etc. -->
1322
+ <script>
1323
+ window.siteAgentConfig = { clientToken: 'YOUR_TOKEN' };
1324
+ </script>
1325
+ <script src="https://cdn.jsdelivr.net/npm/@runtypelabs/persona@latest/dist/install.global.js"></script>
1326
+ ```
1327
+
1328
+ **`windowKey`** — stores the handle on `window[windowKey]` for persistent global access. Combine with `onReady` or `persona:ready` to know when it's available:
1329
+
1330
+ ```html
1331
+ <script>
1332
+ window.siteAgentConfig = {
1333
+ clientToken: 'YOUR_TOKEN',
1334
+ windowKey: 'myChat'
1335
+ };
1336
+ </script>
1337
+ <script src="https://cdn.jsdelivr.net/npm/@runtypelabs/persona@latest/dist/install.global.js"></script>
1338
+
1339
+ <script>
1340
+ window.addEventListener('persona:ready', () => {
1341
+ // window.myChat is now available and persists until destroy()
1342
+ window.myChat.open();
1343
+ });
1344
+ </script>
1345
+ ```
1346
+
1268
1347
  #### Method 2: Manual installation
1269
1348
 
1270
1349
  For more control, manually load CSS and JavaScript: