luzia-widget 0.1.2 → 0.1.4

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
@@ -16,6 +16,6 @@ import { defineLuziaWidget } from 'luzia-widget';
16
16
  defineLuziaWidget();
17
17
  ```
18
18
 
19
- Attributes: `endpoint`, `contact`, `greeting` (said by luzIA when a conversation starts; a default introduction when unset, none with `greeting=""`), `suggested-questions` (JSON array), `mode` (`bubble` | `inline`), `theme` (`light` | `dark` | `auto`), `avatar` (image URL shown large at the top of the chat, like a video call; luzIA's icon when unset). Theme colours are `--luzia-*` CSS custom properties. See the [root README](../../README.md#widget) for details. For React, use [`luzia-react`](../react).
19
+ Attributes: `endpoint`, `contact`, `greeting` (said by luzIA when a conversation starts; a default introduction when unset, none with `greeting=""`), `suggested-questions` (JSON array), `mode` (`bubble` | `inline`), `theme` (`light` | `dark` | `auto`), `avatar` (image URL shown large at the top of the chat, like a video call; luzIA's icon when unset), `avatar-speaking` (an animated version of the avatar, e.g. a GIF moving its mouth, shown while luzIA talks), `avatar-waving` (your hello animation: luzIA peeks out above the closed launcher with it; nothing peeks out without one, see [Hello animation](../../README.md#hello-animation)), `bubble-label` (text on the floating pill; `Ask luzIA about me` when unset). Theme colours are `--luzia-*` CSS custom properties. See the [root README](../../README.md#widget) for details. For React, use [`luzia-react`](../react).
20
20
 
21
21
  `pnpm build` emits the module build and `dist/luzia-widget.js` (the script bundle, built with esbuild from `src/register.ts`).
package/dist/element.d.ts CHANGED
@@ -22,7 +22,7 @@ export interface LuziaWidgetConfig {
22
22
  */
23
23
  suggestedQuestions?: readonly string[];
24
24
  /**
25
- * `bubble` (default): a floating button in the corner that opens the chat.
25
+ * `bubble` (default): a floating pill in the corner (see `bubbleLabel`) that opens the chat.
26
26
  * `inline`: the chat is rendered inside the element itself, always open,
27
27
  * so it can live in a page section.
28
28
  */
@@ -37,6 +37,27 @@ export interface LuziaWidgetConfig {
37
37
  * Owner's photo, a logo…). Without one, luzIA's own icon is shown.
38
38
  */
39
39
  avatar?: string;
40
+ /**
41
+ * URL of an animated version of `avatar` (a GIF, animated WebP or APNG,
42
+ * e.g. the Owner's picture moving its mouth), shown in its place while
43
+ * luzIA talks: as she says the greeting and while a reply streams in. Same
44
+ * size and framing as `avatar`, so one turns into the other in place.
45
+ */
46
+ avatarSpeaking?: string;
47
+ /**
48
+ * URL of the Owner's hello animation (waving, nodding…): an animated WebP,
49
+ * GIF, APNG or SVG with a transparent background, about 4 seconds long (half
50
+ * a second still as she rises, 3 of hello, half a second still as she ducks).
51
+ * While the chat is closed, luzIA peeks out above the launcher with it every
52
+ * 25 seconds (4 seconds each time), in bubble mode only. Without one, or
53
+ * when it can't be loaded, nothing peeks out.
54
+ */
55
+ avatarWaving?: string;
56
+ /**
57
+ * Text on the floating pill that opens the chat (bubble mode), e.g.
58
+ * `Pregúntale a luzIA sobre mí`. Default: `Ask luzIA about me`.
59
+ */
60
+ bubbleLabel?: string;
40
61
  }
41
62
  export type LuziaWidgetMode = 'bubble' | 'inline';
42
63
  export type LuziaWidgetTheme = 'light' | 'dark' | 'auto';
@@ -46,7 +67,7 @@ declare const BaseElement: typeof HTMLElement;
46
67
  * panel where the Visitor talks to the Agent, or with `mode="inline"` the chat
47
68
  * itself, embedded where the element sits. Attributes (see
48
69
  * {@link LuziaWidgetConfig}): `endpoint`, `contact`, `greeting`,
49
- * `suggested-questions` (JSON array of strings), `mode`, `theme` and `avatar`. Each is
70
+ * `suggested-questions` (JSON array of strings), `mode`, `theme`, `avatar` and `bubble-label`. Each is
50
71
  * also a property (`suggestedQuestions` takes an array).
51
72
  *
52
73
  * State lives in a {@link Conversation}; this element only renders its
@@ -70,6 +91,13 @@ export declare class LuziaWidgetElement extends BaseElement implements LuziaWidg
70
91
  set theme(value: LuziaWidgetTheme);
71
92
  get avatar(): string;
72
93
  set avatar(value: string);
94
+ get avatarSpeaking(): string;
95
+ set avatarSpeaking(value: string);
96
+ get avatarWaving(): string;
97
+ set avatarWaving(value: string);
98
+ /** The floating pill's text; without one (or with an empty one), `Ask luzIA about me`. */
99
+ get bubbleLabel(): string;
100
+ set bubbleLabel(value: string);
73
101
  static get observedAttributes(): string[];
74
102
  connectedCallback(): void;
75
103
  attributeChangedCallback(): void;
package/dist/element.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { failureKind, renderAlert } from './alert.js';
2
2
  import { Conversation } from './conversation.js';
3
+ import { renderWithLinks } from './links.js';
3
4
  import { persistConversation, storageKey } from './persistence.js';
4
5
  import { styles } from './styles.js';
5
6
  /** Tag name the widget is registered under. */
@@ -33,8 +34,28 @@ function lookUpOwner(endpoint) {
33
34
  })
34
35
  .catch(() => null);
35
36
  }
36
- /** How long the speech bubble stays after the mouse leaves it. */
37
- const SPEECH_AFTER_HOVER_MS = 2_000;
37
+ /**
38
+ * luzIA peeks out of the closed launcher for 4 seconds (half a second rising, 3 waving, half a
39
+ * second ducking back down), once every 25 seconds (counted from one hello to the next).
40
+ */
41
+ const PEEK_FOR_MS = 4_000;
42
+ const PEEK_EVERY_MS = 25_000;
43
+ /** luzIA thinking, in her reply before it starts (decorative: the log is busy meanwhile). */
44
+ const THINKING_DOTS = '<span class="dot" aria-hidden="true"></span>'.repeat(3);
45
+ /** How long luzIA glows as she says the greeting. */
46
+ const GREETING_GLOW_MS = 1_400;
47
+ function presenceLabel(presence) {
48
+ const spanish = (document.documentElement.lang || navigator.language || 'en').toLowerCase().startsWith('es');
49
+ const labels = {
50
+ idle: ['En línea', 'Online'],
51
+ listening: ['Escuchando…', 'Listening…'],
52
+ thinking: ['Pensando…', 'Thinking…'],
53
+ replying: ['Respondiendo…', 'Replying…'],
54
+ };
55
+ return labels[presence][spanish ? 0 : 1];
56
+ }
57
+ /** The floating pill's text when the Owner sets none (or an empty one). */
58
+ const DEFAULT_BUBBLE_LABEL = 'Ask luzIA about me';
38
59
  /** luzIA's own icon, in the accent colours so it follows the Owner's theme. */
39
60
  const LUZIA_ICON = `<svg viewBox="0 0 32 32" width="100%" height="100%" focusable="false">
40
61
  <circle cx="16" cy="16" r="16" fill="var(--luzia-accent-contrast)" />
@@ -43,13 +64,24 @@ const LUZIA_ICON = `<svg viewBox="0 0 32 32" width="100%" height="100%" focusabl
43
64
  const TEMPLATE = `
44
65
  <section class="panel" role="dialog" aria-label="luzIA" hidden>
45
66
  <header class="header">
67
+ <span class="header-icon" aria-hidden="true">${LUZIA_ICON}</span>
46
68
  <h2 class="title">luzIA</h2>
47
- <button type="button" class="new-conversation">New conversation</button>
48
- <button type="button" class="close" aria-label="Close luzIA chat">&times;</button>
69
+ <button type="button" class="new-conversation" aria-label="Restart conversation">
70
+ <svg viewBox="0 0 24 24" width="20" height="20" focusable="false" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
71
+ <path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" />
72
+ <path d="M3 3v5h5" />
73
+ </svg>
74
+ <span class="tooltip" aria-hidden="true">Restart conversation</span>
75
+ </button>
76
+ <button type="button" class="close" aria-label="Close luzIA chat">
77
+ <svg viewBox="0 0 24 24" width="20" height="20" focusable="false" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round">
78
+ <path d="M18 6 6 18M6 6l12 12" />
79
+ </svg>
80
+ </button>
49
81
  </header>
50
- <div class="stage">
51
- <span class="avatar" aria-hidden="true"></span>
52
- <p class="speech" aria-hidden="true" hidden><span class="speech-text"></span></p>
82
+ <div class="stage" aria-hidden="true">
83
+ <span class="avatar"></span>
84
+ <span class="presence"><span class="presence-dot"></span><span class="presence-text"></span></span>
53
85
  </div>
54
86
  <div class="body">
55
87
  <p class="greeting" hidden></p>
@@ -60,11 +92,17 @@ const TEMPLATE = `
60
92
  <p class="alert" role="alert" hidden></p>
61
93
  <form class="composer">
62
94
  <textarea name="message" rows="2" aria-label="Message luzIA" placeholder="Ask a question…"></textarea>
63
- <button type="submit">Send</button>
95
+ <button type="submit" class="send" aria-label="Send">
96
+ <svg viewBox="0 0 24 24" width="18" height="18" focusable="false" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2.25" stroke-linecap="round" stroke-linejoin="round">
97
+ <path d="M12 19V5M5 12l7-7 7 7" />
98
+ </svg>
99
+ </button>
64
100
  </form>
65
101
  </section>
66
- <button type="button" class="bubble" aria-label="luzIA chat" aria-expanded="false">
67
- <span aria-hidden="true">luzIA</span>
102
+ <button type="button" class="bubble" aria-label="${DEFAULT_BUBBLE_LABEL}" aria-expanded="false">
103
+ <span class="peek" aria-hidden="true"></span>
104
+ <span class="bubble-icon" aria-hidden="true">${LUZIA_ICON}</span>
105
+ <span class="bubble-label" aria-hidden="true"><span class="bubble-text">${DEFAULT_BUBBLE_LABEL}</span></span>
68
106
  </button>
69
107
  `;
70
108
  /**
@@ -72,7 +110,7 @@ const TEMPLATE = `
72
110
  * panel where the Visitor talks to the Agent, or with `mode="inline"` the chat
73
111
  * itself, embedded where the element sits. Attributes (see
74
112
  * {@link LuziaWidgetConfig}): `endpoint`, `contact`, `greeting`,
75
- * `suggested-questions` (JSON array of strings), `mode`, `theme` and `avatar`. Each is
113
+ * `suggested-questions` (JSON array of strings), `mode`, `theme`, `avatar` and `bubble-label`. Each is
76
114
  * also a property (`suggestedQuestions` takes an array).
77
115
  *
78
116
  * State lives in a {@link Conversation}; this element only renders its
@@ -126,8 +164,27 @@ export class LuziaWidgetElement extends BaseElement {
126
164
  set avatar(value) {
127
165
  this.setAttribute('avatar', value);
128
166
  }
167
+ get avatarSpeaking() {
168
+ return this.getAttribute('avatar-speaking') ?? '';
169
+ }
170
+ set avatarSpeaking(value) {
171
+ this.setAttribute('avatar-speaking', value);
172
+ }
173
+ get avatarWaving() {
174
+ return this.getAttribute('avatar-waving') ?? '';
175
+ }
176
+ set avatarWaving(value) {
177
+ this.setAttribute('avatar-waving', value);
178
+ }
179
+ /** The floating pill's text; without one (or with an empty one), `Ask luzIA about me`. */
180
+ get bubbleLabel() {
181
+ return this.getAttribute('bubble-label')?.trim() || DEFAULT_BUBBLE_LABEL;
182
+ }
183
+ set bubbleLabel(value) {
184
+ this.setAttribute('bubble-label', value);
185
+ }
129
186
  static get observedAttributes() {
130
- return ['greeting', 'suggested-questions', 'mode', 'avatar'];
187
+ return ['greeting', 'suggested-questions', 'mode', 'avatar', 'avatar-speaking', 'avatar-waving', 'bubble-label'];
131
188
  }
132
189
  connectedCallback() {
133
190
  if (this.#view)
@@ -176,24 +233,21 @@ class View {
176
233
  #greeting;
177
234
  #avatar;
178
235
  #stage;
179
- #speech;
180
- #speechText;
181
- /**
182
- * What luzIA is "saying" in the speech bubble, kept out of the history until
183
- * it ends: a reply (its index in the conversation) or the greeting.
184
- */
185
- #speaking = null;
236
+ #presence;
237
+ /** luzIA's picture peeking out of the closed launcher (see {@link View.#peek}). */
238
+ #peeker;
239
+ #peekTimer;
240
+ #peeks = 0;
186
241
  #greetingText = '';
242
+ /** Whether luzIA has said this conversation's greeting (it shows once said, or over a restored conversation). */
243
+ #greetingSaid = false;
244
+ /** luzIA glows for a moment as she says the greeting. */
245
+ #greetingGlow;
187
246
  #host;
188
247
  /** Stops following the visible area of the screen (see {@link View.#followViewport}). */
189
248
  #unfollowViewport = null;
190
- /** luzIA greets once per conversation: when the chat first shows, and again after "New conversation". */
249
+ /** luzIA greets once per conversation: when the chat first shows, and again after "Restart conversation". */
191
250
  #greeted = false;
192
- #speechTimer;
193
- /** While the mouse is over the bubble, it stays (the Visitor may be reading or scrolling it). */
194
- #speechHovered = false;
195
- /** Once the mouse has been over the bubble and left, it goes soon after (the Visitor has seen it). */
196
- #speechVisited = false;
197
251
  #messages;
198
252
  #announcer;
199
253
  #suggestions;
@@ -204,6 +258,8 @@ class View {
204
258
  #conversation;
205
259
  #contact;
206
260
  #mode = 'bubble';
261
+ /** The text each message was last rendered from, so streaming only re-renders what changed. */
262
+ #rendered = new WeakMap();
207
263
  #wasStreaming = false;
208
264
  #owner;
209
265
  #ownerName = null;
@@ -219,19 +275,8 @@ class View {
219
275
  this.#greeting = root.querySelector('.greeting');
220
276
  this.#avatar = root.querySelector('.avatar');
221
277
  this.#stage = root.querySelector('.stage');
222
- this.#speech = root.querySelector('.speech');
223
- this.#speechText = root.querySelector('.speech-text');
224
- this.#speech.addEventListener('mouseenter', () => {
225
- this.#speechHovered = true;
226
- this.#speechVisited = true;
227
- clearTimeout(this.#speechTimer);
228
- this.#speechTimer = undefined;
229
- this.#speech.classList.remove('leaving');
230
- });
231
- this.#speech.addEventListener('mouseleave', () => {
232
- this.#speechHovered = false;
233
- this.#render(this.#conversation.state);
234
- });
278
+ this.#presence = root.querySelector('.presence-text');
279
+ this.#peeker = root.querySelector('.peek');
235
280
  this.#messages = root.querySelector('.messages');
236
281
  this.#announcer = root.querySelector('.announcer');
237
282
  this.#suggestions = root.querySelector('.suggestions');
@@ -250,6 +295,7 @@ class View {
250
295
  root.querySelector('.new-conversation').addEventListener('click', () => {
251
296
  conversation.reset();
252
297
  this.#greeted = false;
298
+ this.#greetingSaid = false;
253
299
  this.#greet();
254
300
  this.#input.focus();
255
301
  });
@@ -257,18 +303,30 @@ class View {
257
303
  event.preventDefault();
258
304
  this.#submit();
259
305
  });
306
+ // While the Visitor types, luzIA listens.
307
+ this.#input.addEventListener('input', () => this.#updatePresence(this.#conversation.state));
260
308
  this.#input.addEventListener('keydown', (event) => {
261
309
  if (event.key === 'Enter' && !event.shiftKey && !event.isComposing) {
262
310
  event.preventDefault();
263
311
  this.#submit();
264
312
  }
265
313
  });
314
+ const peeking = setInterval(() => {
315
+ if (this.#host.isConnected)
316
+ this.#peek();
317
+ else
318
+ clearInterval(peeking);
319
+ }, PEEK_EVERY_MS);
266
320
  conversation.subscribe((state) => this.#render(state));
267
321
  this.#render(conversation.state);
268
322
  }
269
323
  /** Applies the Owner's options; called on mount and whenever one changes. */
270
324
  configure(options) {
271
- this.#showAvatar(options.avatar.trim());
325
+ this.#showAvatar(options.avatar.trim(), options.avatarSpeaking.trim());
326
+ this.#showWaving(options.avatarWaving.trim());
327
+ // The visible text is also the button's name, so voice control users can say what they see.
328
+ this.#bubble.setAttribute('aria-label', options.bubbleLabel);
329
+ this.#bubble.querySelector('.bubble-text').textContent = options.bubbleLabel;
272
330
  this.#greetingText = this.#ownerName && this.#owner() ? defaultGreeting(this.#ownerName) : options.greeting.trim();
273
331
  this.#greeting.textContent = this.#greetingText;
274
332
  this.#suggestions.replaceChildren(...options.suggestedQuestions.map((question) => {
@@ -306,7 +364,33 @@ class View {
306
364
  this.#setOpen(open);
307
365
  (open ? this.#input : this.#bubble).focus();
308
366
  }
367
+ /**
368
+ * While the chat is closed, luzIA peeks out of the launcher now and then:
369
+ * she rises above its text, waves, and ducks back down. Only with the
370
+ * Owner's picture (the talking version if there is one), only on screen,
371
+ * and never for Visitors who ask for reduced motion. Decorative.
372
+ */
373
+ #peek() {
374
+ const open = !this.#panel.hidden;
375
+ const reduced = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
376
+ // Only with a hello animation that loads: otherwise nothing peeks out.
377
+ const waving = this.#peeker.firstElementChild;
378
+ const animation = waving?.dataset.waves;
379
+ if (this.#mode !== 'bubble' || open || document.hidden || reduced || !waving || !animation || waving.hasAttribute('data-broken'))
380
+ return;
381
+ // The animation plays from its beginning each time. Browsers share one clock between
382
+ // copies of the same image, so each peek gets its own address (the fragment is never fetched).
383
+ waving.src = `${animation.split('#')[0]}#peek-${++this.#peeks}`;
384
+ this.#bubble.toggleAttribute('data-peeking', true);
385
+ clearTimeout(this.#peekTimer);
386
+ this.#peekTimer = setTimeout(() => this.#bubble.removeAttribute('data-peeking'), PEEK_FOR_MS);
387
+ }
309
388
  #setOpen(open) {
389
+ // Opening the chat cuts a peek short: luzIA is right there now.
390
+ if (open) {
391
+ clearTimeout(this.#peekTimer);
392
+ this.#bubble.removeAttribute('data-peeking');
393
+ }
310
394
  this.#panel.hidden = !open;
311
395
  this.#bubble.setAttribute('aria-expanded', String(open));
312
396
  if (open)
@@ -317,7 +401,7 @@ class View {
317
401
  /**
318
402
  * On a phone the chat fills the screen, but an on-screen keyboard covers its
319
403
  * lower part and the browser scrolls the page under it, pushing luzIA's
320
- * picture and speech bubble out of view. While the chat is open it follows
404
+ * picture and the latest messages out of view. While the chat is open it follows
321
405
  * the part of the screen that is actually visible (`visualViewport`), and
322
406
  * marks the host with `data-keyboard` so the picture gets smaller.
323
407
  */
@@ -348,7 +432,7 @@ class View {
348
432
  this.#host.style.removeProperty('--luzia-viewport-top');
349
433
  this.#host.removeAttribute('data-keyboard');
350
434
  }
351
- /** luzIA says the greeting in the bubble when a conversation starts empty (never over a restored one). */
435
+ /** luzIA says the greeting when a conversation starts empty (a restored one already has it at the top). */
352
436
  #greet() {
353
437
  if (this.#greeted || this.#panel.hidden)
354
438
  return;
@@ -356,9 +440,13 @@ class View {
356
440
  const say = () => {
357
441
  if (!this.#greetingText || this.#panel.hidden || this.#conversation.state.messages.length > 0)
358
442
  return;
359
- this.#stopSpeaking();
360
- this.#speaking = 'greeting';
361
- this.#showSpeech(this.#greetingText);
443
+ this.#greetingSaid = true;
444
+ clearTimeout(this.#greetingGlow);
445
+ this.#stage.toggleAttribute('data-greeting', true);
446
+ this.#greetingGlow = setTimeout(() => {
447
+ this.#stage.removeAttribute('data-greeting');
448
+ this.#updatePresence(this.#conversation.state);
449
+ }, GREETING_GLOW_MS);
362
450
  this.#render(this.#conversation.state);
363
451
  };
364
452
  const lookup = this.#owner();
@@ -375,22 +463,55 @@ class View {
375
463
  say();
376
464
  });
377
465
  }
378
- /** The Owner's image at the top of the chat, or luzIA's icon. Decorative: the title already says "luzIA". */
379
- #showAvatar(url) {
380
- const current = this.#avatar.querySelector('img')?.getAttribute('src') ?? null;
381
- if (url && current === url)
466
+ /**
467
+ * The Owner's image at the top of the chat, or luzIA's icon, and the
468
+ * animated version shown while she talks, if any. Both stay loaded, stacked
469
+ * in place, so switching between them never flickers. Decorative: the title
470
+ * already says "luzIA".
471
+ */
472
+ #showAvatar(url, speakingUrl) {
473
+ const key = `${url}\n${url ? speakingUrl : ''}`;
474
+ if (this.#avatar.dataset.shown === key)
382
475
  return;
383
- if (!url && current === null && this.#avatar.firstChild)
476
+ this.#avatar.dataset.shown = key;
477
+ if (!url) {
478
+ this.#avatar.innerHTML = LUZIA_ICON;
384
479
  return;
385
- if (url) {
386
- const image = document.createElement('img');
387
- image.src = url;
388
- image.alt = '';
389
- this.#avatar.replaceChildren(image);
390
480
  }
391
- else {
392
- this.#avatar.innerHTML = LUZIA_ICON;
481
+ const image = (src, className) => {
482
+ const img = document.createElement('img');
483
+ img.src = src;
484
+ img.alt = '';
485
+ img.className = className;
486
+ return img;
487
+ };
488
+ this.#avatar.replaceChildren(image(url, 'still'), ...(speakingUrl ? [image(speakingUrl, 'speaking')] : []));
489
+ }
490
+ /**
491
+ * The Owner's hello animation, which luzIA peeks out of the closed launcher
492
+ * with (see {@link View.#peek}). None, or one that fails to load, means she
493
+ * never peeks out: nothing shows in its place.
494
+ */
495
+ #showWaving(url) {
496
+ const current = this.#peeker.firstElementChild?.dataset.waves ?? '';
497
+ if (current === url)
498
+ return;
499
+ clearTimeout(this.#peekTimer);
500
+ this.#bubble.removeAttribute('data-peeking');
501
+ if (!url) {
502
+ this.#peeker.replaceChildren();
503
+ return;
393
504
  }
505
+ const image = document.createElement('img');
506
+ image.alt = '';
507
+ image.className = 'peek-image';
508
+ image.dataset.waves = url;
509
+ image.addEventListener('error', () => {
510
+ image.dataset.broken = '';
511
+ this.#bubble.removeAttribute('data-peeking');
512
+ });
513
+ image.src = url;
514
+ this.#peeker.replaceChildren(image);
394
515
  }
395
516
  /** Sends what the Visitor typed; focus stays in (or returns to) the message box, since Send disables while streaming. */
396
517
  #submit() {
@@ -416,67 +537,24 @@ class View {
416
537
  this.#announcer.textContent = state.status === 'idle' && last?.role === 'assistant' ? last.content : '';
417
538
  }
418
539
  /**
419
- * Like a video call, luzIA "says" each reply in a bubble over the picture as
420
- * it streams in; it stays a few seconds after it ends, then fades and the
421
- * reply takes its place in the history. A new question, an error or a reset
422
- * moves it to the history at once; a restored conversation never uses it.
423
- * The bubble is decorative: the status region announces each reply.
540
+ * Like someone on a video call, luzIA shows she is there: she leans in while
541
+ * the Visitor types, thinks before a reply (dots in her message), and glows
542
+ * while it streams in. Her state is named beside the picture. All of it is
543
+ * decorative: the status region announces each finished reply.
424
544
  */
425
- #updateSpeech(state, streaming) {
426
- const last = state.messages.length - 1;
427
- const lastIsReply = state.messages[last]?.role === 'assistant';
428
- if (streaming && lastIsReply) {
429
- if (this.#speaking !== last)
430
- this.#stopSpeaking();
431
- this.#speaking = last;
432
- clearTimeout(this.#speechTimer);
433
- this.#showSpeech(state.messages[last].content);
434
- }
435
- else if (this.#speaking !== null) {
436
- const greeting = this.#speaking === 'greeting';
437
- // The greeting stays until the Visitor says something; a reply until another one starts.
438
- const stillSaying = greeting ? state.messages.length === 0 : this.#speaking === last && state.status === 'idle';
439
- if (!stillSaying)
440
- this.#stopSpeaking();
441
- else if (this.#speechTimer === undefined && !this.#speechHovered) {
442
- const text = greeting ? this.#greetingText : state.messages[last].content;
443
- const delay = this.#speechVisited ? SPEECH_AFTER_HOVER_MS : Math.min(10_000, 4_000 + text.length * 40);
444
- this.#speechTimer = setTimeout(() => this.#fadeSpeech(), delay);
445
- }
446
- }
447
- // What is being said stays out of the history until the bubble is gone.
448
- [...this.#messages.children].forEach((item, index) => {
449
- item.hidden = index === this.#speaking;
450
- });
451
- this.#greeting.hidden = !this.#greetingText || this.#speaking === 'greeting';
452
- }
453
- #showSpeech(text) {
454
- this.#speech.classList.remove('leaving');
455
- // Nothing to say yet (the reply hasn't started): no empty bubble.
456
- this.#speech.hidden = text === '';
457
- this.#stage.toggleAttribute('data-saying', !this.#speech.hidden);
458
- // The bubble shows the reply from its beginning; the Visitor scrolls to read the rest.
459
- if (this.#speechText.textContent !== text)
460
- this.#speechText.textContent = text;
461
- }
462
- #fadeSpeech() {
463
- this.#speech.classList.add('leaving');
464
- this.#speechTimer = setTimeout(() => {
465
- this.#stopSpeaking();
466
- this.#render(this.#conversation.state);
467
- }, 400);
468
- }
469
- #stopSpeaking() {
470
- clearTimeout(this.#speechTimer);
471
- this.#speechTimer = undefined;
472
- this.#speaking = null;
473
- this.#speech.hidden = true;
474
- this.#speechHovered = false;
475
- this.#speechVisited = false;
476
- this.#stage.removeAttribute('data-saying');
477
- this.#speechText.scrollTop = 0;
478
- this.#speech.classList.remove('leaving');
479
- this.#speechText.textContent = '';
545
+ #updatePresence(state) {
546
+ const last = state.messages.at(-1);
547
+ let presence = 'idle';
548
+ if (state.status === 'streaming')
549
+ presence = last?.role === 'assistant' && last.content ? 'replying' : 'thinking';
550
+ else if (this.#input.value.trim() && !this.#input.disabled)
551
+ presence = 'listening';
552
+ this.#stage.dataset.presence = presence;
553
+ // Her mouth moves while words come out: the greeting, or a reply as it streams in (not while she thinks).
554
+ this.#stage.toggleAttribute('data-talking', presence === 'replying' || this.#stage.hasAttribute('data-greeting'));
555
+ const label = presenceLabel(presence);
556
+ if (this.#presence.textContent !== label)
557
+ this.#presence.textContent = label;
480
558
  }
481
559
  #render(state) {
482
560
  // Reuse existing message nodes so a streaming reply only updates its text.
@@ -492,15 +570,30 @@ class View {
492
570
  else
493
571
  this.#messages.append(item);
494
572
  }
495
- if (item.textContent !== message.content)
496
- item.textContent = message.content;
573
+ // A reply that hasn't started yet: luzIA is thinking.
574
+ item.toggleAttribute('data-thinking', message.role === 'assistant' && message.content === '');
575
+ if (this.#rendered.get(item) !== message.content) {
576
+ this.#rendered.set(item, message.content);
577
+ // luzIA's links are clickable; what the Visitor typed stays plain text.
578
+ if (message.role === 'assistant' && message.content === '')
579
+ item.innerHTML = THINKING_DOTS;
580
+ else if (message.role === 'assistant')
581
+ renderWithLinks(item, message.content);
582
+ else
583
+ item.textContent = message.content;
584
+ }
497
585
  });
498
586
  while (this.#messages.children.length > state.messages.length)
499
587
  this.#messages.lastElementChild?.remove();
500
588
  // Suggestions only help the Visitor start; once the conversation has begun they go away.
501
589
  this.#suggestions.hidden = state.messages.length > 0 || this.#suggestions.children.length === 0;
502
590
  const streaming = state.status === 'streaming';
503
- this.#updateSpeech(state, streaming);
591
+ this.#greeting.hidden = !this.#greetingText || (state.messages.length === 0 && !this.#greetingSaid);
592
+ // The greeting's glow ends as soon as the Visitor speaks.
593
+ if (state.messages.length > 0 && this.#stage.hasAttribute('data-greeting')) {
594
+ clearTimeout(this.#greetingGlow);
595
+ this.#stage.removeAttribute('data-greeting');
596
+ }
504
597
  this.#body.scrollTop = this.#body.scrollHeight;
505
598
  // Like a video call: luzIA's avatar lights up while the reply comes in.
506
599
  this.#stage.toggleAttribute('data-speaking', streaming);
@@ -515,6 +608,7 @@ class View {
515
608
  // A message that got no reply is given back for editing or resending (see ConversationState.unsent).
516
609
  if (state.unsent !== null && !conversationOver && !this.#input.value)
517
610
  this.#input.value = state.unsent;
611
+ this.#updatePresence(state);
518
612
  }
519
613
  }
520
614
  //# sourceMappingURL=element.js.map