@convai/web-sdk 1.8.0-beta.6 → 1.8.0-beta.8

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.
Files changed (99) hide show
  1. package/README.md +303 -1
  2. package/dist/core/ConvaiClient.d.ts +59 -2
  3. package/dist/core/ConvaiClient.d.ts.map +1 -1
  4. package/dist/core/ConvaiClient.js +457 -6
  5. package/dist/core/ConvaiClient.js.map +1 -1
  6. package/dist/core/MessageHandler.d.ts +11 -0
  7. package/dist/core/MessageHandler.d.ts.map +1 -1
  8. package/dist/core/MessageHandler.js +325 -30
  9. package/dist/core/MessageHandler.js.map +1 -1
  10. package/dist/core/SSESession.js +2 -2
  11. package/dist/core/SSESession.js.map +1 -1
  12. package/dist/core/connectRequest.d.ts +38 -3
  13. package/dist/core/connectRequest.d.ts.map +1 -1
  14. package/dist/core/connectRequest.js +57 -11
  15. package/dist/core/connectRequest.js.map +1 -1
  16. package/dist/core/index.d.ts +5 -1
  17. package/dist/core/index.d.ts.map +1 -1
  18. package/dist/core/index.js +8 -1
  19. package/dist/core/index.js.map +1 -1
  20. package/dist/core/logicalTurn.d.ts +4 -1
  21. package/dist/core/logicalTurn.d.ts.map +1 -1
  22. package/dist/core/logicalTurn.js +35 -4
  23. package/dist/core/logicalTurn.js.map +1 -1
  24. package/dist/core/modelOutput.d.ts +40 -0
  25. package/dist/core/modelOutput.d.ts.map +1 -0
  26. package/dist/core/modelOutput.js +187 -0
  27. package/dist/core/modelOutput.js.map +1 -0
  28. package/dist/core/publishedChat.d.ts +9 -0
  29. package/dist/core/publishedChat.d.ts.map +1 -0
  30. package/dist/core/publishedChat.js +13 -0
  31. package/dist/core/publishedChat.js.map +1 -0
  32. package/dist/core/types.d.ts +225 -25
  33. package/dist/core/types.d.ts.map +1 -1
  34. package/dist/core/types.js.map +1 -1
  35. package/dist/embed/ConvaiChatElement.d.ts +103 -0
  36. package/dist/embed/ConvaiChatElement.d.ts.map +1 -0
  37. package/dist/embed/ConvaiChatElement.js +679 -0
  38. package/dist/embed/ConvaiChatElement.js.map +1 -0
  39. package/dist/embed/browser.d.ts +3 -0
  40. package/dist/embed/browser.d.ts.map +1 -0
  41. package/dist/embed/browser.js +5 -0
  42. package/dist/embed/browser.js.map +1 -0
  43. package/dist/embed/chat-embed-v1.js +200 -0
  44. package/dist/embed/chat-embed-v1.js.map +7 -0
  45. package/dist/embed/index.d.ts +9 -0
  46. package/dist/embed/index.d.ts.map +1 -0
  47. package/dist/embed/index.js +9 -0
  48. package/dist/embed/index.js.map +1 -0
  49. package/dist/embed/publicationApi.d.ts +23 -0
  50. package/dist/embed/publicationApi.d.ts.map +1 -0
  51. package/dist/embed/publicationApi.js +141 -0
  52. package/dist/embed/publicationApi.js.map +1 -0
  53. package/dist/react/components/ConvaiWidget.d.ts +11 -1
  54. package/dist/react/components/ConvaiWidget.d.ts.map +1 -1
  55. package/dist/react/components/ConvaiWidget.js +67 -11
  56. package/dist/react/components/ConvaiWidget.js.map +1 -1
  57. package/dist/react/components/rtc-widget/components/MarkdownRenderer.d.ts +7 -0
  58. package/dist/react/components/rtc-widget/components/MarkdownRenderer.d.ts.map +1 -1
  59. package/dist/react/components/rtc-widget/components/MarkdownRenderer.js +111 -17
  60. package/dist/react/components/rtc-widget/components/MarkdownRenderer.js.map +1 -1
  61. package/dist/react/components/rtc-widget/components/UserMessage.js +1 -1
  62. package/dist/react/components/rtc-widget/components/UserMessage.js.map +1 -1
  63. package/dist/react/components/rtc-widget/components/conviComponents/SettingsTray.d.ts +1 -1
  64. package/dist/react/components/rtc-widget/components/conviComponents/SettingsTray.d.ts.map +1 -1
  65. package/dist/react/components/rtc-widget/components/conviComponents/SettingsTray.js +2 -2
  66. package/dist/react/components/rtc-widget/components/conviComponents/SettingsTray.js.map +1 -1
  67. package/dist/react/hooks/useConvaiClient.d.ts +2 -0
  68. package/dist/react/hooks/useConvaiClient.d.ts.map +1 -1
  69. package/dist/react/hooks/useConvaiClient.js +8 -0
  70. package/dist/react/hooks/useConvaiClient.js.map +1 -1
  71. package/dist/react/index.d.ts +4 -1
  72. package/dist/react/index.d.ts.map +1 -1
  73. package/dist/react/index.js +8 -1
  74. package/dist/react/index.js.map +1 -1
  75. package/dist/utils/inlineMarkdown.d.ts +22 -0
  76. package/dist/utils/inlineMarkdown.d.ts.map +1 -1
  77. package/dist/utils/inlineMarkdown.js +84 -7
  78. package/dist/utils/inlineMarkdown.js.map +1 -1
  79. package/dist/vanilla/ConvaiWidget.d.ts.map +1 -1
  80. package/dist/vanilla/ConvaiWidget.js +290 -43
  81. package/dist/vanilla/ConvaiWidget.js.map +1 -1
  82. package/dist/vanilla/WidgetEventSubscriptions.d.ts +15 -0
  83. package/dist/vanilla/WidgetEventSubscriptions.d.ts.map +1 -0
  84. package/dist/vanilla/WidgetEventSubscriptions.js +37 -0
  85. package/dist/vanilla/WidgetEventSubscriptions.js.map +1 -0
  86. package/dist/vanilla/index.d.ts +3 -1
  87. package/dist/vanilla/index.d.ts.map +1 -1
  88. package/dist/vanilla/index.js +3 -1
  89. package/dist/vanilla/index.js.map +1 -1
  90. package/dist/vanilla/styles.d.ts.map +1 -1
  91. package/dist/vanilla/styles.js +12 -0
  92. package/dist/vanilla/styles.js.map +1 -1
  93. package/dist/vanilla/types.d.ts +8 -1
  94. package/dist/vanilla/types.d.ts.map +1 -1
  95. package/dist/vanilla/types.js.map +1 -1
  96. package/dist/version.d.ts +1 -1
  97. package/dist/version.js +1 -1
  98. package/dist/version.js.map +1 -1
  99. package/package.json +48 -13
@@ -0,0 +1,679 @@
1
+ import { ConvaiClient } from "../core/ConvaiClient.js";
2
+ import { loadPublishedChatSession, parsePublishedChatUrl, } from "./publicationApi.js";
3
+ const VISIBLE_MESSAGE_TYPES = new Set([
4
+ "user-llm-text",
5
+ "user-transcription",
6
+ "bot-llm-text",
7
+ "bot-output",
8
+ ]);
9
+ /** Placeholder session id the client reports before the server assigns one. */
10
+ const UNASSIGNED_SESSION_ID = "-1";
11
+ const COPY_ICON = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><rect x="9" y="9" width="11" height="11" rx="2"/><path d="M5 15V6a2 2 0 0 1 2-2h9"/></svg>`;
12
+ const RESET_ICON = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M3 12a9 9 0 1 0 3-6.7M3 4v5h5"/></svg>`;
13
+ const SEND_ICON = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>`;
14
+ /**
15
+ * Every colour, radius and font is a custom property so an embedder can retheme
16
+ * the widget from the host page without reaching into the shadow root. The
17
+ * defaults match the Convai playground chat.
18
+ */
19
+ const styles = `
20
+ :host {
21
+ --convai-chat-bg: #0F1113;
22
+ --convai-chat-surface: #181A1D;
23
+ --convai-chat-raised: #24272B;
24
+ --convai-chat-border: rgba(145, 158, 171, 0.20);
25
+ --convai-chat-text: #FFFFFF;
26
+ --convai-chat-muted: #919EAB;
27
+ --convai-chat-faint: #637381;
28
+ --convai-chat-accent: #00A76F;
29
+ --convai-chat-accent-strong: #5BE49B;
30
+ --convai-chat-on-accent: #FFFFFF;
31
+ --convai-chat-user-bubble: #BCDBC7;
32
+ --convai-chat-user-text: #0F1113;
33
+ --convai-chat-danger: #FFAC82;
34
+ /* The widget's own corner. Radii inside the chat — bubbles, the composer
35
+ pill, the circular avatars and send button — are part of its shape, not
36
+ its theme, and are fixed. */
37
+ --convai-chat-radius: 16px;
38
+ --convai-chat-font: "Public Sans", Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
39
+
40
+ color-scheme: dark;
41
+ display: block;
42
+ min-height: 560px;
43
+ width: 100%;
44
+ font-family: var(--convai-chat-font);
45
+ }
46
+ :host([full-window]) { min-height: 100dvh; }
47
+ * { box-sizing: border-box; }
48
+
49
+ .shell {
50
+ background: var(--convai-chat-bg);
51
+ border: 1px solid var(--convai-chat-border);
52
+ border-radius: var(--convai-chat-radius);
53
+ color: var(--convai-chat-text);
54
+ display: grid;
55
+ grid-template-rows: auto minmax(0, 1fr) auto;
56
+ height: 100%;
57
+ min-height: inherit;
58
+ overflow: hidden;
59
+ }
60
+ :host([full-window]) .shell { border: 0; border-radius: 0; }
61
+
62
+ header {
63
+ align-items: center;
64
+ background: var(--convai-chat-surface);
65
+ border-bottom: 1px solid var(--convai-chat-border);
66
+ display: flex;
67
+ gap: 12px;
68
+ padding: 12px 18px;
69
+ }
70
+ .avatar {
71
+ background: var(--convai-chat-accent);
72
+ border-radius: 50%;
73
+ flex: none;
74
+ height: 40px;
75
+ object-fit: cover;
76
+ width: 40px;
77
+ }
78
+ .avatar-fallback { align-items: center; color: var(--convai-chat-on-accent); display: inline-flex; font-size: 12px; font-weight: 600; justify-content: center; text-transform: uppercase; }
79
+ .identity { min-width: 0; }
80
+ h2 { font-size: 15px; font-weight: 600; line-height: 1.3; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
81
+ .status { color: var(--convai-chat-muted); font-size: 12px; margin: 2px 0 0; }
82
+ .header-actions { display: flex; gap: 2px; margin-left: auto; }
83
+ .icon-button {
84
+ align-items: center;
85
+ background: transparent;
86
+ border: 0;
87
+ border-radius: 8px;
88
+ color: var(--convai-chat-muted);
89
+ cursor: pointer;
90
+ display: inline-flex;
91
+ height: 34px;
92
+ justify-content: center;
93
+ padding: 0;
94
+ width: 34px;
95
+ }
96
+ .icon-button:hover:not(:disabled) { background: var(--convai-chat-raised); color: var(--convai-chat-text); }
97
+ .icon-button:disabled { cursor: default; opacity: .4; }
98
+ .icon-button svg { height: 17px; width: 17px; }
99
+
100
+ .messages { display: flex; flex-direction: column; gap: 14px; overflow-y: auto; padding: 20px 18px; }
101
+
102
+ .intro { align-items: center; align-self: center; display: flex; flex-direction: column; gap: 8px; margin: auto 0; max-width: 44ch; text-align: center; }
103
+ .intro .avatar { height: 56px; width: 56px; }
104
+ .intro-name { font-size: 16px; font-weight: 600; }
105
+ .intro-description { color: var(--convai-chat-muted); font-size: 13px; margin: 0; }
106
+
107
+ .row { align-items: flex-end; display: flex; gap: 10px; max-width: 82%; }
108
+ .row.user { align-self: flex-end; flex-direction: row-reverse; }
109
+ .row.character { align-self: flex-start; }
110
+ .row .avatar { height: 26px; width: 26px; }
111
+ .bubble { border-radius: 14px; line-height: 1.5; overflow-wrap: anywhere; padding: 10px 14px; white-space: pre-wrap; }
112
+ .row.user .bubble { background: var(--convai-chat-user-bubble); border-bottom-right-radius: 4px; color: var(--convai-chat-user-text); }
113
+ .row.character .bubble { background: var(--convai-chat-raised); border-bottom-left-radius: 4px; }
114
+
115
+ .typing { display: inline-flex; gap: 4px; padding: 13px 14px; }
116
+ .typing span { animation: convai-blink 1.2s infinite; background: var(--convai-chat-muted); border-radius: 50%; height: 6px; width: 6px; }
117
+ .typing span:nth-child(2) { animation-delay: .2s; }
118
+ .typing span:nth-child(3) { animation-delay: .4s; }
119
+ @keyframes convai-blink { 0%, 80%, 100% { opacity: .25 } 40% { opacity: 1 } }
120
+
121
+ .error { color: var(--convai-chat-danger); font-size: 13px; margin: 0; padding: 0 18px 10px; }
122
+ .retry { align-self: center; margin-top: 12px; }
123
+
124
+ footer { background: var(--convai-chat-surface); border-top: 1px solid var(--convai-chat-border); display: grid; gap: 8px; padding: 12px 18px 10px; }
125
+ form { align-items: center; background: var(--convai-chat-bg); border: 1px solid var(--convai-chat-border); border-radius: 999px; display: flex; gap: 8px; padding: 4px 4px 4px 16px; }
126
+ input { background: transparent; border: 0; color: var(--convai-chat-text); flex: 1; font: inherit; min-width: 0; outline: none; padding: 9px 0; }
127
+ /* The input drops its own outline, so the pill carries keyboard focus. */
128
+ form:focus-within { border-color: var(--convai-chat-accent); box-shadow: 0 0 0 2px var(--convai-chat-accent-strong); }
129
+ input::placeholder { color: var(--convai-chat-faint); }
130
+ button { background: var(--convai-chat-accent); border: 0; border-radius: 999px; color: var(--convai-chat-on-accent); cursor: pointer; font: inherit; font-weight: 600; padding: 9px 16px; }
131
+ button:disabled { cursor: default; opacity: .45; }
132
+ .send { align-items: center; border-radius: 50%; display: inline-flex; height: 38px; justify-content: center; padding: 0; width: 38px; }
133
+ .send svg { height: 17px; width: 17px; }
134
+ .interrupt { background: var(--convai-chat-raised); color: var(--convai-chat-text); padding: 8px 14px; }
135
+
136
+ .meta { align-items: center; color: var(--convai-chat-faint); display: flex; font-size: 11px; gap: 8px; justify-content: space-between; }
137
+ .session { background: transparent; border: 0; border-radius: 4px; color: var(--convai-chat-faint); cursor: pointer; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; font-weight: 400; padding: 2px 6px; }
138
+ .session:hover { background: var(--convai-chat-raised); color: var(--convai-chat-muted); }
139
+ .session[hidden] { display: none; }
140
+
141
+ @media (max-width: 560px) {
142
+ :host { min-height: 100dvh; }
143
+ .shell { border: 0; border-radius: 0; }
144
+ .messages { padding: 16px; }
145
+ .row { max-width: 90%; }
146
+ }
147
+ @media (prefers-reduced-motion: reduce) {
148
+ .typing span { animation: none; }
149
+ }
150
+ `;
151
+ function errorText(error) {
152
+ return error instanceof Error && error.message
153
+ ? error.message
154
+ : "Published chat is temporarily unavailable";
155
+ }
156
+ /** Short, copy-friendly form for a long opaque session id. */
157
+ function shortSessionId(sessionId) {
158
+ return sessionId.length > 12
159
+ ? `${sessionId.slice(0, 4)}…${sessionId.slice(-4)}`
160
+ : sessionId;
161
+ }
162
+ export class ConvaiChatElement extends HTMLElement {
163
+ constructor() {
164
+ super(...arguments);
165
+ this.client = null;
166
+ this.controller = null;
167
+ this.unsubs = [];
168
+ this.mounted = false;
169
+ this.ready = false;
170
+ this.messages = [];
171
+ this.descriptor = null;
172
+ this.failureReported = false;
173
+ /** A session that could still produce a reply: set when the client is wired
174
+ * up, cleared when it ends. Not a wait flag — see `isAwaitingReply()`. */
175
+ this.sessionLive = false;
176
+ this.sessionId = null;
177
+ this.retryVisible = false;
178
+ }
179
+ connectedCallback() {
180
+ if (this.mounted)
181
+ return;
182
+ this.mounted = true;
183
+ this.renderShell();
184
+ void this.start();
185
+ }
186
+ disconnectedCallback() {
187
+ this.mounted = false;
188
+ this.controller?.abort();
189
+ this.controller = null;
190
+ void this.cleanupClient();
191
+ }
192
+ requireAttribute(name) {
193
+ const value = this.getAttribute(name)?.trim();
194
+ if (!value)
195
+ throw new Error(`${name} is required`);
196
+ return value;
197
+ }
198
+ renderShell() {
199
+ const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
200
+ root.innerHTML = `
201
+ <style>${styles}</style>
202
+ <section class="shell" aria-label="Convai character chat">
203
+ <header>
204
+ <img class="avatar" alt="" hidden />
205
+ <div class="identity">
206
+ <h2>Character chat</h2>
207
+ <p class="status" role="status">Preparing chat…</p>
208
+ </div>
209
+ <div class="header-actions">
210
+ <button class="icon-button copy-chat" type="button" title="Copy chat" aria-label="Copy chat" disabled>${COPY_ICON}</button>
211
+ <button class="icon-button reset-session" type="button" title="Reset session" aria-label="Reset session" disabled>${RESET_ICON}</button>
212
+ </div>
213
+ </header>
214
+ <div class="messages" aria-live="polite"></div>
215
+ <p class="error" role="alert" hidden></p>
216
+ <footer>
217
+ <form>
218
+ <input aria-label="Chat message" maxlength="4000" placeholder="Preparing chat…" disabled />
219
+ <button class="interrupt" type="button" hidden>Stop</button>
220
+ <button class="send" type="submit" aria-label="Send" disabled>${SEND_ICON}</button>
221
+ </form>
222
+ <div class="meta">
223
+ <span class="powered">Powered by Convai</span>
224
+ <button class="session" type="button" title="Copy session ID" hidden></button>
225
+ </div>
226
+ </footer>
227
+ </section>
228
+ `;
229
+ const input = root.querySelector("input");
230
+ const sendButton = root.querySelector(".send");
231
+ if (input) {
232
+ input.addEventListener("input", () => {
233
+ if (sendButton) {
234
+ sendButton.disabled = !this.client?.state.isConnected || !input.value.trim();
235
+ }
236
+ });
237
+ }
238
+ root.querySelector("form")?.addEventListener("submit", (event) => {
239
+ event.preventDefault();
240
+ if (!input)
241
+ return;
242
+ const text = input.value.trim();
243
+ if (!text || !this.client?.state.isConnected)
244
+ return;
245
+ this.client.sendUserTextMessage(text);
246
+ input.value = "";
247
+ this.updateControls(true);
248
+ });
249
+ root.querySelector(".interrupt")?.addEventListener("click", () => {
250
+ this.client?.sendInterruptMessage();
251
+ this.setResponding(false);
252
+ this.finalizeOpenTurn();
253
+ });
254
+ root.querySelector(".copy-chat")?.addEventListener("click", () => {
255
+ void this.copyTranscript();
256
+ });
257
+ root.querySelector(".reset-session")?.addEventListener("click", () => {
258
+ this.resetSession();
259
+ });
260
+ root.querySelector(".session")?.addEventListener("click", () => {
261
+ if (this.sessionId)
262
+ void this.writeToClipboard(this.sessionId);
263
+ });
264
+ this.renderDescriptor();
265
+ this.renderMessages();
266
+ }
267
+ async start() {
268
+ this.controller?.abort();
269
+ const controller = new AbortController();
270
+ this.controller = controller;
271
+ // Retire the previous session before awaiting anything. `cleanupClient()`
272
+ // waits on the room teardown, and until that resolves the visitor would
273
+ // otherwise keep a live composer, a Stop control and a typing row driven
274
+ // by a client that has already gone.
275
+ this.ready = false;
276
+ this.failureReported = false;
277
+ this.messages = [];
278
+ this.sessionLive = false;
279
+ this.setResponding(false);
280
+ this.retryVisible = false;
281
+ // A reused element may now point at a different publication. Drop the old
282
+ // descriptor so the header falls back to the host's seed (or the generic
283
+ // title) instead of showing the previous character until the fetch lands.
284
+ this.descriptor = null;
285
+ this.setSessionId(null);
286
+ this.renderDescriptor();
287
+ this.renderMessages();
288
+ this.setError("");
289
+ this.setStatus("Preparing chat…");
290
+ this.updateControls(false);
291
+ await this.cleanupClient();
292
+ if (controller.signal.aborted || this.controller !== controller || !this.mounted)
293
+ return;
294
+ try {
295
+ const publicationId = this.requireAttribute("publication-id");
296
+ const publicationApiUrl = this.getAttribute("publication-api-url")?.trim();
297
+ const coreUrlValue = this.getAttribute("core-url")?.trim();
298
+ const coreUrl = coreUrlValue
299
+ ? parsePublishedChatUrl(coreUrlValue, "core-url").toString().replace(/\/$/, "")
300
+ : undefined;
301
+ const { descriptor, launchToken } = await loadPublishedChatSession(publicationId, publicationApiUrl || undefined, fetch, controller.signal);
302
+ if (controller.signal.aborted || this.controller !== controller || !this.mounted)
303
+ return;
304
+ this.descriptor = descriptor;
305
+ this.renderDescriptor();
306
+ this.renderMessages();
307
+ const client = new ConvaiClient();
308
+ this.client = client;
309
+ this.unsubs = [
310
+ client.on("messagesChange", (messages) => {
311
+ this.messages = [...messages];
312
+ this.renderMessages();
313
+ }),
314
+ client.on("stateChange", (state) => {
315
+ this.setResponding(state.isThinking || state.isSpeaking);
316
+ }),
317
+ client.on("characterSessionId", (sessionId) => {
318
+ this.setSessionId(sessionId);
319
+ }),
320
+ // The abstain marker in the transcript ends the wait on its own; this
321
+ // only retires the Stop control, which no frame lowers.
322
+ client.on("llmNoResponse", () => this.setResponding(false)),
323
+ client.on("botReady", () => {
324
+ this.ready = true;
325
+ this.setStatus("Ready to chat");
326
+ this.updateControls(true);
327
+ this.dispatch("convai-ready");
328
+ }),
329
+ client.on("disconnect", () => {
330
+ this.endSession();
331
+ this.setStatus("Session ended");
332
+ this.updateControls(false);
333
+ this.showRetry();
334
+ this.dispatch("convai-disconnect");
335
+ }),
336
+ client.on("error", (error) => {
337
+ if (!controller.signal.aborted && this.controller === controller) {
338
+ void this.fail(error);
339
+ }
340
+ }),
341
+ ];
342
+ // Messages can arrive from here on, so the session is live before the
343
+ // connect resolves.
344
+ this.sessionLive = true;
345
+ this.setStatus("Connecting…");
346
+ await client.connectWithPublicationGrant(launchToken, { ...(coreUrl ? { url: coreUrl } : {}), signal: controller.signal });
347
+ // The published connect path assigns the character session id from the
348
+ // connection response without emitting `characterSessionId`, so the
349
+ // listener above never sees the initial value. Read it once here; the
350
+ // listener still covers later changes.
351
+ this.setSessionId(client.characterSessionId);
352
+ if (!this.ready)
353
+ this.setStatus("Connected; waiting for character…");
354
+ }
355
+ catch (error) {
356
+ if (controller.signal.aborted || this.controller !== controller || !this.mounted)
357
+ return;
358
+ await this.fail(error);
359
+ }
360
+ }
361
+ async fail(error) {
362
+ if (this.failureReported)
363
+ return;
364
+ this.failureReported = true;
365
+ const message = errorText(error);
366
+ // cleanupClient() removes the state listener before disconnecting, so the
367
+ // session has to be closed out here or nothing else will.
368
+ this.endSession();
369
+ this.setStatus("Unable to connect");
370
+ this.setError(message);
371
+ this.updateControls(false);
372
+ this.showRetry();
373
+ this.dispatch("convai-error", { message });
374
+ await this.cleanupClient();
375
+ }
376
+ async cleanupClient() {
377
+ this.sessionLive = false;
378
+ this.unsubs.splice(0).forEach((unsubscribe) => unsubscribe());
379
+ const client = this.client;
380
+ this.client = null;
381
+ if (client)
382
+ await client.disconnect().catch(() => undefined);
383
+ }
384
+ /**
385
+ * A host that already knows who the character is can seed the header through
386
+ * `display-name` and `image-url`, so the identity paints before the
387
+ * publication descriptor resolves. The fetched descriptor always wins.
388
+ */
389
+ displayName() {
390
+ if (this.descriptor)
391
+ return this.descriptor.display_name || "Character chat";
392
+ return this.getAttribute("display-name")?.trim() || "Character chat";
393
+ }
394
+ /** `null` is a valid descriptor value, so a loaded descriptor without an
395
+ * image clears the seed rather than falling back to it. */
396
+ imageUrl() {
397
+ if (this.descriptor)
398
+ return this.descriptor.image_url;
399
+ return this.getAttribute("image-url")?.trim() || null;
400
+ }
401
+ renderDescriptor() {
402
+ const root = this.shadowRoot;
403
+ if (!root)
404
+ return;
405
+ const title = root.querySelector("h2");
406
+ if (title)
407
+ title.textContent = this.displayName();
408
+ const imageUrl = this.imageUrl();
409
+ root.querySelectorAll("header .avatar").forEach((avatar) => {
410
+ if (imageUrl) {
411
+ avatar.src = imageUrl;
412
+ avatar.alt = "";
413
+ avatar.hidden = false;
414
+ return;
415
+ }
416
+ avatar.removeAttribute("src");
417
+ avatar.hidden = true;
418
+ });
419
+ }
420
+ /**
421
+ * The character's own avatar, reused as the icon beside each of its replies.
422
+ * Without an image this is the character's initial on the accent disc, not a
423
+ * bare `<img>` — which the avatar rule would paint as an empty disc.
424
+ */
425
+ avatarElement() {
426
+ const imageUrl = this.imageUrl();
427
+ if (imageUrl) {
428
+ const avatar = document.createElement("img");
429
+ avatar.className = "avatar";
430
+ avatar.alt = "";
431
+ avatar.src = imageUrl;
432
+ return avatar;
433
+ }
434
+ const fallback = document.createElement("span");
435
+ fallback.className = "avatar avatar-fallback";
436
+ fallback.setAttribute("aria-hidden", "true");
437
+ fallback.textContent = this.displayName().trim().charAt(0) || "?";
438
+ return fallback;
439
+ }
440
+ /**
441
+ * Whether the character owes a reply that has produced no text yet — the
442
+ * question the typing indicator asks.
443
+ *
444
+ * Derived from the message list on every render rather than tracked as
445
+ * state, because the list already records every event that starts or ends a
446
+ * wait: a send appends a `user-llm-text` row synchronously
447
+ * (`MessageHandler.addUserTextMessage`), a turn begins as an empty
448
+ * `bot-llm-text` row (`bot-llm-started`), the first token fills that row,
449
+ * and an abstain appends `llm-no-response`. Held as flags instead, each of
450
+ * those events needed its own handler and every one I missed was a bug:
451
+ * dots through an entire reply, no dots for a character-initiated greeting
452
+ * or a follow-up turn, dots forever after an abstain.
453
+ *
454
+ * The scan reads the unfiltered list: the rows that mark a turn's start and
455
+ * its abstain are exactly the ones the transcript hides. An empty bot row
456
+ * means the turn is open only while it is still streaming — `bot-llm-stopped`
457
+ * finalizes the row in place, so an empty finalized row is a turn that ended
458
+ * without text (an interrupt, or a stop before the first token).
459
+ */
460
+ isAwaitingReply() {
461
+ if (!this.sessionLive)
462
+ return false;
463
+ for (let index = this.messages.length - 1; index >= 0; index -= 1) {
464
+ const message = this.messages[index];
465
+ // The character ended its turn without saying anything.
466
+ if (message.type === "llm-no-response")
467
+ return false;
468
+ if (!VISIBLE_MESSAGE_TYPES.has(message.type))
469
+ continue;
470
+ if (message.type.startsWith("user"))
471
+ return true;
472
+ return Boolean(message.isStreaming) && !message.content?.trim();
473
+ }
474
+ return false;
475
+ }
476
+ /**
477
+ * Close the open turn in this element's own copy of the transcript.
478
+ *
479
+ * `sendInterruptMessage()` transmits the interrupt; it does not finalize the
480
+ * empty streaming row, which stays open until a `bot-llm-stopped` frame
481
+ * arrives — delayed, or never, if the acknowledgement is lost. The visitor
482
+ * asked the character to stop, so the wait ends with the click. A later
483
+ * `messagesChange` replaces this array wholesale, so nothing diverges.
484
+ */
485
+ finalizeOpenTurn() {
486
+ for (let index = this.messages.length - 1; index >= 0; index -= 1) {
487
+ const message = this.messages[index];
488
+ if (!VISIBLE_MESSAGE_TYPES.has(message.type))
489
+ continue;
490
+ if (message.type.startsWith("user"))
491
+ return;
492
+ if (message.isStreaming) {
493
+ this.messages[index] = { ...message, isStreaming: false };
494
+ this.renderMessages();
495
+ }
496
+ return;
497
+ }
498
+ }
499
+ visibleMessages() {
500
+ return this.messages.filter((message) => VISIBLE_MESSAGE_TYPES.has(message.type) && Boolean(message.content?.trim()));
501
+ }
502
+ renderMessages() {
503
+ const container = this.shadowRoot?.querySelector(".messages");
504
+ if (!container)
505
+ return;
506
+ container.replaceChildren();
507
+ const visible = this.visibleMessages();
508
+ const awaiting = this.isAwaitingReply();
509
+ if (visible.length === 0) {
510
+ container.appendChild(this.introElement());
511
+ // A character-initiated greeting is an empty bot row the transcript
512
+ // hides, so the opening state can be waiting too.
513
+ if (awaiting)
514
+ container.appendChild(this.typingElement());
515
+ if (this.retryVisible)
516
+ container.appendChild(this.retryElement());
517
+ this.updateTranscriptControls();
518
+ return;
519
+ }
520
+ visible.forEach((message) => {
521
+ const isUser = message.type.startsWith("user");
522
+ const row = document.createElement("div");
523
+ row.className = `row ${isUser ? "user" : "character"}`;
524
+ if (!isUser)
525
+ row.appendChild(this.avatarElement());
526
+ const bubble = document.createElement("div");
527
+ bubble.className = "bubble";
528
+ bubble.textContent = message.content;
529
+ row.appendChild(bubble);
530
+ container.appendChild(row);
531
+ });
532
+ if (awaiting)
533
+ container.appendChild(this.typingElement());
534
+ if (this.retryVisible)
535
+ container.appendChild(this.retryElement());
536
+ container.scrollTop = container.scrollHeight;
537
+ this.updateTranscriptControls();
538
+ }
539
+ /** Opening state: who the visitor is talking to, not a bare instruction. */
540
+ introElement() {
541
+ const intro = document.createElement("div");
542
+ intro.className = "intro";
543
+ intro.appendChild(this.avatarElement());
544
+ const name = document.createElement("div");
545
+ name.className = "intro-name";
546
+ name.textContent = this.displayName();
547
+ intro.appendChild(name);
548
+ const description = document.createElement("p");
549
+ description.className = "intro-description";
550
+ description.textContent =
551
+ this.descriptor?.description?.trim() || "Send a message to start a private conversation.";
552
+ intro.appendChild(description);
553
+ return intro;
554
+ }
555
+ typingElement() {
556
+ const row = document.createElement("div");
557
+ row.className = "row character";
558
+ row.appendChild(this.avatarElement());
559
+ const typing = document.createElement("div");
560
+ typing.className = "bubble typing";
561
+ typing.setAttribute("aria-label", "Character is replying");
562
+ for (let index = 0; index < 3; index += 1)
563
+ typing.appendChild(document.createElement("span"));
564
+ row.appendChild(typing);
565
+ return row;
566
+ }
567
+ setStatus(message) {
568
+ const status = this.shadowRoot?.querySelector(".status");
569
+ if (status)
570
+ status.textContent = message;
571
+ }
572
+ setError(message) {
573
+ const error = this.shadowRoot?.querySelector(".error");
574
+ if (!error)
575
+ return;
576
+ error.textContent = message;
577
+ error.hidden = !message;
578
+ }
579
+ setSessionId(sessionId) {
580
+ const assigned = sessionId && sessionId !== UNASSIGNED_SESSION_ID ? sessionId : null;
581
+ this.sessionId = assigned;
582
+ const button = this.shadowRoot?.querySelector(".session");
583
+ if (!button)
584
+ return;
585
+ button.hidden = !assigned;
586
+ button.textContent = assigned ? `Session ${shortSessionId(assigned)}` : "";
587
+ }
588
+ updateControls(enabled) {
589
+ const input = this.shadowRoot?.querySelector("input");
590
+ const button = this.shadowRoot?.querySelector(".send");
591
+ const reset = this.shadowRoot?.querySelector(".reset-session");
592
+ if (input) {
593
+ input.disabled = !enabled;
594
+ input.placeholder = enabled
595
+ ? `Message ${this.displayName()}…`
596
+ : "Preparing chat…";
597
+ }
598
+ if (button)
599
+ button.disabled = !enabled || !input?.value.trim();
600
+ if (reset)
601
+ reset.disabled = !enabled;
602
+ this.updateTranscriptControls();
603
+ }
604
+ updateTranscriptControls() {
605
+ const copy = this.shadowRoot?.querySelector(".copy-chat");
606
+ if (copy)
607
+ copy.disabled = this.visibleMessages().length === 0;
608
+ }
609
+ /** Shows or hides Stop. The typing indicator no longer rides on this. */
610
+ setResponding(responding) {
611
+ const interrupt = this.shadowRoot?.querySelector(".interrupt");
612
+ if (interrupt)
613
+ interrupt.hidden = !responding;
614
+ }
615
+ /** The session can no longer produce a reply: stop waiting, stop offering
616
+ * Stop, and redraw so the transcript agrees. */
617
+ endSession() {
618
+ this.sessionLive = false;
619
+ this.setResponding(false);
620
+ this.renderMessages();
621
+ }
622
+ /** Same `Author: text` shape the playground's Copy chat produces. */
623
+ async copyTranscript() {
624
+ const characterName = this.displayName();
625
+ const transcript = this.visibleMessages()
626
+ .map((message) => `${message.type.startsWith("user") ? "User" : characterName}: ${message.content}`)
627
+ .join("\n");
628
+ if (transcript)
629
+ await this.writeToClipboard(transcript);
630
+ }
631
+ async writeToClipboard(value) {
632
+ try {
633
+ await navigator.clipboard.writeText(value);
634
+ this.dispatch("convai-copy", { value });
635
+ }
636
+ catch {
637
+ this.setError("Could not copy to the clipboard");
638
+ }
639
+ }
640
+ /**
641
+ * A published session is bound to the room it connected with: the character
642
+ * session id travels in the connect request, not in each `user_text_message`,
643
+ * so `ConvaiClient.resetSession()` would clear the transcript while the server
644
+ * kept the previous conversation. Reconnecting on a fresh launch grant is the
645
+ * only reset the published flow supports, and `start()` already tears the old
646
+ * client down before requesting one.
647
+ */
648
+ resetSession() {
649
+ if (!this.client?.state.isConnected)
650
+ return;
651
+ this.dispatch("convai-session-reset");
652
+ void this.start();
653
+ }
654
+ retryElement() {
655
+ const retry = document.createElement("button");
656
+ retry.className = "retry";
657
+ retry.type = "button";
658
+ retry.textContent = "Try again";
659
+ retry.addEventListener("click", () => void this.start(), { once: true });
660
+ return retry;
661
+ }
662
+ /** Held as state, not a one-off append: a typing or message re-render would
663
+ * otherwise clear the only control left after a failed connection. */
664
+ showRetry() {
665
+ if (this.retryVisible)
666
+ return;
667
+ this.retryVisible = true;
668
+ this.renderMessages();
669
+ }
670
+ dispatch(name, detail) {
671
+ this.dispatchEvent(new CustomEvent(name, { bubbles: true, composed: true, detail }));
672
+ }
673
+ }
674
+ export function defineConvaiChatElement(registry = customElements) {
675
+ if (!registry.get("convai-chat")) {
676
+ registry.define("convai-chat", ConvaiChatElement);
677
+ }
678
+ }
679
+ //# sourceMappingURL=ConvaiChatElement.js.map