@trillet-ai/web-sdk 1.14.1 → 1.16.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.
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  TrilletAgent
3
- } from "../chunk-GUAIVASQ.js";
3
+ } from "../chunk-WSOSR3UF.js";
4
4
  import {
5
5
  apiUrl
6
6
  } from "../chunk-CVTVHUNZ.js";
@@ -1209,13 +1209,101 @@ function renderMarkdown(source) {
1209
1209
  return fragment;
1210
1210
  }
1211
1211
 
1212
+ // src/widget/heldMessages.ts
1213
+ var HOLD_FALLBACK_MS = 25e3;
1214
+ var HeldMessages = class {
1215
+ constructor(send, fallbackMs = HOLD_FALLBACK_MS) {
1216
+ this.send = send;
1217
+ this.fallbackMs = fallbackMs;
1218
+ this.queue = [];
1219
+ this.sending = false;
1220
+ // Sends only work once the session is live. A greeting that lands earlier (audio
1221
+ // set-up can still be running after the room connects) is remembered until then.
1222
+ this.live = false;
1223
+ this.cued = false;
1224
+ this.timer = null;
1225
+ // Bumped by clear() so a send still in flight from a reset conversation stops there.
1226
+ this.generation = 0;
1227
+ }
1228
+ /** True while a message waits or is still going out. New text must queue behind it. */
1229
+ get holding() {
1230
+ return this.queue.length > 0 || this.sending;
1231
+ }
1232
+ hold(text) {
1233
+ this.queue.push(text);
1234
+ }
1235
+ /** The session is live: send if the agent already cued it, else start the fallback. */
1236
+ connected() {
1237
+ this.live = true;
1238
+ if (!this.queue.length) return;
1239
+ if (this.cued) {
1240
+ void this.release();
1241
+ return;
1242
+ }
1243
+ this.clearTimer();
1244
+ this.timer = setTimeout(() => void this.release(), this.fallbackMs);
1245
+ }
1246
+ /** The agent's greeting releases the queue. A tool call is not a greeting. */
1247
+ transcripts(list) {
1248
+ if (!this.queue.length) return;
1249
+ if (list.some((t) => t.isFinal && t.role === "assistant" && !t.toolUsed)) this.cue();
1250
+ }
1251
+ /** The agent said it will not greet. */
1252
+ greetingSkipped() {
1253
+ if (this.queue.length) this.cue();
1254
+ }
1255
+ /** Send what is held, oldest first, one at a time; text held meanwhile follows. */
1256
+ async release() {
1257
+ this.clearTimer();
1258
+ if (this.sending) return;
1259
+ const generation = this.generation;
1260
+ this.sending = true;
1261
+ try {
1262
+ while (this.queue.length && generation === this.generation) {
1263
+ const next = this.queue.shift();
1264
+ try {
1265
+ await this.send(next);
1266
+ } catch (e) {
1267
+ }
1268
+ }
1269
+ } finally {
1270
+ if (generation === this.generation) this.sending = false;
1271
+ }
1272
+ }
1273
+ /** Forget everything held: the conversation ended or was reset. */
1274
+ clear() {
1275
+ this.generation += 1;
1276
+ this.clearTimer();
1277
+ this.queue = [];
1278
+ this.sending = false;
1279
+ this.live = false;
1280
+ this.cued = false;
1281
+ }
1282
+ cue() {
1283
+ if (this.live) void this.release();
1284
+ else this.cued = true;
1285
+ }
1286
+ clearTimer() {
1287
+ if (this.timer) clearTimeout(this.timer);
1288
+ this.timer = null;
1289
+ }
1290
+ };
1291
+
1292
+ // src/widget/panelOpen.ts
1293
+ function panelOpenAction(ctx) {
1294
+ if (ctx.mode === "voice" || ctx.open) return "none";
1295
+ const fresh = ctx.state === "idle" && !ctx.hasTranscript;
1296
+ if (ctx.mode === "text" && fresh && !ctx.needsRecaptcha) return "begin";
1297
+ return "expand";
1298
+ }
1299
+
1212
1300
  // src/widget/TrilletWidgetElement.ts
1213
1301
  var TrilletWidgetElement = class extends HTMLElement {
1214
1302
  constructor() {
1215
1303
  super();
1216
1304
  this.agent = null;
1217
1305
  this.state = "idle";
1218
- this.open = false;
1306
+ this.isOpen = false;
1219
1307
  // panel open vs. launcher only
1220
1308
  this.micEnabled = true;
1221
1309
  this.speaking = false;
@@ -1234,8 +1322,12 @@ var TrilletWidgetElement = class extends HTMLElement {
1234
1322
  this.remote = null;
1235
1323
  this.remoteDisplay = null;
1236
1324
  this.remoteLoadedFor = null;
1237
- // A suggested prompt clicked before the chat finished connecting; sent on connect.
1238
- this.pendingMessage = null;
1325
+ // Text typed before the agent was ready for it: held for its greeting, then sent in
1326
+ // the order typed (TRI-2230). Sent only on the live agent of this conversation.
1327
+ this.held = new HeldMessages(async (text) => {
1328
+ const agent = this.agent;
1329
+ if (agent && this.state === "live") await agent.sendTextMessage(text);
1330
+ });
1239
1331
  // maxCallDuration countdown (UX only — see startConversation()).
1240
1332
  this.durationTimer = null;
1241
1333
  this.remainingSeconds = null;
@@ -1254,7 +1346,6 @@ var TrilletWidgetElement = class extends HTMLElement {
1254
1346
  this.recaptchaSolved = false;
1255
1347
  this.recaptchaSlot = null;
1256
1348
  this.recaptchaWidgetId = null;
1257
- this.pendingTimer = null;
1258
1349
  // Which top-level view is on screen; the entrance animation only plays on a change.
1259
1350
  this.lastView = null;
1260
1351
  // Inline notice in the chat (e.g. microphone refused). Cleared on the next switch.
@@ -1341,17 +1432,51 @@ var TrilletWidgetElement = class extends HTMLElement {
1341
1432
  * first when needed; the message is delivered as soon as the session connects.
1342
1433
  */
1343
1434
  async sendMessage(text) {
1344
- var _a;
1345
1435
  const message = text.trim();
1346
1436
  if (!message) return;
1347
1437
  if (this.state === "live" && (this.resolvedMode() === "text" || this.config.mode === "both")) {
1348
- await ((_a = this.agent) == null ? void 0 : _a.sendTextMessage(message));
1438
+ this.open();
1439
+ await this.deliver(message);
1349
1440
  return;
1350
1441
  }
1351
1442
  if (this.config.mode === "voice") return;
1352
- if (this.state === "idle" || this.state === "ended") this.transcripts = [];
1353
- this.pendingMessage = message;
1443
+ if (this.state === "idle" || this.state === "ended") {
1444
+ this.transcripts = [];
1445
+ this.held.clear();
1446
+ }
1447
+ this.held.hold(message);
1354
1448
  if (this.state !== "connecting") await this.startConversation("text");
1449
+ else this.open();
1450
+ }
1451
+ /**
1452
+ * Expand the widget if it is minimised, without touching the conversation, its
1453
+ * transcript or the session. Safe to call when already open, when no conversation is
1454
+ * live, and in voice mode (which has no panel, so it does nothing there).
1455
+ */
1456
+ open() {
1457
+ const action = panelOpenAction({
1458
+ mode: this.config.mode,
1459
+ open: this.isOpen,
1460
+ state: this.state,
1461
+ hasTranscript: this.transcripts.length > 0,
1462
+ needsRecaptcha: this.needsRecaptcha()
1463
+ });
1464
+ if (action === "none") return;
1465
+ this.isOpen = true;
1466
+ if (action === "begin") {
1467
+ void this.beginCall("text");
1468
+ return;
1469
+ }
1470
+ this.render();
1471
+ }
1472
+ /** Send now, or behind a message still held for the agent's greeting (TRI-2230). */
1473
+ async deliver(text) {
1474
+ var _a;
1475
+ if (this.held.holding) {
1476
+ this.held.hold(text);
1477
+ return;
1478
+ }
1479
+ await ((_a = this.agent) == null ? void 0 : _a.sendTextMessage(text));
1355
1480
  }
1356
1481
  // ---- Public element API ----------------------------------------------------
1357
1482
  /**
@@ -1359,10 +1484,13 @@ var TrilletWidgetElement = class extends HTMLElement {
1359
1484
  * @param modality - optional; only used in `mode="both"` to pick voice vs text.
1360
1485
  */
1361
1486
  async startConversation(modality) {
1362
- if (this.state === "connecting" || this.state === "live") return;
1487
+ if (this.state === "connecting" || this.state === "live") {
1488
+ if (this.config.mode === "text" || this.config.mode === "both" && modality !== "voice") this.open();
1489
+ return;
1490
+ }
1363
1491
  const chat = this.config.mode === "text" || this.config.mode === "both" && modality !== "voice";
1364
1492
  if (chat) {
1365
- this.open = true;
1493
+ this.isOpen = true;
1366
1494
  }
1367
1495
  if (this.needsRecaptcha()) {
1368
1496
  this.render();
@@ -1387,9 +1515,7 @@ var TrilletWidgetElement = class extends HTMLElement {
1387
1515
  this.callGeneration += 1;
1388
1516
  this.clearDurationTimer();
1389
1517
  this.teardownAgent();
1390
- if (this.pendingTimer) clearTimeout(this.pendingTimer);
1391
- this.pendingTimer = null;
1392
- this.pendingMessage = null;
1518
+ this.held.clear();
1393
1519
  this.transcripts = [];
1394
1520
  this.notice = null;
1395
1521
  this.errorText = null;
@@ -1479,14 +1605,6 @@ var TrilletWidgetElement = class extends HTMLElement {
1479
1605
  const status = this.root.querySelector(".tw-header .tw-status");
1480
1606
  if (status) status.textContent = this.statusText();
1481
1607
  }
1482
- /** Send the message typed before the chat started (once). */
1483
- flushPending() {
1484
- if (this.pendingTimer) clearTimeout(this.pendingTimer);
1485
- this.pendingTimer = null;
1486
- const queued = this.pendingMessage;
1487
- this.pendingMessage = null;
1488
- if (queued && this.agent && this.state === "live") void this.agent.sendTextMessage(queued);
1489
- }
1490
1608
  // ---- Credential resolution + call lifecycle --------------------------------
1491
1609
  async beginCall(modality) {
1492
1610
  if (this.config.termsText && !this.termsAccepted()) {
@@ -1563,10 +1681,9 @@ var TrilletWidgetElement = class extends HTMLElement {
1563
1681
  this.setState("live");
1564
1682
  this.startDurationTimer(!wasSwitching);
1565
1683
  this.switching = false;
1566
- if (this.pendingMessage) {
1567
- this.pendingTimer = setTimeout(() => this.flushPending(), 25e3);
1568
- }
1684
+ this.held.connected();
1569
1685
  });
1686
+ agent.on("greetingSkipped", () => this.held.greetingSkipped());
1570
1687
  agent.on("disconnected", () => {
1571
1688
  this.endLocalCall();
1572
1689
  });
@@ -1584,9 +1701,7 @@ var TrilletWidgetElement = class extends HTMLElement {
1584
1701
  agent.on("assistantStoppedSpeaking", () => this.setSpeaking(false));
1585
1702
  agent.on("transcriptUpdate", (transcripts) => {
1586
1703
  this.transcripts = [...transcripts];
1587
- if (this.pendingMessage && transcripts.some((t) => t.isFinal && t.role === "assistant" && !t.toolUsed)) {
1588
- this.flushPending();
1589
- }
1704
+ this.held.transcripts(transcripts);
1590
1705
  this.dispatch(
1591
1706
  "transcript",
1592
1707
  this.transcripts.filter((t) => t.isFinal)
@@ -1605,9 +1720,7 @@ var TrilletWidgetElement = class extends HTMLElement {
1605
1720
  this.dispatch("conversationEnded", this.elapsedSeconds);
1606
1721
  this.setState("ended");
1607
1722
  this.agent = null;
1608
- this.pendingMessage = null;
1609
- if (this.pendingTimer) clearTimeout(this.pendingTimer);
1610
- this.pendingTimer = null;
1723
+ this.held.clear();
1611
1724
  if (this.config.mode === "both") this.activeMode = null;
1612
1725
  }
1613
1726
  handleError(error) {
@@ -1618,6 +1731,7 @@ var TrilletWidgetElement = class extends HTMLElement {
1618
1731
  this.switching = false;
1619
1732
  this.dispatch("error", err);
1620
1733
  this.agent = null;
1734
+ this.held.clear();
1621
1735
  if (this.config.mode === "both") this.activeMode = null;
1622
1736
  this.setState("idle");
1623
1737
  }
@@ -1734,7 +1848,7 @@ var TrilletWidgetElement = class extends HTMLElement {
1734
1848
  } else if (this.showVariablesForm) {
1735
1849
  view = "variables";
1736
1850
  el = this.renderVariablesForm();
1737
- } else if (this.open) {
1851
+ } else if (this.isOpen) {
1738
1852
  view = "panel";
1739
1853
  el = this.renderPanel();
1740
1854
  } else {
@@ -1832,7 +1946,7 @@ var TrilletWidgetElement = class extends HTMLElement {
1832
1946
  this.showTerms = false;
1833
1947
  const start = this.pendingStart;
1834
1948
  this.pendingStart = null;
1835
- if (start === "text") this.open = true;
1949
+ if (start === "text") this.isOpen = true;
1836
1950
  void this.beginCall(start != null ? start : void 0);
1837
1951
  });
1838
1952
  const decline = document.createElement("button");
@@ -1842,7 +1956,7 @@ var TrilletWidgetElement = class extends HTMLElement {
1842
1956
  decline.addEventListener("click", () => {
1843
1957
  this.showTerms = false;
1844
1958
  this.pendingStart = null;
1845
- this.open = false;
1959
+ this.isOpen = false;
1846
1960
  this.render();
1847
1961
  });
1848
1962
  row.appendChild(accept);
@@ -1887,7 +2001,7 @@ var TrilletWidgetElement = class extends HTMLElement {
1887
2001
  this.showVariablesForm = false;
1888
2002
  const start = this.pendingStart;
1889
2003
  this.pendingStart = null;
1890
- if (start === "text") this.open = true;
2004
+ if (start === "text") this.isOpen = true;
1891
2005
  void this.beginCall(start != null ? start : void 0);
1892
2006
  });
1893
2007
  this.config.dynamicVariablePrompts.forEach((prompt, index) => {
@@ -1941,7 +2055,7 @@ var TrilletWidgetElement = class extends HTMLElement {
1941
2055
  decline.addEventListener("click", () => {
1942
2056
  this.showVariablesForm = false;
1943
2057
  this.pendingStart = null;
1944
- this.open = false;
2058
+ this.isOpen = false;
1945
2059
  this.render();
1946
2060
  });
1947
2061
  row.appendChild(decline);
@@ -2124,7 +2238,7 @@ var TrilletWidgetElement = class extends HTMLElement {
2124
2238
  * until the visitor explicitly starts over (see {@link startOver}).
2125
2239
  */
2126
2240
  openPanel() {
2127
- this.open = true;
2241
+ this.isOpen = true;
2128
2242
  const fresh = this.state === "idle" && this.transcripts.length === 0;
2129
2243
  if (this.config.mode === "text" && fresh && !this.needsRecaptcha()) {
2130
2244
  void this.beginCall("text");
@@ -2185,7 +2299,7 @@ var TrilletWidgetElement = class extends HTMLElement {
2185
2299
  actions.className = "tw-header-actions";
2186
2300
  actions.appendChild(
2187
2301
  this.iconButton(ICONS.minimize, this.config.copy.minimize, () => {
2188
- this.open = false;
2302
+ this.isOpen = false;
2189
2303
  this.render();
2190
2304
  })
2191
2305
  );
@@ -2193,7 +2307,7 @@ var TrilletWidgetElement = class extends HTMLElement {
2193
2307
  actions.appendChild(
2194
2308
  this.iconButton(ICONS.close, this.config.copy.close, () => {
2195
2309
  if (this.state === "live" || this.state === "connecting") this.endConversation();
2196
- this.open = false;
2310
+ this.isOpen = false;
2197
2311
  this.render();
2198
2312
  })
2199
2313
  );
@@ -2519,7 +2633,7 @@ var TrilletWidgetElement = class extends HTMLElement {
2519
2633
  } else if (this.config.mode === "both") {
2520
2634
  bar.appendChild(
2521
2635
  this.iconButton(ICONS.chat, this.config.copy.showConversation, () => {
2522
- this.open = true;
2636
+ this.isOpen = true;
2523
2637
  this.render();
2524
2638
  }, "tw-sq tw-sq-quiet")
2525
2639
  );
@@ -2548,13 +2662,12 @@ var TrilletWidgetElement = class extends HTMLElement {
2548
2662
  input.style.height = `${Math.min(input.scrollHeight, 112)}px`;
2549
2663
  };
2550
2664
  const send = () => {
2551
- var _a;
2552
2665
  const text = input.value.trim();
2553
2666
  if (!text || this.state !== "live") return;
2554
2667
  this.draft = "";
2555
2668
  input.value = "";
2556
2669
  syncState();
2557
- void ((_a = this.agent) == null ? void 0 : _a.sendTextMessage(text));
2670
+ void this.deliver(text);
2558
2671
  };
2559
2672
  input.addEventListener("input", () => {
2560
2673
  this.draft = input.value;
@@ -2622,7 +2735,7 @@ var TrilletWidgetElement = class extends HTMLElement {
2622
2735
  }
2623
2736
  /** Connecting, or the visitor spoke last and the agent hasn't started answering. */
2624
2737
  isAwaitingReply() {
2625
- if (this.state === "connecting" || this.pendingMessage) return true;
2738
+ if (this.state === "connecting" || this.held.holding) return true;
2626
2739
  if (this.state !== "live") return false;
2627
2740
  const last = [...this.transcripts].reverse().find((t) => !t.toolUsed && t.isFinal);
2628
2741
  return (last == null ? void 0 : last.role) === "user";
@@ -2632,7 +2745,7 @@ var TrilletWidgetElement = class extends HTMLElement {
2632
2745
  * there is no text thread on screen, in which case the caller does a full render.
2633
2746
  */
2634
2747
  updateThread() {
2635
- if (!this.open) return false;
2748
+ if (!this.isOpen) return false;
2636
2749
  const thread = this.root.querySelector(".tw-chat");
2637
2750
  if (!thread) return false;
2638
2751
  this.fillThread(thread);