@trillet-ai/web-sdk 1.11.0 → 1.12.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;
@@ -329,6 +371,15 @@ declare class TrilletWidgetElement extends HTMLElement {
329
371
  startConversation(modality?: ActiveMode): Promise<void>;
330
372
  /** Imperatively end the active conversation. Primary headless API. */
331
373
  endConversation(): void;
374
+ /**
375
+ * Fully reset: tear down any live session (without dispatching its end-of-call
376
+ * events — this isn't "the conversation ended", it's "forget it happened"), wipe the
377
+ * transcript, and start a brand new one. Reachable at any time, live or already
378
+ * ended — unlike {@link endConversation}, which keeps the transcript visible, this
379
+ * always lands back in a fresh pre-call state so quick-question suggestions and the
380
+ * dynamic-variables form (if configured) reappear.
381
+ */
382
+ startOver(modality?: ActiveMode): void;
332
383
  /** @deprecated Use {@link startConversation}. Kept as a thin alias for back-compat. */
333
384
  startCall(): Promise<void>;
334
385
  /** @deprecated Use {@link endConversation}. Kept as a thin alias for back-compat. */
@@ -356,6 +407,7 @@ declare class TrilletWidgetElement extends HTMLElement {
356
407
  private flushPending;
357
408
  private notice;
358
409
  private switchingSide;
410
+ private callGeneration;
359
411
  private beginCall;
360
412
  private wireAgentEvents;
361
413
  private endLocalCall;
@@ -399,6 +451,9 @@ declare class TrilletWidgetElement extends HTMLElement {
399
451
  private termsAccepted;
400
452
  private termsAcceptedThisPage;
401
453
  private renderTerms;
454
+ private showVariablesForm;
455
+ private dynamicVariableValues;
456
+ private renderVariablesForm;
402
457
  /** "How was this conversation?" thumbs, once, after a conversation ends. */
403
458
  private renderFeedback;
404
459
  /**
@@ -415,9 +470,11 @@ declare class TrilletWidgetElement extends HTMLElement {
415
470
  /** Suggested questions just above the ask bar while it has focus. */
416
471
  private renderAskSuggestions;
417
472
  /**
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.
473
+ * Open the panel. A text-only widget with no prior conversation starts the chat
474
+ * straight away, so the agent's welcome is already there when the panel opens, like
475
+ * any website chat. `both` waits for the visitor to pick call or message. Reopening
476
+ * never clears anything on its own — an ended conversation's transcript stays put
477
+ * until the visitor explicitly starts over (see {@link startOver}).
421
478
  */
422
479
  private openPanel;
423
480
  private renderPanel;
@@ -432,7 +489,12 @@ declare class TrilletWidgetElement extends HTMLElement {
432
489
  private renderHeader;
433
490
  /** Start, call and gate screens centre a large avatar in the body. */
434
491
  private bodyShowsLargeAvatar;
435
- /** True when a `both` widget is idle/ended with no modality chosen yet. */
492
+ /**
493
+ * True when a `both` widget is idle/ended with no modality chosen and nothing to
494
+ * show. A finished conversation keeps its thread on screen instead, so ending a call
495
+ * never hides what was said; the choose-how-to-talk entry comes back only once the
496
+ * visitor starts over.
497
+ */
436
498
  private isBothPreCall;
437
499
  /** True when a site key is configured and this session has not solved it yet. */
438
500
  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,33 @@ 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-input {
1023
+ height: 38px;
1024
+ padding: 0 10px;
1025
+ border: 1px solid var(--tw-line);
1026
+ border-radius: 8px;
1027
+ background: var(--tw-surface);
1028
+ color: var(--tw-ink);
1029
+ font: inherit;
1030
+ font-size: 14px;
1031
+ }
1032
+ .tw-field-input:focus-visible { outline: none; border-color: var(--tw-accent); }
1033
+ .tw-field-error { margin: 0; font-size: 12px; color: var(--tw-danger); }
1034
+ .tw-variables-form .tw-pill {
1035
+ flex: 1;
1036
+ height: 40px;
1037
+ border: none;
1038
+ border-radius: 8px;
1039
+ background: var(--tw-accent);
1040
+ color: var(--tw-on-accent);
1041
+ font-weight: 600;
1042
+ cursor: pointer;
1043
+ }
1044
+
969
1045
  /* ---- Feedback ---- */
970
1046
  .tw-feedback {
971
1047
  display: flex;
@@ -1178,6 +1254,9 @@ var TrilletWidgetElement = class extends HTMLElement {
1178
1254
  // Inline notice in the chat (e.g. microphone refused). Cleared on the next switch.
1179
1255
  this.notice = null;
1180
1256
  this.switchingSide = false;
1257
+ // Bumped on every start/reset so a slow credential failure from a conversation the
1258
+ // visitor has already moved on from cannot disturb the current one.
1259
+ this.callGeneration = 0;
1181
1260
  this.smoothedLevel = 0;
1182
1261
  // ---- Terms gate + feedback --------------------------------------------------
1183
1262
  this.showTerms = false;
@@ -1186,6 +1265,13 @@ var TrilletWidgetElement = class extends HTMLElement {
1186
1265
  this.feedbackState = "hidden";
1187
1266
  // Fallback when storage is blocked: accepted for the life of this page.
1188
1267
  this.termsAcceptedThisPage = false;
1268
+ // ---- Pre-call dynamic-variables form ----------------------------------------
1269
+ this.showVariablesForm = false;
1270
+ // null = not collected yet, which is what gates beginCall when prompts are
1271
+ // configured; {} once submitted, even with every field left blank. Cleared by
1272
+ // startOver(), so a fresh conversation asks again — every restart path runs through
1273
+ // startOver(), so in practice the visitor is asked once per conversation.
1274
+ this.dynamicVariableValues = null;
1189
1275
  this.askFocused = false;
1190
1276
  // Last start/connection failure, shown instead of the "ended" line.
1191
1277
  this.errorText = null;
@@ -1283,6 +1369,39 @@ var TrilletWidgetElement = class extends HTMLElement {
1283
1369
  var _a;
1284
1370
  (_a = this.agent) == null ? void 0 : _a.endCall();
1285
1371
  }
1372
+ /**
1373
+ * Fully reset: tear down any live session (without dispatching its end-of-call
1374
+ * events — this isn't "the conversation ended", it's "forget it happened"), wipe the
1375
+ * transcript, and start a brand new one. Reachable at any time, live or already
1376
+ * ended — unlike {@link endConversation}, which keeps the transcript visible, this
1377
+ * always lands back in a fresh pre-call state so quick-question suggestions and the
1378
+ * dynamic-variables form (if configured) reappear.
1379
+ */
1380
+ startOver(modality) {
1381
+ this.callGeneration += 1;
1382
+ this.clearDurationTimer();
1383
+ this.teardownAgent();
1384
+ if (this.pendingTimer) clearTimeout(this.pendingTimer);
1385
+ this.pendingTimer = null;
1386
+ this.pendingMessage = null;
1387
+ this.transcripts = [];
1388
+ this.notice = null;
1389
+ this.errorText = null;
1390
+ this.feedbackState = "hidden";
1391
+ this.speaking = false;
1392
+ this.callId = null;
1393
+ this.display = null;
1394
+ this.dynamicVariableValues = null;
1395
+ this.state = "idle";
1396
+ if (this.config.mode === "both") {
1397
+ this.activeMode = modality != null ? modality : null;
1398
+ if (!modality) {
1399
+ this.render();
1400
+ return;
1401
+ }
1402
+ }
1403
+ void this.beginCall(modality);
1404
+ }
1286
1405
  /** @deprecated Use {@link startConversation}. Kept as a thin alias for back-compat. */
1287
1406
  startCall() {
1288
1407
  return this.startConversation();
@@ -1370,12 +1489,19 @@ var TrilletWidgetElement = class extends HTMLElement {
1370
1489
  this.render();
1371
1490
  return;
1372
1491
  }
1492
+ if (this.config.dynamicVariablePrompts.length > 0 && !this.dynamicVariableValues) {
1493
+ this.pendingStart = modality != null ? modality : this.resolvedMode();
1494
+ this.showVariablesForm = true;
1495
+ this.render();
1496
+ return;
1497
+ }
1373
1498
  this.feedbackState = "hidden";
1374
1499
  if (modality) this.activeMode = modality;
1375
1500
  const mode = this.resolvedMode();
1376
1501
  const session = this.config.mode === "both" ? "multimodal" : mode;
1377
1502
  this.notice = null;
1378
1503
  if (!this.switching) this.display = null;
1504
+ const generation = ++this.callGeneration;
1379
1505
  this.setState("connecting");
1380
1506
  try {
1381
1507
  const agent = new TrilletAgent({
@@ -1384,7 +1510,13 @@ var TrilletWidgetElement = class extends HTMLElement {
1384
1510
  apiBaseUrl: this.config.apiBase,
1385
1511
  mode: session,
1386
1512
  startWithVoice: session === "multimodal" && mode === "voice",
1387
- variables: this.config.variables
1513
+ // Presets, then what the visitor just told us, then an explicit `variables`/
1514
+ // `dynamic-variables` attribute (power-user override) wins over both.
1515
+ variables: {
1516
+ ...this.config.dynamicVariablePresets,
1517
+ ...this.dynamicVariableValues,
1518
+ ...this.config.variables
1519
+ }
1388
1520
  });
1389
1521
  this.agent = agent;
1390
1522
  this.wireAgentEvents(agent);
@@ -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,96 @@ 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.headerTitle() || this.config.copy.chooseModePrompt;
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
+ field.appendChild(label);
1895
+ const input = document.createElement("input");
1896
+ input.type = "text";
1897
+ input.id = inputId;
1898
+ input.className = "tw-field-input";
1899
+ if (prompt.placeholder) input.placeholder = prompt.placeholder;
1900
+ if (prompt.required) input.required = true;
1901
+ input.setAttribute("aria-describedby", errId);
1902
+ input.addEventListener("keydown", (event) => {
1903
+ if (event.key === "Enter") {
1904
+ event.preventDefault();
1905
+ submit.click();
1906
+ }
1907
+ });
1908
+ field.appendChild(input);
1909
+ const err = document.createElement("p");
1910
+ err.className = "tw-field-error";
1911
+ err.id = errId;
1912
+ err.textContent = this.config.copy.fieldRequired;
1913
+ err.hidden = true;
1914
+ field.appendChild(err);
1915
+ card.appendChild(field);
1916
+ entries.push({ key: prompt.key, required: Boolean(prompt.required), input, err });
1917
+ });
1918
+ const row = document.createElement("div");
1919
+ row.className = "tw-card-actions";
1920
+ row.appendChild(submit);
1921
+ const decline = document.createElement("button");
1922
+ decline.type = "button";
1923
+ decline.className = "tw-secondary-btn";
1924
+ decline.textContent = this.config.copy.termsDecline;
1925
+ decline.addEventListener("click", () => {
1926
+ this.showVariablesForm = false;
1927
+ this.pendingStart = null;
1928
+ this.open = false;
1929
+ this.render();
1930
+ });
1931
+ row.appendChild(decline);
1932
+ card.appendChild(row);
1933
+ return card;
1934
+ }
1709
1935
  /** "How was this conversation?" thumbs, once, after a conversation ends. */
1710
1936
  renderFeedback() {
1711
1937
  if (this.feedbackState === "hidden") return null;
@@ -1875,15 +2101,16 @@ var TrilletWidgetElement = class extends HTMLElement {
1875
2101
  stack.insertBefore(row, bar);
1876
2102
  }
1877
2103
  /**
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.
2104
+ * Open the panel. A text-only widget with no prior conversation starts the chat
2105
+ * straight away, so the agent's welcome is already there when the panel opens, like
2106
+ * any website chat. `both` waits for the visitor to pick call or message. Reopening
2107
+ * never clears anything on its own — an ended conversation's transcript stays put
2108
+ * until the visitor explicitly starts over (see {@link startOver}).
1881
2109
  */
1882
2110
  openPanel() {
1883
2111
  this.open = true;
1884
- const idle = this.state === "idle" || this.state === "ended";
1885
- if (this.config.mode === "text" && idle && !this.needsRecaptcha()) {
1886
- this.transcripts = [];
2112
+ const fresh = this.state === "idle" && this.transcripts.length === 0;
2113
+ if (this.config.mode === "text" && fresh && !this.needsRecaptcha()) {
1887
2114
  void this.beginCall("text");
1888
2115
  return;
1889
2116
  }
@@ -1966,9 +2193,14 @@ var TrilletWidgetElement = class extends HTMLElement {
1966
2193
  if (this.config.mode !== "both" && this.resolvedMode() === "voice") return true;
1967
2194
  return this.state === "idle" && this.transcripts.length === 0;
1968
2195
  }
1969
- /** True when a `both` widget is idle/ended with no modality chosen yet. */
2196
+ /**
2197
+ * True when a `both` widget is idle/ended with no modality chosen and nothing to
2198
+ * show. A finished conversation keeps its thread on screen instead, so ending a call
2199
+ * never hides what was said; the choose-how-to-talk entry comes back only once the
2200
+ * visitor starts over.
2201
+ */
1970
2202
  isBothPreCall() {
1971
- return this.config.mode === "both" && this.activeMode === null && (this.state === "idle" || this.state === "ended");
2203
+ return this.config.mode === "both" && this.activeMode === null && this.transcripts.length === 0 && (this.state === "idle" || this.state === "ended");
1972
2204
  }
1973
2205
  // ---- BYO reCAPTCHA gate ----------------------------------------------------
1974
2206
  /** True when a site key is configured and this session has not solved it yet. */
@@ -2081,10 +2313,7 @@ var TrilletWidgetElement = class extends HTMLElement {
2081
2313
  const span = document.createElement("span");
2082
2314
  span.textContent = text;
2083
2315
  btn.appendChild(span);
2084
- btn.addEventListener("click", () => {
2085
- if (modality === "text") this.transcripts = [];
2086
- void this.beginCall(modality);
2087
- });
2316
+ btn.addEventListener("click", () => this.startOver(modality));
2088
2317
  return btn;
2089
2318
  };
2090
2319
  choices.appendChild(choice(ICONS.phone, this.config.startCallText, "voice", true));
@@ -2140,10 +2369,10 @@ var TrilletWidgetElement = class extends HTMLElement {
2140
2369
  restart.type = "button";
2141
2370
  restart.className = "tw-secondary-btn tw-small";
2142
2371
  restart.textContent = this.config.copy.startNewChat;
2143
- restart.addEventListener("click", () => {
2144
- this.transcripts = [];
2145
- void this.beginCall("text");
2146
- });
2372
+ restart.addEventListener(
2373
+ "click",
2374
+ () => this.startOver(this.config.mode === "both" ? void 0 : this.resolvedMode())
2375
+ );
2147
2376
  footer2.appendChild(restart);
2148
2377
  body.appendChild(footer2);
2149
2378
  const feedback = this.renderFeedback();
@@ -2168,6 +2397,13 @@ var TrilletWidgetElement = class extends HTMLElement {
2168
2397
  endBtn.disabled = this.state === "connecting";
2169
2398
  endBtn.addEventListener("click", () => this.endConversation());
2170
2399
  footer.appendChild(endBtn);
2400
+ const startOverBtn = document.createElement("button");
2401
+ startOverBtn.className = "tw-link-btn";
2402
+ startOverBtn.type = "button";
2403
+ startOverBtn.textContent = this.config.copy.startOver;
2404
+ startOverBtn.disabled = this.state === "connecting";
2405
+ startOverBtn.addEventListener("click", () => this.startOver(this.resolvedMode()));
2406
+ footer.appendChild(startOverBtn);
2171
2407
  body.appendChild(footer);
2172
2408
  }
2173
2409
  /** Starter questions, shown until the visitor has said something. */
@@ -2199,9 +2435,7 @@ var TrilletWidgetElement = class extends HTMLElement {
2199
2435
  const span = document.createElement("span");
2200
2436
  span.textContent = isText ? this.config.copy.startChatText : this.config.startCallText;
2201
2437
  startBtn.appendChild(span);
2202
- startBtn.addEventListener("click", () => {
2203
- void this.beginCall();
2204
- });
2438
+ startBtn.addEventListener("click", () => this.startOver());
2205
2439
  controls.appendChild(startBtn);
2206
2440
  return controls;
2207
2441
  }