@trillet-ai/web-sdk 1.11.0 → 1.13.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.
@@ -39,6 +39,8 @@ interface WidgetCopy {
39
39
  callEnded: string;
40
40
  chatEnded: string;
41
41
  startNewChat: string;
42
+ /** Always-reachable reset action, live or ended — distinct from "end call". */
43
+ startOver: string;
42
44
  backToChat: string;
43
45
  /** `{name}` is replaced with the agent or brand name. */
44
46
  chatWithNamed: string;
@@ -65,6 +67,9 @@ interface WidgetCopy {
65
67
  typeInstead: string;
66
68
  showConversation: string;
67
69
  talkInstead: string;
70
+ /** Pre-call dynamic-variables form. */
71
+ continueText: string;
72
+ fieldRequired: string;
68
73
  }
69
74
 
70
75
  type WidgetVariant = 'compact' | 'expanded';
@@ -105,6 +110,23 @@ interface RemoteWidgetConfig {
105
110
  suggestedPrompts?: string[];
106
111
  /** Widget UI language (en | de | fr | it). Absent: the host page's <html lang>. */
107
112
  language?: string | null;
113
+ /**
114
+ * Variables collected before a call starts, as the dashboard saves them: 'preset'
115
+ * entries carry a fixed value, 'prompt' entries are asked of the visitor. This is
116
+ * what GET /web-call/public/widget-config serves inside `widget`; the widget splits
117
+ * it locally (see splitDynamicVariableDefs).
118
+ *
119
+ * Note these are public: the config endpoint is unauthenticated, so a preset value
120
+ * is readable by anyone and must never hold a secret.
121
+ */
122
+ dynamicVariables?: DynamicVariableDef[];
123
+ }
124
+ /** A visitor-facing "ask user" dynamic variable, collected before a call starts. */
125
+ interface DynamicVariablePrompt {
126
+ key: string;
127
+ label?: string;
128
+ placeholder?: string;
129
+ required?: boolean;
108
130
  }
109
131
  interface WidgetBrand {
110
132
  logo?: string;
@@ -171,6 +193,13 @@ interface WidgetConfig {
171
193
  feedback: 'none' | 'end';
172
194
  mode: WidgetMode;
173
195
  variables?: Record<string, unknown>;
196
+ /**
197
+ * Fixed values for "preset" dynamic variables, sent with every conversation. Not a
198
+ * place for secrets: they reach the browser and are readable by the visitor.
199
+ */
200
+ dynamicVariablePresets: Record<string, string>;
201
+ /** "Ask user" dynamic variables; the widget renders a form for these before a call starts. */
202
+ dynamicVariablePrompts: DynamicVariablePrompt[];
174
203
  maxCallDuration?: number;
175
204
  /**
176
205
  * Optional Google reCAPTCHA v2 site key. When set, the widget renders a checkbox the
@@ -185,6 +214,19 @@ interface WidgetConfig {
185
214
  disableBanner: boolean;
186
215
  brand: WidgetBrand;
187
216
  }
217
+ /**
218
+ * A single dynamic-variable definition — the same preset/ask-user shape the Widget
219
+ * Studio and the demo-link editor both save (see trillet-backend's widgetConfig.js).
220
+ * Arrives either on the `dynamic-variable-defs` attribute or inside the saved design.
221
+ */
222
+ interface DynamicVariableDef {
223
+ key: string;
224
+ mode: 'preset' | 'prompt';
225
+ value?: string;
226
+ label?: string;
227
+ placeholder?: string;
228
+ required?: boolean;
229
+ }
188
230
 
189
231
  interface TrilletWidgetProps {
190
232
  agentId: string;
@@ -297,6 +339,14 @@ declare class TrilletWidgetElement extends HTMLElement {
297
339
  mode: ActiveMode;
298
340
  /** Session type to mint: `multimodal` for mode="both" (type or talk). */
299
341
  session: SessionMode;
342
+ /**
343
+ * Presets plus whatever the visitor just entered, resolved for this
344
+ * conversation. A room minted here is built before the widget can send
345
+ * anything itself, so a minting endpoint has to forward these as
346
+ * `dynamic_variables` or the agent never sees them and `{{key}}` stays
347
+ * literal in the prompt.
348
+ */
349
+ variables: Record<string, unknown>;
300
350
  }) => Promise<TrilletRoomDetails> | TrilletRoomDetails;
301
351
  roomDetails?: TrilletRoomDetails;
302
352
  private durationTimer;
@@ -329,6 +379,15 @@ declare class TrilletWidgetElement extends HTMLElement {
329
379
  startConversation(modality?: ActiveMode): Promise<void>;
330
380
  /** Imperatively end the active conversation. Primary headless API. */
331
381
  endConversation(): void;
382
+ /**
383
+ * Fully reset: tear down any live session (without dispatching its end-of-call
384
+ * events — this isn't "the conversation ended", it's "forget it happened"), wipe the
385
+ * transcript, and start a brand new one. Reachable at any time, live or already
386
+ * ended — unlike {@link endConversation}, which keeps the transcript visible, this
387
+ * always lands back in a fresh pre-call state so quick-question suggestions and the
388
+ * dynamic-variables form (if configured) reappear.
389
+ */
390
+ startOver(modality?: ActiveMode): void;
332
391
  /** @deprecated Use {@link startConversation}. Kept as a thin alias for back-compat. */
333
392
  startCall(): Promise<void>;
334
393
  /** @deprecated Use {@link endConversation}. Kept as a thin alias for back-compat. */
@@ -356,6 +415,7 @@ declare class TrilletWidgetElement extends HTMLElement {
356
415
  private flushPending;
357
416
  private notice;
358
417
  private switchingSide;
418
+ private callGeneration;
359
419
  private beginCall;
360
420
  private wireAgentEvents;
361
421
  private endLocalCall;
@@ -399,6 +459,9 @@ declare class TrilletWidgetElement extends HTMLElement {
399
459
  private termsAccepted;
400
460
  private termsAcceptedThisPage;
401
461
  private renderTerms;
462
+ private showVariablesForm;
463
+ private dynamicVariableValues;
464
+ private renderVariablesForm;
402
465
  /** "How was this conversation?" thumbs, once, after a conversation ends. */
403
466
  private renderFeedback;
404
467
  /**
@@ -415,9 +478,11 @@ declare class TrilletWidgetElement extends HTMLElement {
415
478
  /** Suggested questions just above the ask bar while it has focus. */
416
479
  private renderAskSuggestions;
417
480
  /**
418
- * Open the panel. A text-only widget starts the chat straight away, so the agent's
419
- * welcome is already there when the panel opens, like any website chat. `both`
420
- * waits for the visitor to pick call or message.
481
+ * Open the panel. A text-only widget with no prior conversation starts the chat
482
+ * straight away, so the agent's welcome is already there when the panel opens, like
483
+ * any website chat. `both` waits for the visitor to pick call or message. Reopening
484
+ * never clears anything on its own — an ended conversation's transcript stays put
485
+ * until the visitor explicitly starts over (see {@link startOver}).
421
486
  */
422
487
  private openPanel;
423
488
  private renderPanel;
@@ -432,7 +497,12 @@ declare class TrilletWidgetElement extends HTMLElement {
432
497
  private renderHeader;
433
498
  /** Start, call and gate screens centre a large avatar in the body. */
434
499
  private bodyShowsLargeAvatar;
435
- /** True when a `both` widget is idle/ended with no modality chosen yet. */
500
+ /**
501
+ * True when a `both` widget is idle/ended with no modality chosen and nothing to
502
+ * show. A finished conversation keeps its thread on screen instead, so ending a call
503
+ * never hides what was said; the choose-how-to-talk entry comes back only once the
504
+ * visitor starts over.
505
+ */
436
506
  private isBothPreCall;
437
507
  /** True when a site key is configured and this session has not solved it yet. */
438
508
  private needsRecaptcha;
@@ -34,6 +34,7 @@ var WIDGET_COPY = {
34
34
  callEnded: "Call ended",
35
35
  chatEnded: "This chat has ended.",
36
36
  startNewChat: "Start a new chat",
37
+ startOver: "Start over",
37
38
  backToChat: "Back to chat",
38
39
  chatWithNamed: "Chat with {name}",
39
40
  chat: "Chat",
@@ -58,7 +59,9 @@ var WIDGET_COPY = {
58
59
  minimize: "Minimize",
59
60
  typeInstead: "Type instead",
60
61
  showConversation: "Show the conversation",
61
- talkInstead: "Talk instead"
62
+ talkInstead: "Talk instead",
63
+ continueText: "Continue",
64
+ fieldRequired: "This field is required."
62
65
  },
63
66
  de: {
64
67
  startCallText: "Anruf starten",
@@ -86,6 +89,7 @@ var WIDGET_COPY = {
86
89
  callEnded: "Anruf beendet",
87
90
  chatEnded: "Dieser Chat wurde beendet.",
88
91
  startNewChat: "Neuen Chat starten",
92
+ startOver: "Neu starten",
89
93
  backToChat: "Zur\xFCck zum Chat",
90
94
  chatWithNamed: "Chat mit {name}",
91
95
  chat: "Chat",
@@ -110,7 +114,9 @@ var WIDGET_COPY = {
110
114
  minimize: "Minimieren",
111
115
  typeInstead: "Lieber schreiben",
112
116
  showConversation: "Gespr\xE4ch anzeigen",
113
- talkInstead: "Lieber sprechen"
117
+ talkInstead: "Lieber sprechen",
118
+ continueText: "Weiter",
119
+ fieldRequired: "Dieses Feld ist erforderlich."
114
120
  },
115
121
  fr: {
116
122
  startCallText: "D\xE9marrer un appel",
@@ -138,6 +144,7 @@ var WIDGET_COPY = {
138
144
  callEnded: "Appel termin\xE9",
139
145
  chatEnded: "Ce chat est termin\xE9.",
140
146
  startNewChat: "D\xE9marrer un nouveau chat",
147
+ startOver: "Recommencer",
141
148
  backToChat: "Retour au chat",
142
149
  chatWithNamed: "Chat avec {name}",
143
150
  chat: "Chat",
@@ -162,7 +169,9 @@ var WIDGET_COPY = {
162
169
  minimize: "R\xE9duire",
163
170
  typeInstead: "\xC9crire plut\xF4t",
164
171
  showConversation: "Afficher la conversation",
165
- talkInstead: "Parler plut\xF4t"
172
+ talkInstead: "Parler plut\xF4t",
173
+ continueText: "Continuer",
174
+ fieldRequired: "Ce champ est obligatoire."
166
175
  },
167
176
  it: {
168
177
  startCallText: "Avvia una chiamata",
@@ -190,6 +199,7 @@ var WIDGET_COPY = {
190
199
  callEnded: "Chiamata terminata",
191
200
  chatEnded: "Questa chat \xE8 terminata.",
192
201
  startNewChat: "Avvia una nuova chat",
202
+ startOver: "Ricomincia",
193
203
  backToChat: "Torna alla chat",
194
204
  chatWithNamed: "Chat con {name}",
195
205
  chat: "Chat",
@@ -214,7 +224,9 @@ var WIDGET_COPY = {
214
224
  minimize: "Riduci a icona",
215
225
  typeInstead: "Preferisco scrivere",
216
226
  showConversation: "Mostra la conversazione",
217
- talkInstead: "Preferisco parlare"
227
+ talkInstead: "Preferisco parlare",
228
+ continueText: "Continua",
229
+ fieldRequired: "Questo campo \xE8 obbligatorio."
218
230
  }
219
231
  };
220
232
  function normalizeLanguage(tag) {
@@ -278,6 +290,10 @@ var OBSERVED_ATTRIBUTES = [
278
290
  "variables",
279
291
  "dynamic-variables",
280
292
  // convai alias -> variables
293
+ // The variable *definitions* (preset/ask-user specs) rendered as a pre-call form —
294
+ // distinct from `variables`/`dynamic-variables` above, which carry the final
295
+ // key/value map sent into the call.
296
+ "dynamic-variable-defs",
281
297
  "max-call-duration",
282
298
  "recaptcha-site-key",
283
299
  "disable-banner",
@@ -317,6 +333,35 @@ var HEX = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
317
333
  function hexOrUndefined(value) {
318
334
  return value && HEX.test(value.trim()) ? value.trim() : void 0;
319
335
  }
336
+ function splitDynamicVariableDefs(defs) {
337
+ var _a;
338
+ const presets = {};
339
+ const prompts = [];
340
+ for (const def of defs) {
341
+ if (!def || typeof def.key !== "string" || !def.key) continue;
342
+ if (def.mode === "prompt") {
343
+ prompts.push({
344
+ key: def.key,
345
+ label: def.label || void 0,
346
+ placeholder: def.placeholder || void 0,
347
+ required: Boolean(def.required)
348
+ });
349
+ } else {
350
+ presets[def.key] = (_a = def.value) != null ? _a : "";
351
+ }
352
+ }
353
+ return { presets, prompts };
354
+ }
355
+ function parseDynamicVariableDefsAttr(value) {
356
+ if (!value) return void 0;
357
+ try {
358
+ const parsed = JSON.parse(value);
359
+ if (Array.isArray(parsed)) return splitDynamicVariableDefs(parsed);
360
+ } catch (e) {
361
+ console.warn("[trillet-widget] Failed to parse dynamic-variable-defs:", value);
362
+ }
363
+ return void 0;
364
+ }
320
365
  function parsePromptsAttr(value) {
321
366
  if (value === null) return void 0;
322
367
  const trimmed = value.trim();
@@ -345,7 +390,7 @@ function resolveVariant(raw) {
345
390
  }
346
391
  }
347
392
  function configFromElement(el, remote) {
348
- var _a, _b, _c;
393
+ var _a, _b, _c, _d, _e, _f;
349
394
  const r = remote || {};
350
395
  const get = (name, fallback) => {
351
396
  var _a2;
@@ -374,6 +419,7 @@ function configFromElement(el, remote) {
374
419
  const orbColor2 = get("avatar-orb-color-2", r.orbColor2) || void 0;
375
420
  const styleRaw = get("avatar-style", r.avatarStyle);
376
421
  const avatarStyle = styleRaw === "orb" || styleRaw === "image" || styleRaw === "initials" ? styleRaw : void 0;
422
+ const defs = (_b = parseDynamicVariableDefsAttr(el.getAttribute("dynamic-variable-defs"))) != null ? _b : Array.isArray(r.dynamicVariables) ? splitDynamicVariableDefs(r.dynamicVariables) : void 0;
377
423
  return {
378
424
  agentId: get("agent-id") || "",
379
425
  signedUrlEndpoint: get("signed-url-endpoint") || void 0,
@@ -410,7 +456,7 @@ function configFromElement(el, remote) {
410
456
  recaptchaText: get("recaptcha-text") || copy.recaptchaText,
411
457
  greeting: get("greeting", r.greeting) || void 0,
412
458
  inputPlaceholder: get("input-placeholder", r.inputPlaceholder) || copy.inputPlaceholder,
413
- suggestedPrompts: (_c = (_b = parsePromptsAttr(el.getAttribute("suggested-prompts"))) != null ? _b : r.suggestedPrompts) != null ? _c : [],
459
+ suggestedPrompts: (_d = (_c = parsePromptsAttr(el.getAttribute("suggested-prompts"))) != null ? _c : r.suggestedPrompts) != null ? _d : [],
414
460
  theme: get("theme", r.theme) === "dark" ? "dark" : "light",
415
461
  backgroundColor: hexOrUndefined(get("bg-color", r.backgroundColor)),
416
462
  textColor: hexOrUndefined(get("text-color", r.textColor)),
@@ -420,6 +466,8 @@ function configFromElement(el, remote) {
420
466
  mode,
421
467
  // convai `dynamic-variables` is an alias of our `variables`.
422
468
  variables: parseJsonAttr(get("variables") || get("dynamic-variables")),
469
+ dynamicVariablePresets: (_e = defs == null ? void 0 : defs.presets) != null ? _e : {},
470
+ dynamicVariablePrompts: (_f = defs == null ? void 0 : defs.prompts) != null ? _f : [],
423
471
  maxCallDuration: parseNumberAttr(get("max-call-duration")),
424
472
  recaptchaSiteKey: get("recaptcha-site-key") || void 0,
425
473
  disableBanner: parseBoolAttr(get("disable-banner")),
@@ -912,6 +960,7 @@ svg { display: block; flex-shrink: 0; }
912
960
  display: flex;
913
961
  align-items: center;
914
962
  justify-content: flex-end;
963
+ gap: 12px;
915
964
  min-height: 30px;
916
965
  padding: 2px 14px 6px;
917
966
  }
@@ -966,6 +1015,34 @@ svg { display: block; flex-shrink: 0; }
966
1015
  }
967
1016
  .tw-terms .tw-secondary-btn { height: 40px; }
968
1017
 
1018
+ /* ---- Pre-call dynamic-variables form ---- */
1019
+ .tw-variables-form { width: 320px; }
1020
+ .tw-field { display: flex; flex-direction: column; gap: 4px; }
1021
+ .tw-field-label { font-size: 13px; font-weight: 500; color: var(--tw-muted); }
1022
+ .tw-field-required { color: var(--tw-danger); margin-left: 2px; }
1023
+ .tw-field-input {
1024
+ height: 38px;
1025
+ padding: 0 10px;
1026
+ border: 1px solid var(--tw-line);
1027
+ border-radius: 8px;
1028
+ background: var(--tw-surface);
1029
+ color: var(--tw-ink);
1030
+ font: inherit;
1031
+ font-size: 14px;
1032
+ }
1033
+ .tw-field-input:focus-visible { outline: none; border-color: var(--tw-accent); }
1034
+ .tw-field-error { margin: 0; font-size: 12px; color: var(--tw-danger); }
1035
+ .tw-variables-form .tw-pill {
1036
+ flex: 1;
1037
+ height: 40px;
1038
+ border: none;
1039
+ border-radius: 8px;
1040
+ background: var(--tw-accent);
1041
+ color: var(--tw-on-accent);
1042
+ font-weight: 600;
1043
+ cursor: pointer;
1044
+ }
1045
+
969
1046
  /* ---- Feedback ---- */
970
1047
  .tw-feedback {
971
1048
  display: flex;
@@ -1178,6 +1255,9 @@ var TrilletWidgetElement = class extends HTMLElement {
1178
1255
  // Inline notice in the chat (e.g. microphone refused). Cleared on the next switch.
1179
1256
  this.notice = null;
1180
1257
  this.switchingSide = false;
1258
+ // Bumped on every start/reset so a slow credential failure from a conversation the
1259
+ // visitor has already moved on from cannot disturb the current one.
1260
+ this.callGeneration = 0;
1181
1261
  this.smoothedLevel = 0;
1182
1262
  // ---- Terms gate + feedback --------------------------------------------------
1183
1263
  this.showTerms = false;
@@ -1186,6 +1266,13 @@ var TrilletWidgetElement = class extends HTMLElement {
1186
1266
  this.feedbackState = "hidden";
1187
1267
  // Fallback when storage is blocked: accepted for the life of this page.
1188
1268
  this.termsAcceptedThisPage = false;
1269
+ // ---- Pre-call dynamic-variables form ----------------------------------------
1270
+ this.showVariablesForm = false;
1271
+ // null = not collected yet, which is what gates beginCall when prompts are
1272
+ // configured; {} once submitted, even with every field left blank. Cleared by
1273
+ // startOver(), so a fresh conversation asks again — every restart path runs through
1274
+ // startOver(), so in practice the visitor is asked once per conversation.
1275
+ this.dynamicVariableValues = null;
1189
1276
  this.askFocused = false;
1190
1277
  // Last start/connection failure, shown instead of the "ended" line.
1191
1278
  this.errorText = null;
@@ -1283,6 +1370,39 @@ var TrilletWidgetElement = class extends HTMLElement {
1283
1370
  var _a;
1284
1371
  (_a = this.agent) == null ? void 0 : _a.endCall();
1285
1372
  }
1373
+ /**
1374
+ * Fully reset: tear down any live session (without dispatching its end-of-call
1375
+ * events — this isn't "the conversation ended", it's "forget it happened"), wipe the
1376
+ * transcript, and start a brand new one. Reachable at any time, live or already
1377
+ * ended — unlike {@link endConversation}, which keeps the transcript visible, this
1378
+ * always lands back in a fresh pre-call state so quick-question suggestions and the
1379
+ * dynamic-variables form (if configured) reappear.
1380
+ */
1381
+ startOver(modality) {
1382
+ this.callGeneration += 1;
1383
+ this.clearDurationTimer();
1384
+ this.teardownAgent();
1385
+ if (this.pendingTimer) clearTimeout(this.pendingTimer);
1386
+ this.pendingTimer = null;
1387
+ this.pendingMessage = null;
1388
+ this.transcripts = [];
1389
+ this.notice = null;
1390
+ this.errorText = null;
1391
+ this.feedbackState = "hidden";
1392
+ this.speaking = false;
1393
+ this.callId = null;
1394
+ this.display = null;
1395
+ this.dynamicVariableValues = null;
1396
+ this.state = "idle";
1397
+ if (this.config.mode === "both") {
1398
+ this.activeMode = modality != null ? modality : null;
1399
+ if (!modality) {
1400
+ this.render();
1401
+ return;
1402
+ }
1403
+ }
1404
+ void this.beginCall(modality);
1405
+ }
1286
1406
  /** @deprecated Use {@link startConversation}. Kept as a thin alias for back-compat. */
1287
1407
  startCall() {
1288
1408
  return this.startConversation();
@@ -1370,12 +1490,24 @@ var TrilletWidgetElement = class extends HTMLElement {
1370
1490
  this.render();
1371
1491
  return;
1372
1492
  }
1493
+ if (this.config.dynamicVariablePrompts.length > 0 && !this.dynamicVariableValues) {
1494
+ this.pendingStart = modality != null ? modality : this.resolvedMode();
1495
+ this.showVariablesForm = true;
1496
+ this.render();
1497
+ return;
1498
+ }
1373
1499
  this.feedbackState = "hidden";
1374
1500
  if (modality) this.activeMode = modality;
1375
1501
  const mode = this.resolvedMode();
1376
1502
  const session = this.config.mode === "both" ? "multimodal" : mode;
1377
1503
  this.notice = null;
1378
1504
  if (!this.switching) this.display = null;
1505
+ const variables = {
1506
+ ...this.config.dynamicVariablePresets,
1507
+ ...this.dynamicVariableValues,
1508
+ ...this.config.variables
1509
+ };
1510
+ const generation = ++this.callGeneration;
1379
1511
  this.setState("connecting");
1380
1512
  try {
1381
1513
  const agent = new TrilletAgent({
@@ -1384,14 +1516,14 @@ var TrilletWidgetElement = class extends HTMLElement {
1384
1516
  apiBaseUrl: this.config.apiBase,
1385
1517
  mode: session,
1386
1518
  startWithVoice: session === "multimodal" && mode === "voice",
1387
- variables: this.config.variables
1519
+ variables
1388
1520
  });
1389
1521
  this.agent = agent;
1390
1522
  this.wireAgentEvents(agent);
1391
1523
  if (this.config.signedUrlEndpoint || this.getRoomDetails) {
1392
1524
  let details;
1393
1525
  if (this.getRoomDetails) {
1394
- details = await this.getRoomDetails({ mode, session });
1526
+ details = await this.getRoomDetails({ mode, session, variables });
1395
1527
  } else {
1396
1528
  const res = await fetch(this.config.signedUrlEndpoint, {
1397
1529
  method: "GET",
@@ -1413,6 +1545,7 @@ var TrilletWidgetElement = class extends HTMLElement {
1413
1545
  );
1414
1546
  }
1415
1547
  } catch (error) {
1548
+ if (generation !== this.callGeneration) return;
1416
1549
  this.handleError(error);
1417
1550
  }
1418
1551
  }
@@ -1590,6 +1723,9 @@ var TrilletWidgetElement = class extends HTMLElement {
1590
1723
  if (this.showTerms) {
1591
1724
  view = "terms";
1592
1725
  el = this.renderTerms();
1726
+ } else if (this.showVariablesForm) {
1727
+ view = "variables";
1728
+ el = this.renderVariablesForm();
1593
1729
  } else if (this.open) {
1594
1730
  view = "panel";
1595
1731
  el = this.renderPanel();
@@ -1706,6 +1842,104 @@ var TrilletWidgetElement = class extends HTMLElement {
1706
1842
  card.appendChild(row);
1707
1843
  return card;
1708
1844
  }
1845
+ renderVariablesForm() {
1846
+ const card = document.createElement("div");
1847
+ card.className = "tw-card tw-variables-form";
1848
+ card.setAttribute("role", "dialog");
1849
+ card.setAttribute("aria-label", this.config.copy.continueText);
1850
+ const title = document.createElement("div");
1851
+ title.className = "tw-card-title";
1852
+ title.textContent = this.config.copy.termsTitle;
1853
+ card.appendChild(title);
1854
+ const entries = [];
1855
+ const submit = document.createElement("button");
1856
+ submit.type = "button";
1857
+ submit.className = "tw-pill";
1858
+ submit.textContent = this.config.copy.continueText;
1859
+ submit.addEventListener("click", () => {
1860
+ var _a;
1861
+ const values = {};
1862
+ let valid = true;
1863
+ for (const entry of entries) {
1864
+ const value = entry.input.value.trim();
1865
+ const missing = entry.required && !value;
1866
+ entry.err.hidden = !missing;
1867
+ entry.input.setAttribute("aria-invalid", missing ? "true" : "false");
1868
+ if (missing) {
1869
+ valid = false;
1870
+ continue;
1871
+ }
1872
+ if (value) values[entry.key] = value;
1873
+ }
1874
+ if (!valid) {
1875
+ (_a = entries.find((entry) => !entry.err.hidden)) == null ? void 0 : _a.input.focus();
1876
+ return;
1877
+ }
1878
+ this.dynamicVariableValues = values;
1879
+ this.showVariablesForm = false;
1880
+ const start = this.pendingStart;
1881
+ this.pendingStart = null;
1882
+ if (start === "text") this.open = true;
1883
+ void this.beginCall(start != null ? start : void 0);
1884
+ });
1885
+ this.config.dynamicVariablePrompts.forEach((prompt, index) => {
1886
+ const field = document.createElement("div");
1887
+ field.className = "tw-field";
1888
+ const inputId = `tw-var-${index}`;
1889
+ const errId = `tw-var-err-${index}`;
1890
+ const label = document.createElement("label");
1891
+ label.className = "tw-field-label";
1892
+ label.htmlFor = inputId;
1893
+ label.textContent = prompt.label || prompt.key;
1894
+ if (prompt.required) {
1895
+ const required = document.createElement("span");
1896
+ required.className = "tw-field-required";
1897
+ required.textContent = "*";
1898
+ required.setAttribute("aria-hidden", "true");
1899
+ label.appendChild(required);
1900
+ }
1901
+ field.appendChild(label);
1902
+ const input = document.createElement("input");
1903
+ input.type = "text";
1904
+ input.id = inputId;
1905
+ input.className = "tw-field-input";
1906
+ if (prompt.placeholder) input.placeholder = prompt.placeholder;
1907
+ if (prompt.required) input.required = true;
1908
+ input.setAttribute("aria-required", prompt.required ? "true" : "false");
1909
+ input.setAttribute("aria-describedby", errId);
1910
+ input.addEventListener("keydown", (event) => {
1911
+ if (event.key === "Enter") {
1912
+ event.preventDefault();
1913
+ submit.click();
1914
+ }
1915
+ });
1916
+ field.appendChild(input);
1917
+ const err = document.createElement("p");
1918
+ err.className = "tw-field-error";
1919
+ err.id = errId;
1920
+ err.textContent = this.config.copy.fieldRequired;
1921
+ err.hidden = true;
1922
+ field.appendChild(err);
1923
+ card.appendChild(field);
1924
+ entries.push({ key: prompt.key, required: Boolean(prompt.required), input, err });
1925
+ });
1926
+ const row = document.createElement("div");
1927
+ row.className = "tw-card-actions";
1928
+ row.appendChild(submit);
1929
+ const decline = document.createElement("button");
1930
+ decline.type = "button";
1931
+ decline.className = "tw-secondary-btn";
1932
+ decline.textContent = this.config.copy.termsDecline;
1933
+ decline.addEventListener("click", () => {
1934
+ this.showVariablesForm = false;
1935
+ this.pendingStart = null;
1936
+ this.open = false;
1937
+ this.render();
1938
+ });
1939
+ row.appendChild(decline);
1940
+ card.appendChild(row);
1941
+ return card;
1942
+ }
1709
1943
  /** "How was this conversation?" thumbs, once, after a conversation ends. */
1710
1944
  renderFeedback() {
1711
1945
  if (this.feedbackState === "hidden") return null;
@@ -1875,15 +2109,16 @@ var TrilletWidgetElement = class extends HTMLElement {
1875
2109
  stack.insertBefore(row, bar);
1876
2110
  }
1877
2111
  /**
1878
- * Open the panel. A text-only widget starts the chat straight away, so the agent's
1879
- * welcome is already there when the panel opens, like any website chat. `both`
1880
- * waits for the visitor to pick call or message.
2112
+ * Open the panel. A text-only widget with no prior conversation starts the chat
2113
+ * straight away, so the agent's welcome is already there when the panel opens, like
2114
+ * any website chat. `both` waits for the visitor to pick call or message. Reopening
2115
+ * never clears anything on its own — an ended conversation's transcript stays put
2116
+ * until the visitor explicitly starts over (see {@link startOver}).
1881
2117
  */
1882
2118
  openPanel() {
1883
2119
  this.open = true;
1884
- const idle = this.state === "idle" || this.state === "ended";
1885
- if (this.config.mode === "text" && idle && !this.needsRecaptcha()) {
1886
- this.transcripts = [];
2120
+ const fresh = this.state === "idle" && this.transcripts.length === 0;
2121
+ if (this.config.mode === "text" && fresh && !this.needsRecaptcha()) {
1887
2122
  void this.beginCall("text");
1888
2123
  return;
1889
2124
  }
@@ -1966,9 +2201,14 @@ var TrilletWidgetElement = class extends HTMLElement {
1966
2201
  if (this.config.mode !== "both" && this.resolvedMode() === "voice") return true;
1967
2202
  return this.state === "idle" && this.transcripts.length === 0;
1968
2203
  }
1969
- /** True when a `both` widget is idle/ended with no modality chosen yet. */
2204
+ /**
2205
+ * True when a `both` widget is idle/ended with no modality chosen and nothing to
2206
+ * show. A finished conversation keeps its thread on screen instead, so ending a call
2207
+ * never hides what was said; the choose-how-to-talk entry comes back only once the
2208
+ * visitor starts over.
2209
+ */
1970
2210
  isBothPreCall() {
1971
- return this.config.mode === "both" && this.activeMode === null && (this.state === "idle" || this.state === "ended");
2211
+ return this.config.mode === "both" && this.activeMode === null && this.transcripts.length === 0 && (this.state === "idle" || this.state === "ended");
1972
2212
  }
1973
2213
  // ---- BYO reCAPTCHA gate ----------------------------------------------------
1974
2214
  /** True when a site key is configured and this session has not solved it yet. */
@@ -2081,10 +2321,7 @@ var TrilletWidgetElement = class extends HTMLElement {
2081
2321
  const span = document.createElement("span");
2082
2322
  span.textContent = text;
2083
2323
  btn.appendChild(span);
2084
- btn.addEventListener("click", () => {
2085
- if (modality === "text") this.transcripts = [];
2086
- void this.beginCall(modality);
2087
- });
2324
+ btn.addEventListener("click", () => this.startOver(modality));
2088
2325
  return btn;
2089
2326
  };
2090
2327
  choices.appendChild(choice(ICONS.phone, this.config.startCallText, "voice", true));
@@ -2140,10 +2377,10 @@ var TrilletWidgetElement = class extends HTMLElement {
2140
2377
  restart.type = "button";
2141
2378
  restart.className = "tw-secondary-btn tw-small";
2142
2379
  restart.textContent = this.config.copy.startNewChat;
2143
- restart.addEventListener("click", () => {
2144
- this.transcripts = [];
2145
- void this.beginCall("text");
2146
- });
2380
+ restart.addEventListener(
2381
+ "click",
2382
+ () => this.startOver(this.config.mode === "both" ? void 0 : this.resolvedMode())
2383
+ );
2147
2384
  footer2.appendChild(restart);
2148
2385
  body.appendChild(footer2);
2149
2386
  const feedback = this.renderFeedback();
@@ -2168,6 +2405,13 @@ var TrilletWidgetElement = class extends HTMLElement {
2168
2405
  endBtn.disabled = this.state === "connecting";
2169
2406
  endBtn.addEventListener("click", () => this.endConversation());
2170
2407
  footer.appendChild(endBtn);
2408
+ const startOverBtn = document.createElement("button");
2409
+ startOverBtn.className = "tw-link-btn";
2410
+ startOverBtn.type = "button";
2411
+ startOverBtn.textContent = this.config.copy.startOver;
2412
+ startOverBtn.disabled = this.state === "connecting";
2413
+ startOverBtn.addEventListener("click", () => this.startOver(this.resolvedMode()));
2414
+ footer.appendChild(startOverBtn);
2171
2415
  body.appendChild(footer);
2172
2416
  }
2173
2417
  /** Starter questions, shown until the visitor has said something. */
@@ -2199,9 +2443,7 @@ var TrilletWidgetElement = class extends HTMLElement {
2199
2443
  const span = document.createElement("span");
2200
2444
  span.textContent = isText ? this.config.copy.startChatText : this.config.startCallText;
2201
2445
  startBtn.appendChild(span);
2202
- startBtn.addEventListener("click", () => {
2203
- void this.beginCall();
2204
- });
2446
+ startBtn.addEventListener("click", () => this.startOver());
2205
2447
  controls.appendChild(startBtn);
2206
2448
  return controls;
2207
2449
  }