@convai/web-sdk 1.9.0-beta.0 → 1.9.0-beta.1

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 (36) hide show
  1. package/dist/core/index.d.ts +2 -0
  2. package/dist/core/index.d.ts.map +1 -1
  3. package/dist/core/index.js +1 -0
  4. package/dist/core/index.js.map +1 -1
  5. package/dist/embed/chat-embed-v1.js +1 -1
  6. package/dist/embed/chat-embed-v1.js.map +1 -1
  7. package/dist/react/components/rtc-widget/components/MarkdownRenderer.d.ts +14 -3
  8. package/dist/react/components/rtc-widget/components/MarkdownRenderer.d.ts.map +1 -1
  9. package/dist/react/components/rtc-widget/components/MarkdownRenderer.js +110 -130
  10. package/dist/react/components/rtc-widget/components/MarkdownRenderer.js.map +1 -1
  11. package/dist/react/index.d.ts +2 -0
  12. package/dist/react/index.d.ts.map +1 -1
  13. package/dist/react/index.js +1 -0
  14. package/dist/react/index.js.map +1 -1
  15. package/dist/utils/lightbox.d.ts +16 -0
  16. package/dist/utils/lightbox.d.ts.map +1 -0
  17. package/dist/utils/lightbox.js +95 -0
  18. package/dist/utils/lightbox.js.map +1 -0
  19. package/dist/utils/linkPresentation.d.ts +69 -0
  20. package/dist/utils/linkPresentation.d.ts.map +1 -0
  21. package/dist/utils/linkPresentation.js +358 -0
  22. package/dist/utils/linkPresentation.js.map +1 -0
  23. package/dist/vanilla/ConvaiWidget.d.ts.map +1 -1
  24. package/dist/vanilla/ConvaiWidget.js +216 -189
  25. package/dist/vanilla/ConvaiWidget.js.map +1 -1
  26. package/dist/vanilla/index.d.ts +2 -0
  27. package/dist/vanilla/index.d.ts.map +1 -1
  28. package/dist/vanilla/index.js +1 -0
  29. package/dist/vanilla/index.js.map +1 -1
  30. package/dist/vanilla/styles.d.ts.map +1 -1
  31. package/dist/vanilla/styles.js +9 -0
  32. package/dist/vanilla/styles.js.map +1 -1
  33. package/dist/version.d.ts +1 -1
  34. package/dist/version.js +1 -1
  35. package/dist/version.js.map +1 -1
  36. package/package.json +2 -2
@@ -8,6 +8,8 @@ import { Icons } from "./icons.js";
8
8
  import { WidgetEventSubscriptions } from "./WidgetEventSubscriptions.js";
9
9
  import { ConvaiClient } from "../core/ConvaiClient.js";
10
10
  import { parseInlineMarkdown, compactUrl } from "../utils/inlineMarkdown.js";
11
+ import { linkKindForUrl, describeUrl, fileNameOf, documentBadge, contactLabel, hoverTitle, isMixedContent, glyph, presentationStyles as S, toCssText, PREVIEW_TIMEOUT_MS, MAX_CARDS_PER_MESSAGE, } from "../utils/linkPresentation.js";
12
+ import { openLightbox } from "../utils/lightbox.js";
11
13
  import { freshPublicationGrantOptions } from "../core/publishedChat.js";
12
14
  /**
13
15
  * Positioning declarations for the widget root, keyed by `placement`. The
@@ -2074,217 +2076,242 @@ export function createConvaiWidget(container, options) {
2074
2076
  });
2075
2077
  };
2076
2078
  // Microphone button removed - only controlled via voice mode now
2077
- // Helper for Markdown - matches React MarkdownRenderer.tsx
2078
- // `media` renders inline image/video previews under a media link. Bot messages
2079
- // want them; the user's own bubble does not — you already know what you typed,
2080
- // and a preview there would let a URL a user pasted embed remote media into
2081
- // the transcript. Links stay clickable either way.
2079
+ // The bubble renderer. Everything visual comes from utils/linkPresentation
2080
+ // so this and the React MarkdownRenderer cannot drift; this only maps
2081
+ // tokenizer nodes onto DOM. `media` gates cards and chips: bot bubbles get
2082
+ // them, the user's own bubble keeps anchors only, so a pasted URL never
2083
+ // pulls remote media into the transcript.
2082
2084
  const renderMarkdown = (text, container, { media = true } = {}) => {
2083
2085
  if (!text)
2084
2086
  return;
2085
- // Handle both actual newlines and \n escape sequences
2086
- const normalizedText = text.replace(/\\n/g, "\n");
2087
- const lines = normalizedText.split("\n");
2088
- lines.forEach((line, lineIndex) => {
2089
- // Process markdown within each line
2090
- // Bold, italic, links, images and bare URLs — see utils/inlineMarkdown.
2091
- // `cdn.example.com/a/b/c.png` -> `cdn.example.com · c.png`
2092
- const describeUrl = (url) => {
2093
- try {
2094
- const { host, pathname } = new URL(url);
2095
- const file = pathname.split("/").filter(Boolean).pop();
2096
- return file ? `${host} · ${file}` : host;
2097
- }
2098
- catch {
2099
- return url;
2087
+ // Handle both actual newlines and literal "\n" sequences.
2088
+ const lines = text.replace(/\\n/g, "\n").split("\n");
2089
+ // Cards drawn so far in this message, against MAX_CARDS_PER_MESSAGE.
2090
+ const budget = { cards: 0 };
2091
+ const make = (tag, part, ...styles) => {
2092
+ const node = document.createElement(tag);
2093
+ node.setAttribute("part", part);
2094
+ node.style.cssText = toCssText(Object.assign({}, ...styles));
2095
+ return node;
2096
+ };
2097
+ // Inline styles cannot express :focus-visible, so the ring is toggled by hand.
2098
+ const focusRing = (node) => {
2099
+ const base = node.style.cssText;
2100
+ node.addEventListener("focus", () => {
2101
+ if (node.matches(":focus-visible"))
2102
+ node.style.cssText = `${base} ${toCssText(S.linkFocus)}`;
2103
+ });
2104
+ node.addEventListener("blur", () => {
2105
+ node.style.cssText = base;
2106
+ });
2107
+ };
2108
+ const anchor = (href, kind, label) => {
2109
+ const a = make("a", "message-link", S.link);
2110
+ a.href = href;
2111
+ a.target = "_blank";
2112
+ a.rel = "noopener noreferrer";
2113
+ // A bare URL is its own label and may be elided for display; the title
2114
+ // compares against what is shown so the full target is one hover away.
2115
+ const display = label === href ? compactUrl(label) : label;
2116
+ a.textContent = display;
2117
+ const title = hoverTitle(kind, href, display);
2118
+ if (title)
2119
+ a.title = title;
2120
+ if (kind === "ftp") {
2121
+ const badge = make("span", "message-link-badge", S.badge);
2122
+ badge.textContent = "FTP";
2123
+ a.appendChild(badge);
2124
+ }
2125
+ focusRing(a);
2126
+ return a;
2127
+ };
2128
+ const contactChip = (href, kind) => {
2129
+ const a = make("a", "message-contact", S.contact);
2130
+ a.href = href;
2131
+ a.title = hoverTitle(kind, href, href);
2132
+ const g = make("span", "message-contact-glyph", S.contactGlyph);
2133
+ g.innerHTML = glyph(kind);
2134
+ const t = document.createElement("span");
2135
+ t.textContent = contactLabel(href);
2136
+ a.append(g, t);
2137
+ focusRing(a);
2138
+ return a;
2139
+ };
2140
+ const documentCard = (href) => {
2141
+ let host = "";
2142
+ try {
2143
+ host = new URL(href).host;
2144
+ }
2145
+ catch {
2146
+ /* keep empty */
2147
+ }
2148
+ const a = make("a", "message-file-card", S.fileCard);
2149
+ a.href = href;
2150
+ a.target = "_blank";
2151
+ a.rel = "noopener noreferrer";
2152
+ a.title = href;
2153
+ const icon = make("span", "message-file-icon", S.fileIcon);
2154
+ icon.textContent = documentBadge(href);
2155
+ const body = document.createElement("span");
2156
+ body.style.cssText = toCssText(S.fileBody);
2157
+ const name = make("span", "message-file-name", S.fileName);
2158
+ name.textContent = fileNameOf(href);
2159
+ const meta = make("span", "message-file-meta", S.fileMeta);
2160
+ meta.textContent = `${host} · Opens in a new tab`;
2161
+ body.append(name, meta);
2162
+ a.append(icon, body);
2163
+ focusRing(a);
2164
+ return a;
2165
+ };
2166
+ const unavailable = (src, insecure) => {
2167
+ const dead = make("span", "message-media-unavailable", S.unavailable);
2168
+ dead.textContent = `Preview unavailable — ${describeUrl(src)}${insecure ? " (insecure URL)" : ""}`;
2169
+ return dead;
2170
+ };
2171
+ /**
2172
+ * Image or video card. The element is created up front so the fetch starts
2173
+ * immediately; a skeleton covers it until it decodes. Error, timeout, or an
2174
+ * insecure URL collapse the card to one muted line — a character can
2175
+ * invent a URL, and an empty player in the transcript reads as a broken
2176
+ * widget.
2177
+ */
2178
+ const mediaPreview = (src, alt, kind) => {
2179
+ const frag = document.createDocumentFragment();
2180
+ if (isMixedContent(src)) {
2181
+ frag.appendChild(unavailable(src, true));
2182
+ return frag;
2183
+ }
2184
+ const card = make("span", "message-media-card", S.card, kind === "image" ? S.cardButton : {});
2185
+ const el = kind === "image"
2186
+ ? Object.assign(document.createElement("img"), { src, alt, loading: "lazy" })
2187
+ : Object.assign(document.createElement("video"), { src, controls: true, preload: "metadata" });
2188
+ el.setAttribute("part", "message-media");
2189
+ el.style.cssText = toCssText(Object.assign({}, S.media, kind === "video" ? S.videoMedia : {}, { opacity: 0 }));
2190
+ const skeleton = make("span", "message-media-skeleton", S.skeleton);
2191
+ skeleton.setAttribute("aria-hidden", "true");
2192
+ skeleton.dataset.convaiSkeleton = "";
2193
+ const caption = make("span", "message-media-caption", S.caption);
2194
+ caption.textContent = describeUrl(src);
2195
+ const fail = () => {
2196
+ clearTimeout(timer);
2197
+ card.replaceWith(unavailable(src, false));
2198
+ caption.remove();
2199
+ };
2200
+ const settled = () => {
2201
+ clearTimeout(timer);
2202
+ el.style.opacity = "1";
2203
+ skeleton.remove();
2204
+ if (kind === "image") {
2205
+ const zoom = make("span", "message-media-zoom", S.zoom);
2206
+ zoom.innerHTML = glyph("zoom");
2207
+ card.appendChild(zoom);
2100
2208
  }
2101
2209
  };
2102
- /**
2103
- * A preview card that reserves its space before the media arrives, so the
2104
- * bubble does not jump when an image decodes, and collapses to one muted
2105
- * line if the URL does not resolve — a character can easily invent one,
2106
- * and an empty player in the transcript reads as a broken widget.
2107
- *
2108
- * Chrome is derived from the inherited text colour: the widget is embedded
2109
- * in someone else's page, on a background this code cannot know.
2110
- */
2111
- const mediaPreview = (src, alt, media) => {
2112
- const card = document.createElement("span");
2113
- card.setAttribute("part", "message-media-card");
2114
- card.style.cssText = `
2115
- display: block;
2116
- position: relative;
2117
- margin-top: 6px;
2118
- max-width: 320px;
2119
- aspect-ratio: 16 / 10;
2120
- border-radius: 8px;
2121
- overflow: hidden;
2122
- background: color-mix(in srgb, currentColor 6%, transparent);
2123
- box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 10%, transparent);
2124
- `;
2125
- const el = media === "image"
2126
- ? Object.assign(document.createElement("img"), { src, alt, loading: "lazy" })
2127
- : Object.assign(document.createElement("video"), { src, controls: true, preload: "metadata" });
2128
- el.setAttribute("part", "message-media");
2129
- el.style.cssText = `
2130
- display: block;
2131
- width: 100%;
2132
- height: 100%;
2133
- object-fit: ${media === "image" ? "cover" : "contain"};
2134
- border-radius: 8px;
2135
- opacity: 0;
2136
- transition: opacity .18s ease;
2137
- `;
2138
- const skeleton = document.createElement("span");
2139
- skeleton.setAttribute("part", "message-media-skeleton");
2140
- skeleton.setAttribute("aria-hidden", "true");
2141
- skeleton.dataset.convaiSkeleton = "";
2142
- skeleton.style.cssText = `
2143
- position: absolute;
2144
- inset: 0;
2145
- border-radius: 8px;
2146
- background: color-mix(in srgb, currentColor 8%, transparent);
2147
- background-image: linear-gradient(90deg, transparent, color-mix(in srgb, currentColor 12%, transparent), transparent);
2148
- background-size: 200% 100%;
2149
- animation: convai-preview-shimmer 1.4s ease-in-out infinite;
2150
- `;
2151
- const caption = document.createElement("span");
2152
- caption.setAttribute("part", "message-media-caption");
2153
- caption.textContent = describeUrl(src);
2154
- caption.style.cssText = `
2155
- display: block;
2156
- margin-top: 4px;
2157
- font-size: 11px;
2158
- opacity: .7;
2159
- white-space: nowrap;
2160
- overflow: hidden;
2161
- text-overflow: ellipsis;
2162
- `;
2163
- const settled = () => {
2164
- el.style.opacity = "1";
2165
- skeleton.remove();
2166
- };
2167
- el.addEventListener(media === "image" ? "load" : "loadedmetadata", settled);
2168
- el.addEventListener("error", () => {
2169
- const dead = document.createElement("span");
2170
- dead.setAttribute("part", "message-media-unavailable");
2171
- dead.textContent = `Preview unavailable — ${describeUrl(src)}`;
2172
- dead.style.cssText = `
2173
- display: block;
2174
- margin-top: 6px;
2175
- font-size: 11px;
2176
- opacity: .55;
2177
- font-style: italic;
2178
- white-space: nowrap;
2179
- overflow: hidden;
2180
- text-overflow: ellipsis;
2181
- `;
2182
- card.replaceWith(dead);
2183
- caption.remove();
2210
+ const timer = setTimeout(fail, PREVIEW_TIMEOUT_MS);
2211
+ el.addEventListener(kind === "image" ? "load" : "loadedmetadata", settled);
2212
+ el.addEventListener("error", fail);
2213
+ if (kind === "image") {
2214
+ card.setAttribute("role", "button");
2215
+ card.tabIndex = 0;
2216
+ card.setAttribute("aria-label", `Open image: ${fileNameOf(src)}`);
2217
+ const open = () => openLightbox({
2218
+ src,
2219
+ alt,
2220
+ caption: describeUrl(src),
2221
+ href: src,
2222
+ root: styleTarget,
2223
+ returnFocusTo: card,
2184
2224
  });
2185
- card.appendChild(el);
2186
- card.appendChild(skeleton);
2187
- const frag = document.createDocumentFragment();
2188
- frag.appendChild(card);
2189
- frag.appendChild(caption);
2190
- return frag;
2191
- };
2192
- const processLine = (text) => {
2193
- const parts = [];
2194
- for (const node of parseInlineMarkdown(text)) {
2195
- switch (node.kind) {
2196
- case "strong": {
2197
- // matches React UserBubble styling
2198
- const strong = document.createElement("strong");
2199
- strong.textContent = node.text;
2200
- strong.style.cssText = `
2225
+ card.addEventListener("click", open);
2226
+ card.addEventListener("keydown", (e) => {
2227
+ if (e.key === "Enter" || e.key === " ") {
2228
+ e.preventDefault();
2229
+ open();
2230
+ }
2231
+ });
2232
+ }
2233
+ card.append(el, skeleton);
2234
+ frag.append(card, caption);
2235
+ return frag;
2236
+ };
2237
+ const processLine = (line) => {
2238
+ const parts = [];
2239
+ for (const node of parseInlineMarkdown(line)) {
2240
+ switch (node.kind) {
2241
+ case "strong": {
2242
+ // matches React UserBubble styling
2243
+ const strong = document.createElement("strong");
2244
+ strong.textContent = node.text;
2245
+ strong.style.cssText = `
2201
2246
  font-weight: 600;
2202
2247
  color: var(--convai-panel-fg, ${aeroTheme.colors.text.primary});
2203
2248
  `;
2204
- parts.push(strong);
2205
- break;
2206
- }
2207
- case "em": {
2208
- const em = document.createElement("em");
2209
- em.textContent = node.text;
2210
- em.style.cssText = `font-style: italic;`;
2211
- parts.push(em);
2212
- break;
2249
+ parts.push(strong);
2250
+ break;
2251
+ }
2252
+ case "em": {
2253
+ const em = document.createElement("em");
2254
+ em.textContent = node.text;
2255
+ em.style.cssText = `font-style: italic;`;
2256
+ parts.push(em);
2257
+ break;
2258
+ }
2259
+ case "blocked": {
2260
+ // Not an anchor, ever. The label reads as the character wrote it;
2261
+ // the target is only revealed on hover.
2262
+ const blocked = make("span", "message-link-blocked", S.blocked);
2263
+ blocked.textContent = node.label;
2264
+ blocked.title = `Link blocked: ${node.href}`;
2265
+ parts.push(blocked);
2266
+ break;
2267
+ }
2268
+ case "image": {
2269
+ if (media && budget.cards < MAX_CARDS_PER_MESSAGE) {
2270
+ budget.cards++;
2271
+ parts.push(mediaPreview(node.src, node.alt, "image"));
2213
2272
  }
2214
- case "link": {
2215
- const a = document.createElement("a");
2216
- a.href = node.href;
2217
- a.textContent = node.label === node.href ? compactUrl(node.label) : node.label;
2218
- a.target = "_blank";
2219
- a.rel = "noopener noreferrer";
2220
- a.setAttribute("part", "message-link");
2221
- a.style.cssText = `
2222
- color: #10b981;
2223
- text-decoration: underline;
2224
- word-break: break-all;
2225
- `;
2226
- parts.push(a);
2227
- if (media && node.media)
2228
- parts.push(mediaPreview(node.href, node.label, node.media));
2229
- break;
2273
+ else {
2274
+ // With previews off (or the budget spent), an image still has to
2275
+ // say what it pointed at, so it degrades to a plain anchor.
2276
+ parts.push(anchor(node.src, "image", node.alt || node.src));
2230
2277
  }
2231
- case "blocked": {
2232
- const blocked = document.createElement("span");
2233
- blocked.setAttribute("part", "message-link-blocked");
2234
- blocked.textContent = node.label;
2235
- blocked.title = `Link blocked: ${node.href}`;
2236
- blocked.style.cssText = `
2237
- opacity: .75;
2238
- text-decoration: underline dotted;
2239
- cursor: help;
2240
- `;
2241
- parts.push(blocked);
2278
+ break;
2279
+ }
2280
+ case "link": {
2281
+ const kind = linkKindForUrl(node.href);
2282
+ if (kind === "email" || kind === "phone" || kind === "sms") {
2283
+ parts.push(media ? contactChip(node.href, kind) : anchor(node.href, kind, node.label));
2242
2284
  break;
2243
2285
  }
2244
- case "image": {
2245
- if (media) {
2246
- parts.push(mediaPreview(node.src, node.alt, "image"));
2247
- break;
2248
- }
2249
- // With previews off, an image still has to say what it pointed at,
2250
- // so it degrades to the same anchor a plain link would render.
2251
- const imgLink = document.createElement("a");
2252
- imgLink.href = node.src;
2253
- imgLink.textContent = node.alt || node.src;
2254
- imgLink.target = "_blank";
2255
- imgLink.rel = "noopener noreferrer";
2256
- imgLink.setAttribute("part", "message-link");
2257
- imgLink.style.cssText = `
2258
- color: #10b981;
2259
- text-decoration: underline;
2260
- word-break: break-all;
2261
- `;
2262
- parts.push(imgLink);
2286
+ parts.push(anchor(node.href, kind, node.label));
2287
+ const wantsCard = media &&
2288
+ (kind === "image" || kind === "video" || kind === "document") &&
2289
+ budget.cards < MAX_CARDS_PER_MESSAGE;
2290
+ if (!wantsCard)
2263
2291
  break;
2264
- }
2265
- default:
2266
- parts.push(document.createTextNode(node.text));
2292
+ budget.cards++;
2293
+ parts.push(kind === "document" ? documentCard(node.href) : mediaPreview(node.href, node.label, kind));
2294
+ break;
2267
2295
  }
2296
+ default:
2297
+ parts.push(document.createTextNode(node.text));
2268
2298
  }
2269
- return parts;
2270
- };
2271
- const processedLine = processLine(line);
2272
- // Handle empty lines (creates paragraph spacing)
2299
+ }
2300
+ return parts;
2301
+ };
2302
+ lines.forEach((line, lineIndex) => {
2303
+ // An empty line is paragraph spacing.
2273
2304
  if (line.trim() === "" && lineIndex > 0) {
2274
2305
  container.appendChild(document.createElement("br"));
2275
2306
  return;
2276
2307
  }
2277
- // Append the processed line
2278
- if (processedLine.length > 0) {
2279
- processedLine.forEach((part) => container.appendChild(part));
2280
- }
2281
- else {
2308
+ const processed = processLine(line);
2309
+ if (processed.length > 0)
2310
+ processed.forEach((p) => container.appendChild(p));
2311
+ else
2282
2312
  container.appendChild(document.createTextNode(line));
2283
- }
2284
- // Add line break between lines
2285
- if (lineIndex < lines.length - 1) {
2313
+ if (lineIndex < lines.length - 1)
2286
2314
  container.appendChild(document.createElement("br"));
2287
- }
2288
2315
  });
2289
2316
  };
2290
2317
  const updateMessageList = () => {