@vettvangur/ai-sdk 0.15.0 → 0.16.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.
package/README.md CHANGED
@@ -100,12 +100,20 @@ working inside the thread, so a phone's keyboard does not cover the answer.
100
100
 
101
101
  A `suggestions` event lists questions to ask next. They are drawn under the finished answer as
102
102
  `__follow-up` buttons in a `__follow-ups` group labelled `follow-ups`, ask themselves when clicked,
103
- and are removed when the next question is asked.
103
+ and are removed when the next question is asked. A question the server gave an `action`
104
+ (Vettvangur.AiChat 3.1 and later) keeps it in `data-ai-chat-follow-up-action` and sends it as the
105
+ request's `followUpAction`, so the server runs the call it stands for instead of matching the words.
106
+ `session.send(question, { followUpAction })` does the same outside the binding.
107
+
108
+ Each finished answer is read out once through a `__announcer` element the binding adds to the root
109
+ (`role="status"`, `aria-live="polite"`), with the same text the copy button copies, so a screen
110
+ reader hears the answer without the thread announcing every streamed word. It is for screen readers
111
+ only: hide it visually as `styles.css` does, or the host's stylesheet must.
104
112
 
105
113
  Elements it adds carry classes under `CHAT_CLASS` (`vv-ai-chat`): `__message` with `--user` or
106
114
  `--assistant`, `__text`, `__tool` with `--done` and `--failed`, `__tool-dot`, `__tool-label`,
107
115
  `__tool-status`, `__result` around a rendered tool result, `__reply-actions`, `__follow-ups`,
108
- `__follow-up` and `__error`.
116
+ `__follow-up`, `__announcer` and `__error`.
109
117
 
110
118
  `binding.setFormContext(name, summary, followUp)` sends what a visitor entered into an embedded form
111
119
  with every later question. An unchanged summary is not sent again, and `followUp` is added as an
package/dist/binmx.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { N as AiTemplateUrlSurfaceOptions, p as AiChatExtensionContext } from "./contracts.js";
1
+ import { F as AiTemplateUrlSurfaceOptions, p as AiChatExtensionContext } from "./contracts.js";
2
2
  //#region src/binmx.d.ts
3
3
  /**
4
4
  * Makes cloned markup live. binmx processes only the page it started on and what it swaps in
package/dist/chat.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { a as AiLiveChatMessage, c as AiLiveChatStartOptions, l as AiLiveChatSurveyField, n as AiLiveChatAnswers, o as AiLiveChatProvider, r as AiLiveChatAvailability, s as AiLiveChatSheetHost, t as AiLiveChatAction } from "./provider.js";
2
2
  import { t as AiCardImage } from "./dom.js";
3
- import { A as AiLauncherTeaserOptions, C as AiChatSessionOptions, D as AiChatSurface, E as AiChatStreamEvent, M as AiTemplateItemSurfaceOptions, N as AiTemplateUrlSurfaceOptions, O as AiFormContextOptions, S as AiChatSession, T as AiChatSessionStatus, _ as AiChatMessage, a as AiAnswerCardSection, b as AiChatRenderer, c as AiChatActionItem, d as AiChatContext, f as AiChatEventMap, g as AiChatLabels, h as AiChatHandoverResult, i as AiAnswerCardLink, j as AiLiveChatFactoryContext, k as AiHandoverPayload, l as AiChatBinding, m as AiChatHandoverOptions, n as AiAnswerCardFact, o as AiAnswerCardTable, p as AiChatExtensionContext, r as AiAnswerCardLabels, s as AiChatAction, t as AiAnswerCard, u as AiChatBindings, v as AiChatMode, w as AiChatSessionResult, x as AiChatRequest, y as AiChatRegistry } from "./contracts.js";
3
+ import { A as AiFormContextOptions, C as AiChatSendOptions, D as AiChatSessionStatus, E as AiChatSessionResult, F as AiTemplateUrlSurfaceOptions, M as AiLauncherTeaserOptions, N as AiLiveChatFactoryContext, O as AiChatStreamEvent, P as AiTemplateItemSurfaceOptions, S as AiChatRequest, T as AiChatSessionOptions, _ as AiChatLabels, a as AiAnswerCardSection, b as AiChatRegistry, c as AiChatActionItem, d as AiChatContext, f as AiChatEventMap, g as AiChatHandoverResult, h as AiChatHandoverOptions, i as AiAnswerCardLink, j as AiHandoverPayload, k as AiChatSurface, l as AiChatBinding, m as AiChatFollowUp, n as AiAnswerCardFact, o as AiAnswerCardTable, p as AiChatExtensionContext, r as AiAnswerCardLabels, s as AiChatAction, t as AiAnswerCard, u as AiChatBindings, v as AiChatMessage, w as AiChatSession, x as AiChatRenderer, y as AiChatMode } from "./contracts.js";
4
4
  //#region src/answer-card.d.ts
5
5
  declare const MAX_ANSWER_CARD_SECTIONS = 12;
6
6
  declare const MAX_ANSWER_CARD_FACTS = 24;
@@ -129,4 +129,4 @@ declare function bindChatWindow(binding: AiChatBinding): () => void;
129
129
  */
130
130
  declare function bindLauncherTeaser(binding: AiChatBinding, container: ParentNode, options?: AiLauncherTeaserOptions): () => void;
131
131
  //#endregion
132
- export { ACTIONS_CLASS, AI_CHAT_PROTOCOL_VERSION, ANSWER_CARD_CLASS, type AiAnswerCard, type AiAnswerCardFact, type AiAnswerCardLabels, type AiAnswerCardLink, type AiAnswerCardSection, type AiAnswerCardTable, type AiCardImage, type AiChatAction, type AiChatActionItem, type AiChatBinding, type AiChatBindings, type AiChatContext, type AiChatEventMap, type AiChatExtensionContext, type AiChatHandoverOptions, type AiChatHandoverResult, type AiChatMessage, type AiChatMode, type AiChatRegistry, type AiChatRenderer, type AiChatRequest, type AiChatSession, type AiChatSessionOptions, type AiChatSessionResult, type AiChatSessionStatus, type AiChatStreamEvent, type AiChatSurface, type AiFormContextOptions, type AiHandoverPayload, type AiLauncherTeaserOptions, type AiLiveChatAction, type AiLiveChatAnswers, type AiLiveChatAvailability, type AiLiveChatFactoryContext, type AiLiveChatMessage, type AiLiveChatProvider, type AiLiveChatSheetHost, type AiLiveChatStartOptions, type AiLiveChatSurveyField, type AiTemplateItemSurfaceOptions, type AiTemplateUrlSurfaceOptions, CARD_LIST_CLASS, CHAT_CLASS, HANDOVER_TOOL, LIVE_CHAT_ACTION, MAX_ANSWER_CARD_FACTS, MAX_ANSWER_CARD_LINKS, MAX_ANSWER_CARD_SECTIONS, MAX_ANSWER_CARD_TABLES, MAX_ANSWER_CARD_TABLE_CELLS, MAX_ANSWER_CARD_TABLE_ROWS, MAX_ANSWER_CARD_TEXT_LENGTH, MAX_ANSWER_CARD_TOTAL_STRUCTURAL_ITEMS, MAX_ANSWER_CARD_TOTAL_TABLE_CELLS, MAX_ANSWER_CARD_TOTAL_TEXT_CHARACTERS, MAX_ANSWER_CARD_URL_LENGTH, assertAnswerCardContract, bindChat, bindChatWindow, bindLauncherTeaser, closeHandoverCard, createAiChatRegistry, createChatSession, createSingleSurface, createTemplateItemSurface, isAnswerCard, isHandoverPayload, parseStreamEvent, readEventStream, renderActions, renderAnswerCard, renderCardList, renderHandoverCard, watchFormContext };
132
+ export { ACTIONS_CLASS, AI_CHAT_PROTOCOL_VERSION, ANSWER_CARD_CLASS, type AiAnswerCard, type AiAnswerCardFact, type AiAnswerCardLabels, type AiAnswerCardLink, type AiAnswerCardSection, type AiAnswerCardTable, type AiCardImage, type AiChatAction, type AiChatActionItem, type AiChatBinding, type AiChatBindings, type AiChatContext, type AiChatEventMap, type AiChatExtensionContext, type AiChatFollowUp, type AiChatHandoverOptions, type AiChatHandoverResult, type AiChatMessage, type AiChatMode, type AiChatRegistry, type AiChatRenderer, type AiChatRequest, type AiChatSendOptions, type AiChatSession, type AiChatSessionOptions, type AiChatSessionResult, type AiChatSessionStatus, type AiChatStreamEvent, type AiChatSurface, type AiFormContextOptions, type AiHandoverPayload, type AiLauncherTeaserOptions, type AiLiveChatAction, type AiLiveChatAnswers, type AiLiveChatAvailability, type AiLiveChatFactoryContext, type AiLiveChatMessage, type AiLiveChatProvider, type AiLiveChatSheetHost, type AiLiveChatStartOptions, type AiLiveChatSurveyField, type AiTemplateItemSurfaceOptions, type AiTemplateUrlSurfaceOptions, CARD_LIST_CLASS, CHAT_CLASS, HANDOVER_TOOL, LIVE_CHAT_ACTION, MAX_ANSWER_CARD_FACTS, MAX_ANSWER_CARD_LINKS, MAX_ANSWER_CARD_SECTIONS, MAX_ANSWER_CARD_TABLES, MAX_ANSWER_CARD_TABLE_CELLS, MAX_ANSWER_CARD_TABLE_ROWS, MAX_ANSWER_CARD_TEXT_LENGTH, MAX_ANSWER_CARD_TOTAL_STRUCTURAL_ITEMS, MAX_ANSWER_CARD_TOTAL_TABLE_CELLS, MAX_ANSWER_CARD_TOTAL_TEXT_CHARACTERS, MAX_ANSWER_CARD_URL_LENGTH, assertAnswerCardContract, bindChat, bindChatWindow, bindLauncherTeaser, closeHandoverCard, createAiChatRegistry, createChatSession, createSingleSurface, createTemplateItemSurface, isAnswerCard, isHandoverPayload, parseStreamEvent, readEventStream, renderActions, renderAnswerCard, renderCardList, renderHandoverCard, watchFormContext };
@@ -10,6 +10,19 @@ type AiChatRequest = {
10
10
  messages: AiChatMessage[];
11
11
  provider?: string;
12
12
  context?: Record<string, string>;
13
+ /** The action of the follow-up the visitor picked, sent with the question it was offered with. */
14
+ followUpAction?: string;
15
+ };
16
+ /**
17
+ * A question the server suggests asking next. `action` is what picking it sends back, so the
18
+ * server runs the call it stands for; it is null for a question the model answers.
19
+ */
20
+ type AiChatFollowUp = {
21
+ text: string;
22
+ action: string | null;
23
+ };
24
+ type AiChatSendOptions = {
25
+ followUpAction?: string | null;
13
26
  };
14
27
  type AiChatContext = Record<string, string>;
15
28
  type AiChatSessionStatus = 'idle' | 'sending' | 'disposed';
@@ -30,7 +43,7 @@ type AiChatSession = {
30
43
  readonly history: readonly AiChatMessage[];
31
44
  readonly context: Readonly<AiChatContext>;
32
45
  readonly status: AiChatSessionStatus;
33
- send(message: string): Promise<AiChatSessionResult>;
46
+ send(message: string, options?: AiChatSendOptions): Promise<AiChatSessionResult>;
34
47
  cancel(): void;
35
48
  reset(): void;
36
49
  setContext(name: string, value: string): void;
@@ -113,6 +126,7 @@ type AiChatStreamEvent = {
113
126
  name: 'suggestions';
114
127
  data: {
115
128
  suggestions: string[];
129
+ followUps: AiChatFollowUp[];
116
130
  };
117
131
  } | {
118
132
  name: 'done';
@@ -335,4 +349,4 @@ type AiLauncherTeaserOptions = {
335
349
  storageKey?: string;
336
350
  };
337
351
  //#endregion
338
- export { AiLauncherTeaserOptions as A, AiChatSessionOptions as C, AiChatSurface as D, AiChatStreamEvent as E, AiTemplateItemSurfaceOptions as M, AiTemplateUrlSurfaceOptions as N, AiFormContextOptions as O, AiChatSession as S, AiChatSessionStatus as T, AiChatMessage as _, AiAnswerCardSection as a, AiChatRenderer as b, AiChatActionItem as c, AiChatContext as d, AiChatEventMap as f, AiChatLabels as g, AiChatHandoverResult as h, AiAnswerCardLink as i, AiLiveChatFactoryContext as j, AiHandoverPayload as k, AiChatBinding as l, AiChatHandoverOptions as m, AiAnswerCardFact as n, AiAnswerCardTable as o, AiChatExtensionContext as p, AiAnswerCardLabels as r, AiChatAction as s, AiAnswerCard as t, AiChatBindings as u, AiChatMode as v, AiChatSessionResult as w, AiChatRequest as x, AiChatRegistry as y };
352
+ export { AiFormContextOptions as A, AiChatSendOptions as C, AiChatSessionStatus as D, AiChatSessionResult as E, AiTemplateUrlSurfaceOptions as F, AiLauncherTeaserOptions as M, AiLiveChatFactoryContext as N, AiChatStreamEvent as O, AiTemplateItemSurfaceOptions as P, AiChatRequest as S, AiChatSessionOptions as T, AiChatLabels as _, AiAnswerCardSection as a, AiChatRegistry as b, AiChatActionItem as c, AiChatContext as d, AiChatEventMap as f, AiChatHandoverResult as g, AiChatHandoverOptions as h, AiAnswerCardLink as i, AiHandoverPayload as j, AiChatSurface as k, AiChatBinding as l, AiChatFollowUp as m, AiAnswerCardFact as n, AiAnswerCardTable as o, AiChatExtensionContext as p, AiAnswerCardLabels as r, AiChatAction as s, AiAnswerCard as t, AiChatBindings as u, AiChatMessage as v, AiChatSession as w, AiChatRenderer as x, AiChatMode as y };
@@ -1,5 +1,5 @@
1
1
  import { l as AiLiveChatSurveyField, o as AiLiveChatProvider } from "../provider.js";
2
- import { j as AiLiveChatFactoryContext } from "../contracts.js";
2
+ import { N as AiLiveChatFactoryContext } from "../contracts.js";
3
3
  //#region src/live-chat/aichat.d.ts
4
4
  type AiChatLiveConfig = {
5
5
  /** The prefix the site maps the chat under with `MapAiChat`, such as `/webapi/aichat`. */
package/dist/mount.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { o as AiLiveChatProvider } from "./provider.js";
2
- import { j as AiLiveChatFactoryContext, l as AiChatBinding, u as AiChatBindings } from "./contracts.js";
2
+ import { N as AiLiveChatFactoryContext, l as AiChatBinding, u as AiChatBindings } from "./contracts.js";
3
3
  //#region src/mount.d.ts
4
4
  /**
5
5
  * The provider a person answers through, chosen by the root's attributes: Omnichannel when
@@ -171,6 +171,7 @@ function createOmnichannelProvider(config) {
171
171
  function load() {
172
172
  if (chat) return chat;
173
173
  const pending = (async () => {
174
+ globalThis.global ??= globalThis;
174
175
  const module = await import("@microsoft/omnichannel-chat-sdk").catch((error) => {
175
176
  throw new Error("Omnichannel needs @microsoft/omnichannel-chat-sdk installed.", { cause: error });
176
177
  });
package/dist/styles.css CHANGED
@@ -652,8 +652,10 @@ body:has(.vv-ai-chat:not(.vv-ai-chat--window)) {
652
652
  animation-delay: 0.3s;
653
653
  }
654
654
 
655
- /* The typing indicator's text is for screen readers; the dots show it. */
656
- .vv-ai-chat__typing-label {
655
+ /* The typing indicator's text is for screen readers; the dots show it. So is the announcer, which
656
+ reads out each finished answer. */
657
+ .vv-ai-chat__typing-label,
658
+ .vv-ai-chat__announcer {
657
659
  position: absolute;
658
660
  width: 1px;
659
661
  height: 1px;
package/dist/window.js CHANGED
@@ -424,6 +424,7 @@ function renderActions(document, items) {
424
424
  const AI_CHAT_PROTOCOL_VERSION = 1;
425
425
  const MAX_TURN_TOKEN_LENGTH = 1024;
426
426
  const MAX_SIGNATURE_LENGTH = 1024;
427
+ const MAX_FOLLOW_UP_ACTION_LENGTH = 4096;
427
428
  function isRecord(value) {
428
429
  return typeof value === "object" && value !== null && !Array.isArray(value);
429
430
  }
@@ -492,9 +493,23 @@ function parseStreamEvent(name, data) {
492
493
  case "suggestions": {
493
494
  const suggestions = data.suggestions;
494
495
  if (!Array.isArray(suggestions)) return null;
496
+ const isQuestion = (item) => typeof item === "string" && item.trim().length > 0 && item.length <= 200;
497
+ const valid = (Array.isArray(data.followUps) ? data.followUps.filter(isRecord).filter((item) => isQuestion(item.text)).map((item) => {
498
+ const action = stringValue(item, "action");
499
+ return {
500
+ text: item.text,
501
+ action: action && action.length <= MAX_FOLLOW_UP_ACTION_LENGTH ? action : null
502
+ };
503
+ }) : suggestions.filter(isQuestion).map((text) => ({
504
+ text,
505
+ action: null
506
+ }))).slice(0, 6);
495
507
  return {
496
508
  name,
497
- data: { suggestions: suggestions.filter((item) => typeof item === "string" && item.trim().length > 0).filter((item) => item.length <= 200).slice(0, 6) }
509
+ data: {
510
+ suggestions: valid.map((followUp) => followUp.text),
511
+ followUps: valid
512
+ }
498
513
  };
499
514
  }
500
515
  case "done": {
@@ -575,7 +590,7 @@ function createChatSession(options) {
575
590
  get status() {
576
591
  return status;
577
592
  },
578
- async send(message) {
593
+ async send(message, sendOptions) {
579
594
  const content = message.trim();
580
595
  if (!content) throw new Error("AI chat messages cannot be empty");
581
596
  if (status === "disposed") throw new Error("AI chat session is disposed");
@@ -606,7 +621,8 @@ function createChatSession(options) {
606
621
  body: JSON.stringify({
607
622
  messages: requestHistory,
608
623
  provider: options.provider,
609
- context
624
+ context,
625
+ followUpAction: sendOptions?.followUpAction ?? void 0
610
626
  }),
611
627
  signal: requestController.signal
612
628
  });
@@ -1151,7 +1167,7 @@ function bindChat(root, options = {}) {
1151
1167
  activeEntryText = null;
1152
1168
  }
1153
1169
  } else if (event.name === "tool_done") finishTool(event.data.id, event.data.isError);
1154
- else if (event.name === "suggestions") followUps = event.data.suggestions;
1170
+ else if (event.name === "suggestions") followUps = event.data.followUps;
1155
1171
  else if (event.name === "done" || event.name === "error") clearPendingTool();
1156
1172
  });
1157
1173
  function setBusy(busy) {
@@ -1180,12 +1196,28 @@ function bindChat(root, options = {}) {
1180
1196
  const button = root.ownerDocument.createElement("button");
1181
1197
  button.type = "button";
1182
1198
  button.className = part("follow-up");
1183
- button.dataset.aiChatSuggestion = suggestion;
1184
- button.append(root.ownerDocument.createTextNode(suggestion));
1199
+ button.dataset.aiChatSuggestion = suggestion.text;
1200
+ if (suggestion.action) button.dataset.aiChatFollowUpAction = suggestion.action;
1201
+ button.append(root.ownerDocument.createTextNode(suggestion.text));
1185
1202
  list.append(button);
1186
1203
  }
1187
1204
  parent.append(list);
1188
1205
  }
1206
+ const announcer = root.ownerDocument.createElement("div");
1207
+ announcer.className = part("announcer");
1208
+ announcer.dataset.aiChatAnnouncer = "true";
1209
+ announcer.setAttribute("role", "status");
1210
+ announcer.setAttribute("aria-live", "polite");
1211
+ announcer.setAttribute("aria-atomic", "true");
1212
+ root.append(announcer);
1213
+ cleanups.push(() => announcer.remove());
1214
+ function announce(message) {
1215
+ announcer.textContent = "";
1216
+ const text = answerText(message);
1217
+ if (text) requestAnimationFrame(() => {
1218
+ announcer.textContent = text;
1219
+ });
1220
+ }
1189
1221
  function appendReplyActions(parent, turn) {
1190
1222
  const template = root.querySelector("template[data-ai-chat-reply-actions]");
1191
1223
  if (!template) return;
@@ -1196,11 +1228,13 @@ function bindChat(root, options = {}) {
1196
1228
  else for (const button of actions.querySelectorAll("[data-ai-chat-feedback]")) button.remove();
1197
1229
  if (actions.children.length) parent.append(actions);
1198
1230
  }
1231
+ function answerText(message) {
1232
+ return Array.from(message?.querySelectorAll("[data-ai-chat-text], [data-ai-chat-card]") ?? [], (element) => (element.innerText ?? element.textContent ?? "").trim()).filter(Boolean).join("\n\n");
1233
+ }
1199
1234
  async function copyReply(button) {
1200
- const message = button.closest("[data-ai-chat-message]");
1201
- const parts = Array.from(message?.querySelectorAll("[data-ai-chat-text], [data-ai-chat-card]") ?? [], (element) => (element.innerText ?? element.textContent ?? "").trim()).filter(Boolean);
1202
- if (!parts.length) return;
1203
- await navigator.clipboard.writeText(parts.join("\n\n"));
1235
+ const text = answerText(button.closest("[data-ai-chat-message]"));
1236
+ if (!text) return;
1237
+ await navigator.clipboard.writeText(text);
1204
1238
  button.dataset.aiChatCopied = "true";
1205
1239
  setTimeout(() => delete button.dataset.aiChatCopied, 2e3);
1206
1240
  }
@@ -1642,7 +1676,7 @@ function bindChat(root, options = {}) {
1642
1676
  scrollToEnd();
1643
1677
  await live.provider.send(text);
1644
1678
  }
1645
- const submit = async (event) => {
1679
+ const submit = async (event, followUpAction) => {
1646
1680
  event?.preventDefault();
1647
1681
  const question = input.value.trim();
1648
1682
  if (!question || mode === "handover") return;
@@ -1677,7 +1711,7 @@ function bindChat(root, options = {}) {
1677
1711
  setBusy(true);
1678
1712
  const assistant = activeAssistant;
1679
1713
  const entry = activeEntry;
1680
- const result = await session.send(question);
1714
+ const result = await session.send(question, { followUpAction });
1681
1715
  if (destroyed) return;
1682
1716
  setBusy(false);
1683
1717
  entry.turn = result.turn;
@@ -1691,6 +1725,7 @@ function bindChat(root, options = {}) {
1691
1725
  if (!result.error && assistant.isConnected && assistant.childNodes.length) {
1692
1726
  appendReplyActions(assistant, result.turn);
1693
1727
  appendFollowUps(assistant, followUps);
1728
+ announce(assistant);
1694
1729
  }
1695
1730
  focusInput();
1696
1731
  if (result.canceled) activeAssistant?.querySelectorAll(`[data-ai-chat-tool]:not(.${part("tool--done")})`).forEach((item) => {
@@ -1702,7 +1737,7 @@ function bindChat(root, options = {}) {
1702
1737
  const suggestion = target?.closest("[data-ai-chat-suggestion]");
1703
1738
  if (suggestion) {
1704
1739
  input.value = suggestion.dataset.aiChatSuggestion ?? suggestion.textContent?.trim() ?? "";
1705
- submit();
1740
+ submit(void 0, suggestion.dataset.aiChatFollowUpAction);
1706
1741
  return;
1707
1742
  }
1708
1743
  if (target?.closest("[data-ai-chat-stop]")) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vettvangur/ai-sdk",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "Framework-agnostic AI client primitives, and an AI chat for server-rendered shells",
5
5
  "type": "module",
6
6
  "access": "public",