@vettvangur/ai-sdk 0.15.0 → 0.17.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 +14 -4
- package/dist/binmx.d.ts +1 -1
- package/dist/chat.d.ts +2 -2
- package/dist/contracts.d.ts +19 -2
- package/dist/live-chat/aichat.d.ts +1 -1
- package/dist/live-chat.d.ts +3 -0
- package/dist/mount.d.ts +1 -1
- package/dist/omnichannel.js +23 -10
- package/dist/provider.d.ts +2 -0
- package/dist/styles.css +4 -2
- package/dist/survey.js +1 -1
- package/dist/window.js +53 -14
- package/package.json +1 -1
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
|
|
@@ -167,7 +175,9 @@ there, shows its pre-chat survey in the thread as a `vv-ai-survey` form with `pr
|
|
|
167
175
|
(by field name or id), and starts the chat. A toggle is never ticked from `prefill`: consent is
|
|
168
176
|
the visitor's to give. The form has an optional `survey-title` and
|
|
169
177
|
`survey-intro` above its fields, a `survey-prefilled` note under each field filled in for the
|
|
170
|
-
visitor, and checks that an email field holds an address (`survey-invalid-email`).
|
|
178
|
+
visitor, and checks that an email field holds an address (`survey-invalid-email`). Omnichannel
|
|
179
|
+
writes its own heading and a legend for required fields into the survey, in the widget's language
|
|
180
|
+
rather than the page's; `survey-heading` and `survey-legend` show in their place when set. With `handoverEndpoint` (or
|
|
171
181
|
`data-ai-chat-handover-endpoint`), it first posts `{ conversationId, culture, messages }` with the
|
|
172
182
|
antiforgery header and expects `{ transcript, context, ticket }`: the transcript is sent as the
|
|
173
183
|
visitor's opening message and the context goes to the agent, under the `context` the host passes.
|
|
@@ -223,7 +233,7 @@ binding read. `binding.liveChat` is the provider in use, for a host to offer its
|
|
|
223
233
|
`upload`. Labels: `live-chat-connecting`, `live-chat-closed`, `live-chat-ended`, `live-chat-failed`,
|
|
224
234
|
`live-chat-typing`, `live-chat-agent`, `live-chat-started`, `live-chat-joined` and
|
|
225
235
|
`live-chat-agent-ended` (with `{name}`), `live-chat-waiting`, `live-chat-waiting-text`,
|
|
226
|
-
`live-chat-team`, `survey-title`, `survey-intro`, `survey-prefilled`, `survey-submit`,
|
|
236
|
+
`live-chat-team`, `survey-title`, `survey-intro`, `survey-prefilled`, `survey-heading`, `survey-legend`, `survey-submit`,
|
|
227
237
|
`survey-cancel`, `survey-required`, `survey-invalid-email`, `sheet-close`.
|
|
228
238
|
|
|
229
239
|
`@vettvangur/ai-sdk/live-chat` exports the provider contract (`AiLiveChatProvider`) and
|
package/dist/binmx.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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
|
|
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 };
|
package/dist/contracts.d.ts
CHANGED
|
@@ -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';
|
|
@@ -157,6 +171,9 @@ type AiChatLabels = AiAnswerCardLabels & {
|
|
|
157
171
|
surveyIntro: string;
|
|
158
172
|
/** Under a field the host's prefill filled in, when not empty. */
|
|
159
173
|
surveyPrefilled: string;
|
|
174
|
+
/** In place of the live chat service's own heading and legend in its survey, when not empty. */
|
|
175
|
+
surveyHeading: string;
|
|
176
|
+
surveyLegend: string;
|
|
160
177
|
/** The button that closes a page an agent's message opened in a sheet over the thread. */
|
|
161
178
|
sheetClose: string;
|
|
162
179
|
error: string;
|
|
@@ -335,4 +352,4 @@ type AiLauncherTeaserOptions = {
|
|
|
335
352
|
storageKey?: string;
|
|
336
353
|
};
|
|
337
354
|
//#endregion
|
|
338
|
-
export {
|
|
355
|
+
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 {
|
|
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/live-chat.d.ts
CHANGED
|
@@ -13,6 +13,9 @@ type AiLiveChatSurveyLabels = {
|
|
|
13
13
|
intro?: string;
|
|
14
14
|
/** Shown under a field the prefill filled in, when not empty. */
|
|
15
15
|
prefilled?: string;
|
|
16
|
+
/** In place of the survey's own heading and legend notes, when not empty. */
|
|
17
|
+
heading?: string;
|
|
18
|
+
legend?: string;
|
|
16
19
|
};
|
|
17
20
|
type AiLiveChatSurveyOptions = {
|
|
18
21
|
labels: AiLiveChatSurveyLabels;
|
package/dist/mount.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { o as AiLiveChatProvider } from "./provider.js";
|
|
2
|
-
import {
|
|
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
|
package/dist/omnichannel.js
CHANGED
|
@@ -66,24 +66,35 @@ function isPlaceholderChoice(title) {
|
|
|
66
66
|
/**
|
|
67
67
|
* The pre-chat survey Omnichannel keeps as an Adaptive Card, as the field list the chat window
|
|
68
68
|
* draws. A question's label is its own, or the text block right above it as Omnichannel lays them
|
|
69
|
-
* out; any other text block is a note.
|
|
70
|
-
*
|
|
69
|
+
* out; any other text block is a note. Omnichannel writes its own heading, in bold before the first
|
|
70
|
+
* question, and a subtle legend after the last, in the widget's language rather than the page's;
|
|
71
|
+
* those notes say so in `role`, so the window can show the host's text instead. Throws on an input
|
|
72
|
+
* type the window cannot show rather than leaving a question out.
|
|
71
73
|
*/
|
|
72
74
|
function surveyFieldsFromAdaptiveCard(card) {
|
|
73
75
|
const fields = [];
|
|
74
76
|
let pending = null;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
let questions = 0;
|
|
78
|
+
const flush = (last = false) => {
|
|
79
|
+
const note = pending ? text(pending.text) : null;
|
|
80
|
+
if (pending && note) {
|
|
81
|
+
const role = !questions && String(pending.weight ?? "").toLowerCase() === "bolder" ? "heading" : last && pending.isSubtle === true ? "legend" : null;
|
|
82
|
+
fields.push(role ? {
|
|
83
|
+
kind: "note",
|
|
84
|
+
text: markdown(note),
|
|
85
|
+
role
|
|
86
|
+
} : {
|
|
87
|
+
kind: "note",
|
|
88
|
+
text: markdown(note)
|
|
89
|
+
});
|
|
90
|
+
}
|
|
80
91
|
pending = null;
|
|
81
92
|
};
|
|
82
93
|
for (const element of inputs(card)) {
|
|
83
94
|
const type = element.type;
|
|
84
95
|
if (type === "TextBlock") {
|
|
85
96
|
flush();
|
|
86
|
-
pending =
|
|
97
|
+
pending = element;
|
|
87
98
|
continue;
|
|
88
99
|
}
|
|
89
100
|
if (typeof type !== "string" || !INPUT_TYPES.has(type)) continue;
|
|
@@ -94,10 +105,11 @@ function surveyFieldsFromAdaptiveCard(card) {
|
|
|
94
105
|
const base = {
|
|
95
106
|
id,
|
|
96
107
|
name: asked.name ?? id,
|
|
97
|
-
label: label(own ?? pending ?? asked.text ?? id),
|
|
108
|
+
label: label(own ?? (pending ? text(pending.text) : null) ?? asked.text ?? id),
|
|
98
109
|
required: element.isRequired === true || asked.required
|
|
99
110
|
};
|
|
100
111
|
pending = null;
|
|
112
|
+
questions++;
|
|
101
113
|
if (type === "Input.Text") {
|
|
102
114
|
const style = String(element.style ?? "");
|
|
103
115
|
fields.push({
|
|
@@ -127,7 +139,7 @@ function surveyFieldsFromAdaptiveCard(card) {
|
|
|
127
139
|
kind: "toggle"
|
|
128
140
|
});
|
|
129
141
|
}
|
|
130
|
-
flush();
|
|
142
|
+
flush(true);
|
|
131
143
|
return fields;
|
|
132
144
|
}
|
|
133
145
|
/**
|
|
@@ -171,6 +183,7 @@ function createOmnichannelProvider(config) {
|
|
|
171
183
|
function load() {
|
|
172
184
|
if (chat) return chat;
|
|
173
185
|
const pending = (async () => {
|
|
186
|
+
globalThis.global ??= globalThis;
|
|
174
187
|
const module = await import("@microsoft/omnichannel-chat-sdk").catch((error) => {
|
|
175
188
|
throw new Error("Omnichannel needs @microsoft/omnichannel-chat-sdk installed.", { cause: error });
|
|
176
189
|
});
|
package/dist/provider.d.ts
CHANGED
|
@@ -40,6 +40,8 @@ type AiLiveChatToggleField = AiLiveChatFieldBase & {
|
|
|
40
40
|
type AiLiveChatSurveyNote = {
|
|
41
41
|
kind: 'note';
|
|
42
42
|
text: string;
|
|
43
|
+
/** The survey's own heading or its legend, written by the live chat service rather than its author. */
|
|
44
|
+
role?: 'heading' | 'legend';
|
|
43
45
|
};
|
|
44
46
|
type AiLiveChatSurveyField = AiLiveChatTextField | AiLiveChatChoiceField | AiLiveChatToggleField | AiLiveChatSurveyNote;
|
|
45
47
|
/**
|
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
|
-
.
|
|
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/survey.js
CHANGED
|
@@ -62,7 +62,7 @@ function renderSurveyForm(document, fields, options) {
|
|
|
62
62
|
fields.forEach((field, index) => {
|
|
63
63
|
if (field.kind === "note") {
|
|
64
64
|
const note = createPart(document, "div", SURVEY_CLASS, "note");
|
|
65
|
-
note.append(renderMarkdown(document, field.text));
|
|
65
|
+
note.append(renderMarkdown(document, field.role && options.labels[field.role] || field.text));
|
|
66
66
|
form.append(note);
|
|
67
67
|
return;
|
|
68
68
|
}
|
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: {
|
|
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
|
});
|
|
@@ -899,6 +915,8 @@ const defaultLabels = {
|
|
|
899
915
|
surveyTitle: "",
|
|
900
916
|
surveyIntro: "",
|
|
901
917
|
surveyPrefilled: "",
|
|
918
|
+
surveyHeading: "",
|
|
919
|
+
surveyLegend: "",
|
|
902
920
|
sheetClose: "Close"
|
|
903
921
|
};
|
|
904
922
|
const NEAR_LIMIT = .9;
|
|
@@ -1151,7 +1169,7 @@ function bindChat(root, options = {}) {
|
|
|
1151
1169
|
activeEntryText = null;
|
|
1152
1170
|
}
|
|
1153
1171
|
} else if (event.name === "tool_done") finishTool(event.data.id, event.data.isError);
|
|
1154
|
-
else if (event.name === "suggestions") followUps = event.data.
|
|
1172
|
+
else if (event.name === "suggestions") followUps = event.data.followUps;
|
|
1155
1173
|
else if (event.name === "done" || event.name === "error") clearPendingTool();
|
|
1156
1174
|
});
|
|
1157
1175
|
function setBusy(busy) {
|
|
@@ -1180,12 +1198,28 @@ function bindChat(root, options = {}) {
|
|
|
1180
1198
|
const button = root.ownerDocument.createElement("button");
|
|
1181
1199
|
button.type = "button";
|
|
1182
1200
|
button.className = part("follow-up");
|
|
1183
|
-
button.dataset.aiChatSuggestion = suggestion;
|
|
1184
|
-
button.
|
|
1201
|
+
button.dataset.aiChatSuggestion = suggestion.text;
|
|
1202
|
+
if (suggestion.action) button.dataset.aiChatFollowUpAction = suggestion.action;
|
|
1203
|
+
button.append(root.ownerDocument.createTextNode(suggestion.text));
|
|
1185
1204
|
list.append(button);
|
|
1186
1205
|
}
|
|
1187
1206
|
parent.append(list);
|
|
1188
1207
|
}
|
|
1208
|
+
const announcer = root.ownerDocument.createElement("div");
|
|
1209
|
+
announcer.className = part("announcer");
|
|
1210
|
+
announcer.dataset.aiChatAnnouncer = "true";
|
|
1211
|
+
announcer.setAttribute("role", "status");
|
|
1212
|
+
announcer.setAttribute("aria-live", "polite");
|
|
1213
|
+
announcer.setAttribute("aria-atomic", "true");
|
|
1214
|
+
root.append(announcer);
|
|
1215
|
+
cleanups.push(() => announcer.remove());
|
|
1216
|
+
function announce(message) {
|
|
1217
|
+
announcer.textContent = "";
|
|
1218
|
+
const text = answerText(message);
|
|
1219
|
+
if (text) requestAnimationFrame(() => {
|
|
1220
|
+
announcer.textContent = text;
|
|
1221
|
+
});
|
|
1222
|
+
}
|
|
1189
1223
|
function appendReplyActions(parent, turn) {
|
|
1190
1224
|
const template = root.querySelector("template[data-ai-chat-reply-actions]");
|
|
1191
1225
|
if (!template) return;
|
|
@@ -1196,11 +1230,13 @@ function bindChat(root, options = {}) {
|
|
|
1196
1230
|
else for (const button of actions.querySelectorAll("[data-ai-chat-feedback]")) button.remove();
|
|
1197
1231
|
if (actions.children.length) parent.append(actions);
|
|
1198
1232
|
}
|
|
1233
|
+
function answerText(message) {
|
|
1234
|
+
return Array.from(message?.querySelectorAll("[data-ai-chat-text], [data-ai-chat-card]") ?? [], (element) => (element.innerText ?? element.textContent ?? "").trim()).filter(Boolean).join("\n\n");
|
|
1235
|
+
}
|
|
1199
1236
|
async function copyReply(button) {
|
|
1200
|
-
const
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
await navigator.clipboard.writeText(parts.join("\n\n"));
|
|
1237
|
+
const text = answerText(button.closest("[data-ai-chat-message]"));
|
|
1238
|
+
if (!text) return;
|
|
1239
|
+
await navigator.clipboard.writeText(text);
|
|
1204
1240
|
button.dataset.aiChatCopied = "true";
|
|
1205
1241
|
setTimeout(() => delete button.dataset.aiChatCopied, 2e3);
|
|
1206
1242
|
}
|
|
@@ -1551,7 +1587,9 @@ function bindChat(root, options = {}) {
|
|
|
1551
1587
|
invalidEmail: labels.surveyInvalidEmail,
|
|
1552
1588
|
title: labels.surveyTitle,
|
|
1553
1589
|
intro: labels.surveyIntro,
|
|
1554
|
-
prefilled: labels.surveyPrefilled
|
|
1590
|
+
prefilled: labels.surveyPrefilled,
|
|
1591
|
+
heading: labels.surveyHeading,
|
|
1592
|
+
legend: labels.surveyLegend
|
|
1555
1593
|
},
|
|
1556
1594
|
prefill: {
|
|
1557
1595
|
...handoverPrefill,
|
|
@@ -1642,7 +1680,7 @@ function bindChat(root, options = {}) {
|
|
|
1642
1680
|
scrollToEnd();
|
|
1643
1681
|
await live.provider.send(text);
|
|
1644
1682
|
}
|
|
1645
|
-
const submit = async (event) => {
|
|
1683
|
+
const submit = async (event, followUpAction) => {
|
|
1646
1684
|
event?.preventDefault();
|
|
1647
1685
|
const question = input.value.trim();
|
|
1648
1686
|
if (!question || mode === "handover") return;
|
|
@@ -1677,7 +1715,7 @@ function bindChat(root, options = {}) {
|
|
|
1677
1715
|
setBusy(true);
|
|
1678
1716
|
const assistant = activeAssistant;
|
|
1679
1717
|
const entry = activeEntry;
|
|
1680
|
-
const result = await session.send(question);
|
|
1718
|
+
const result = await session.send(question, { followUpAction });
|
|
1681
1719
|
if (destroyed) return;
|
|
1682
1720
|
setBusy(false);
|
|
1683
1721
|
entry.turn = result.turn;
|
|
@@ -1691,6 +1729,7 @@ function bindChat(root, options = {}) {
|
|
|
1691
1729
|
if (!result.error && assistant.isConnected && assistant.childNodes.length) {
|
|
1692
1730
|
appendReplyActions(assistant, result.turn);
|
|
1693
1731
|
appendFollowUps(assistant, followUps);
|
|
1732
|
+
announce(assistant);
|
|
1694
1733
|
}
|
|
1695
1734
|
focusInput();
|
|
1696
1735
|
if (result.canceled) activeAssistant?.querySelectorAll(`[data-ai-chat-tool]:not(.${part("tool--done")})`).forEach((item) => {
|
|
@@ -1702,7 +1741,7 @@ function bindChat(root, options = {}) {
|
|
|
1702
1741
|
const suggestion = target?.closest("[data-ai-chat-suggestion]");
|
|
1703
1742
|
if (suggestion) {
|
|
1704
1743
|
input.value = suggestion.dataset.aiChatSuggestion ?? suggestion.textContent?.trim() ?? "";
|
|
1705
|
-
submit();
|
|
1744
|
+
submit(void 0, suggestion.dataset.aiChatFollowUpAction);
|
|
1706
1745
|
return;
|
|
1707
1746
|
}
|
|
1708
1747
|
if (target?.closest("[data-ai-chat-stop]")) {
|