@vettvangur/ai-sdk 0.11.0 → 0.14.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 +21 -8
- package/dist/contracts.d.ts +5 -0
- package/dist/live-chat/aichat.js +9 -11
- package/dist/livechat.js +3 -1
- package/dist/omnichannel.js +3 -1
- package/dist/provider.d.ts +7 -0
- package/dist/styles.css +17 -3
- package/dist/survey.js +0 -1
- package/dist/window.js +64 -26
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ const chat = bindChat(document.querySelector('[data-ai-chat]')!, { registry })
|
|
|
52
52
|
| `data-ai-chat-scroll` | The element the thread scrolls in. While an answer arrives, the latest question is held at its top; scrolling by hand lets go until the next question. |
|
|
53
53
|
| `data-ai-chat-offset` | A selector for the element the shell sits under, such as a fixed header. Its height is kept in `--vv-ai-chat-offset` on the root. `offsetElement` does the same from script. |
|
|
54
54
|
| `data-ai-chat-label-*` | Labels, in kebab case: `error`, `timeout`, `tool-running`, `tool-complete`, `tool-failed`, `tool-interrupted`, `sources`, `details`, `table`, `table-scroll`, `follow-ups`, `live-chat`, `contact`. The `labels` option wins over the attribute. Any other name is the host's own, read with `label(name)`. |
|
|
55
|
-
| `data-ai-chat-tool-*` | The progress label for one tool, with underscores in its name written as hyphens.
|
|
55
|
+
| `data-ai-chat-tool-*` | The progress label for one tool, with underscores in its name written as hyphens. The `toolLabels` option wins over the attribute, and the attribute over a `label` the server sends with the `tool` event. Other tools show `tool-running`. |
|
|
56
56
|
|
|
57
57
|
The binding also keeps `--vv-ai-chat-keyboard` on the root at the height an on-screen keyboard
|
|
58
58
|
covers, grows the input to fit what is typed, sets `aria-busy` on the root while a turn is in flight,
|
|
@@ -126,12 +126,17 @@ it again and carries on. A reset clears it. Without storage, the chat works as b
|
|
|
126
126
|
`binding.handover({ provider, context, prefill })` hands the thread to a person through a live
|
|
127
127
|
chat provider (`provider` defaults to the `liveChat` option). It asks the provider whether anyone is
|
|
128
128
|
there, shows its pre-chat survey in the thread as a `vv-ai-survey` form with `prefill` values in it
|
|
129
|
-
(by field name or id), and starts the chat.
|
|
129
|
+
(by field name or id), and starts the chat. A toggle is never ticked from `prefill`: consent is
|
|
130
|
+
the visitor's to give. The form has an optional `survey-title` and
|
|
130
131
|
`survey-intro` above its fields, a `survey-prefilled` note under each field filled in for the
|
|
131
132
|
visitor, and checks that an email field holds an address (`survey-invalid-email`). With `handoverEndpoint` (or
|
|
132
133
|
`data-ai-chat-handover-endpoint`), it first posts `{ conversationId, culture, messages }` with the
|
|
133
|
-
antiforgery header and expects `{ transcript, context }`: the transcript is sent as the
|
|
134
|
-
opening message and the context goes to the agent, under the `context` the host passes.
|
|
134
|
+
antiforgery header and expects `{ transcript, context, ticket }`: the transcript is sent as the
|
|
135
|
+
visitor's opening message and the context goes to the agent, under the `context` the host passes.
|
|
136
|
+
Each assistant message carries the `signature` the stream gave it (`replySignature` on a
|
|
137
|
+
`tool_result`, `signature` on `done`), and Vettvangur.AiChat 2.0 leaves out of the transcript any
|
|
138
|
+
assistant line whose signature does not hold, so a visitor cannot write the assistant's side. The
|
|
139
|
+
`ticket` is what the AiChat Live desk starts from. A failed
|
|
135
140
|
request hands over without them, so a visitor always reaches a person. It resolves to `live`,
|
|
136
141
|
`closed` with a reason, `canceled` or `failed`. Every handover starts from the `handoverPrefill`
|
|
137
142
|
option, or the `data-ai-chat-live-prefill` attribute as a JSON object of answers, with `prefill`
|
|
@@ -163,7 +168,9 @@ the buttons a message carries (`actions`) in its `__text` bubble (or in the mess
|
|
|
163
168
|
no text) in `renderActions` markup, the first primary. A
|
|
164
169
|
link button opens in a new tab. A `sheet` button opens its page in a `__sheet` dialog appended to
|
|
165
170
|
the root: a `__sheet-header` with a `__sheet-title` (the button's text) and a `__sheet-close`
|
|
166
|
-
button (the `sheet-close` label), and the page in a `__sheet-frame
|
|
171
|
+
button (the `sheet-close` label), and the page in a `__sheet-frame`, sandboxed to scripts, forms,
|
|
172
|
+
popups and its own origin. When the page is on another host, a `__sheet-host` line under the title
|
|
173
|
+
names it, so the visitor sees where they are signing in. Escape closes it too, and
|
|
167
174
|
focus goes back to the button. A provider with `connectSheet(frame, { send, close })` connects the
|
|
168
175
|
page to its own app protocol, so the page can send the visitor's message and close the sheet. The
|
|
169
176
|
provider's own messages are `__notice--system`. While live the input shows its
|
|
@@ -185,7 +192,10 @@ binding read. `binding.liveChat` is the provider in use, for a host to offer its
|
|
|
185
192
|
`renderSurveyForm`. `@vettvangur/ai-sdk/live-chat/omnichannel` exports
|
|
186
193
|
`createOmnichannelProvider({ orgId, orgUrl, widgetId })` over Dynamics 365 Omnichannel. It needs
|
|
187
194
|
the optional peer dependency `@microsoft/omnichannel-chat-sdk` and imports it only when the
|
|
188
|
-
provider is first used, so a page pays for it when a visitor asks for a person.
|
|
195
|
+
provider is first used, so a page pays for it when a visitor asks for a person. A site that does
|
|
196
|
+
not install it needs no bundler setting for it: esbuild leaves the import for run time, where it
|
|
197
|
+
fails with an error naming the package only if the provider is used. The same holds for
|
|
198
|
+
`@livechat/customer-sdk` below, so `mountAiChat` builds without either. It reads opening
|
|
189
199
|
hours from the widget's config and turns the Adaptive Card survey into text, choice and toggle
|
|
190
200
|
fields, refusing a card with any other input rather than leaving a question out.
|
|
191
201
|
|
|
@@ -227,7 +237,10 @@ section. `endpoint` is the prefix the site maps with `MapAiChat`, and `headers`
|
|
|
227
237
|
request, so pass the antiforgery header the chat sends. It reports `no-agents` while nobody has the
|
|
228
238
|
Live view open, and its resume token is the visitor's chat token. Given `survey` fields, it asks
|
|
229
239
|
them before the chat starts and sends the answers to the agent by field name, a choice by its label
|
|
230
|
-
and a toggle as `yes` or `no` (`true` and `false` when not given).
|
|
240
|
+
and a toggle as `yes` or `no` (`true` and `false` when not given). It starts the chat from the
|
|
241
|
+
handover's `ticket`, so the transcript and context the agent sees are the ones the server signed,
|
|
242
|
+
and the window's own context goes as added details. It needs Vettvangur.AiChat 2.0 or later. The
|
|
243
|
+
events stream reconnects when it drops and stops when the server answers 401, 403 or 404.
|
|
231
244
|
|
|
232
245
|
```ts
|
|
233
246
|
import { createAiChatLiveProvider } from '@vettvangur/ai-sdk/live-chat/aichat'
|
|
@@ -384,7 +397,7 @@ The theme's custom properties, set on the root or any ancestor of the chat and t
|
|
|
384
397
|
| `--vv-ai-shadow-prompt`, `--vv-ai-focus-ring` | a faint ring, the accent at 15% |
|
|
385
398
|
| `--vv-ai-chat-window-z` (the launcher sits one above) | `1000` |
|
|
386
399
|
| `--vv-ai-chat-window-width`, `-height`, `-inset`, `-radius`, `-shadow` | `23.75rem`, `38.75rem`, `1.25rem`, `1rem`, a soft drop |
|
|
387
|
-
| `--vv-ai-chat-window-header`, `-header-agent`, `-header-text`, `-header-muted`, `-header-hover` | the accent, its hover, on-accent,
|
|
400
|
+
| `--vv-ai-chat-window-header`, `-header-agent`, `-header-text`, `-header-muted`, `-header-hover` | the accent, its hover, on-accent, the header text at 85% and 15% |
|
|
388
401
|
| `--vv-ai-chat-assistant-avatar` (an image in the window's assistant avatar) | `none` |
|
|
389
402
|
| `--vv-ai-launcher-shadow`, `--vv-ai-launcher-teaser-shadow`, `--vv-ai-menu-shadow` | soft drops |
|
|
390
403
|
| `--vv-ai-chat-spring`, `--vv-ai-launcher-spring`, `--vv-ai-launcher-wind-up` | the window's and launcher's easings |
|
package/dist/contracts.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ import { n as AiLiveChatAnswers, o as AiLiveChatProvider } from "./provider.js";
|
|
|
3
3
|
type AiChatMessage = {
|
|
4
4
|
role: 'user' | 'assistant';
|
|
5
5
|
content: string;
|
|
6
|
+
/** The server's signature on an assistant reply, which a handover needs to pass it to an agent. */
|
|
7
|
+
signature?: string;
|
|
6
8
|
};
|
|
7
9
|
type AiChatRequest = {
|
|
8
10
|
messages: AiChatMessage[];
|
|
@@ -89,6 +91,7 @@ type AiChatStreamEvent = {
|
|
|
89
91
|
data: {
|
|
90
92
|
id: string;
|
|
91
93
|
name: string;
|
|
94
|
+
label?: string;
|
|
92
95
|
};
|
|
93
96
|
} | {
|
|
94
97
|
name: 'tool_result';
|
|
@@ -97,6 +100,7 @@ type AiChatStreamEvent = {
|
|
|
97
100
|
name: string;
|
|
98
101
|
data: unknown;
|
|
99
102
|
reply: string | null;
|
|
103
|
+
replySignature?: string | null;
|
|
100
104
|
};
|
|
101
105
|
} | {
|
|
102
106
|
name: 'tool_done';
|
|
@@ -114,6 +118,7 @@ type AiChatStreamEvent = {
|
|
|
114
118
|
name: 'done';
|
|
115
119
|
data: {
|
|
116
120
|
turn: string | null;
|
|
121
|
+
signature?: string | null;
|
|
117
122
|
};
|
|
118
123
|
} | {
|
|
119
124
|
name: 'error';
|
package/dist/live-chat/aichat.js
CHANGED
|
@@ -4,6 +4,7 @@ import { i as isPlainObject, o as readJsonAttribute } from "../dom.js";
|
|
|
4
4
|
const TOKEN_HEADER = "X-AiChat-Live";
|
|
5
5
|
const RETRY_MS = 1e3;
|
|
6
6
|
const RETRY_LIMIT_MS = 15e3;
|
|
7
|
+
const MAX_RECORD_LENGTH = 1048576;
|
|
7
8
|
function details(config, answers) {
|
|
8
9
|
const result = {};
|
|
9
10
|
for (const field of config.survey ?? []) {
|
|
@@ -49,6 +50,7 @@ async function* readEvents(body) {
|
|
|
49
50
|
if (event) yield event;
|
|
50
51
|
boundary = buffer.indexOf("\n\n");
|
|
51
52
|
}
|
|
53
|
+
if (buffer.length > MAX_RECORD_LENGTH) throw new Error("AI chat live event is too large");
|
|
52
54
|
}
|
|
53
55
|
} finally {
|
|
54
56
|
reader.cancel().catch(() => void 0);
|
|
@@ -117,7 +119,7 @@ function createAiChatLiveProvider(config) {
|
|
|
117
119
|
headers: headers({ Accept: "text/event-stream" }),
|
|
118
120
|
signal: controller.signal
|
|
119
121
|
});
|
|
120
|
-
if (response.status === 404) throw Object.assign(/* @__PURE__ */ new Error("AI chat live chat no longer exists"), { gone: true });
|
|
122
|
+
if (response.status === 404 || response.status === 401 || response.status === 403) throw Object.assign(/* @__PURE__ */ new Error("AI chat live chat no longer exists"), { gone: true });
|
|
121
123
|
if (!response.ok || !response.body) throw new Error(`AI chat live events failed with ${response.status}`);
|
|
122
124
|
return response.body;
|
|
123
125
|
};
|
|
@@ -175,16 +177,12 @@ function createAiChatLiveProvider(config) {
|
|
|
175
177
|
async start(options) {
|
|
176
178
|
stop();
|
|
177
179
|
if (options.resume) token = options.resume;
|
|
178
|
-
else {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
details: details(config, options.answers),
|
|
185
|
-
openingMessage: options.openingMessage
|
|
186
|
-
})).json()).token;
|
|
187
|
-
}
|
|
180
|
+
else token = (await (await post("start", {
|
|
181
|
+
ticket: options.ticket,
|
|
182
|
+
culture: options.context?.aiLanguage ?? document.documentElement.lang,
|
|
183
|
+
context: options.addedContext ?? {},
|
|
184
|
+
details: details(config, options.answers)
|
|
185
|
+
})).json()).token;
|
|
188
186
|
try {
|
|
189
187
|
await follow();
|
|
190
188
|
} catch (caught) {
|
package/dist/livechat.js
CHANGED
|
@@ -345,7 +345,9 @@ function createLiveChatProvider(config) {
|
|
|
345
345
|
function load() {
|
|
346
346
|
if (connection) return connection;
|
|
347
347
|
const pending = (async () => {
|
|
348
|
-
const sdk = (await import("@livechat/customer-sdk")
|
|
348
|
+
const sdk = (await import("@livechat/customer-sdk").catch((error) => {
|
|
349
|
+
throw new Error("LiveChat needs @livechat/customer-sdk installed.", { cause: error });
|
|
350
|
+
})).init({
|
|
349
351
|
organizationId: config.organizationId,
|
|
350
352
|
clientId: config.clientId,
|
|
351
353
|
...config.groupId === void 0 ? {} : { groupId: config.groupId }
|
package/dist/omnichannel.js
CHANGED
|
@@ -171,7 +171,9 @@ function createOmnichannelProvider(config) {
|
|
|
171
171
|
function load() {
|
|
172
172
|
if (chat) return chat;
|
|
173
173
|
const pending = (async () => {
|
|
174
|
-
const module = await import("@microsoft/omnichannel-chat-sdk")
|
|
174
|
+
const module = await import("@microsoft/omnichannel-chat-sdk").catch((error) => {
|
|
175
|
+
throw new Error("Omnichannel needs @microsoft/omnichannel-chat-sdk installed.", { cause: error });
|
|
176
|
+
});
|
|
175
177
|
const sdk = new module.OmnichannelChatSDK({
|
|
176
178
|
orgId: config.orgId,
|
|
177
179
|
orgUrl: config.orgUrl,
|
package/dist/provider.d.ts
CHANGED
|
@@ -53,6 +53,13 @@ type AiLiveChatStartOptions = {
|
|
|
53
53
|
context?: Record<string, string>;
|
|
54
54
|
/** Sent as the visitor's first message once the chat has started. */
|
|
55
55
|
openingMessage?: string;
|
|
56
|
+
/**
|
|
57
|
+
* The handover endpoint's ticket, which carries the transcript and `context` it gave. A
|
|
58
|
+
* provider on the same server starts from it in place of `openingMessage` and `context`.
|
|
59
|
+
*/
|
|
60
|
+
ticket?: string;
|
|
61
|
+
/** What the window adds to `context`, over the handover endpoint's. */
|
|
62
|
+
addedContext?: Record<string, string>;
|
|
56
63
|
/** A token from `resumeToken()`: reconnect to that chat instead of starting a new one. */
|
|
57
64
|
resume?: string;
|
|
58
65
|
};
|
package/dist/styles.css
CHANGED
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
--vv-ai-chat-window-header: var(--vv-ai-color-accent);
|
|
55
55
|
--vv-ai-chat-window-header-agent: var(--vv-ai-color-accent-hover);
|
|
56
56
|
--vv-ai-chat-window-header-text: var(--vv-ai-color-on-accent);
|
|
57
|
-
--vv-ai-chat-window-header-muted:
|
|
58
|
-
--vv-ai-chat-window-header-hover:
|
|
57
|
+
--vv-ai-chat-window-header-muted: color-mix(in srgb, var(--vv-ai-chat-window-header-text) 85%, transparent);
|
|
58
|
+
--vv-ai-chat-window-header-hover: color-mix(in srgb, var(--vv-ai-chat-window-header-text) 15%, transparent);
|
|
59
59
|
/* An image in the circle beside each of the assistant's bubbles in the window. */
|
|
60
60
|
--vv-ai-chat-assistant-avatar: none;
|
|
61
61
|
--vv-ai-launcher-shadow: 0 0.375rem 1.25rem rgb(16 40 80 / 0.25);
|
|
@@ -840,6 +840,10 @@ body:has(.vv-ai-chat:not(.vv-ai-chat--window)) {
|
|
|
840
840
|
background: var(--vv-ai-chat-window-header);
|
|
841
841
|
}
|
|
842
842
|
|
|
843
|
+
.vv-ai-chat__sheet-heading {
|
|
844
|
+
min-width: 0;
|
|
845
|
+
}
|
|
846
|
+
|
|
843
847
|
.vv-ai-chat__sheet-title {
|
|
844
848
|
min-width: 0;
|
|
845
849
|
margin: 0;
|
|
@@ -852,6 +856,16 @@ body:has(.vv-ai-chat:not(.vv-ai-chat--window)) {
|
|
|
852
856
|
text-overflow: ellipsis;
|
|
853
857
|
}
|
|
854
858
|
|
|
859
|
+
.vv-ai-chat__sheet-host {
|
|
860
|
+
margin: 0.125rem 0 0;
|
|
861
|
+
font-size: 0.75rem;
|
|
862
|
+
line-height: 1.2;
|
|
863
|
+
opacity: 0.8;
|
|
864
|
+
white-space: nowrap;
|
|
865
|
+
overflow: hidden;
|
|
866
|
+
text-overflow: ellipsis;
|
|
867
|
+
}
|
|
868
|
+
|
|
855
869
|
/* The close label is for screen readers; a cross shows it. */
|
|
856
870
|
.vv-ai-chat__sheet-close {
|
|
857
871
|
position: relative;
|
|
@@ -993,7 +1007,7 @@ body:has(.vv-ai-chat:not(.vv-ai-chat--window)) {
|
|
|
993
1007
|
}
|
|
994
1008
|
|
|
995
1009
|
.vv-ai-chat--window .vv-ai-chat__avatar {
|
|
996
|
-
background:
|
|
1010
|
+
background: color-mix(in srgb, var(--vv-ai-chat-window-header-text) 18%, transparent);
|
|
997
1011
|
}
|
|
998
1012
|
|
|
999
1013
|
.vv-ai-chat--window .vv-ai-chat__name {
|
package/dist/survey.js
CHANGED
|
@@ -306,7 +306,6 @@ function renderSurveyForm(document, fields, options) {
|
|
|
306
306
|
else {
|
|
307
307
|
input = createPart(document, "input", SURVEY_CLASS, "input");
|
|
308
308
|
input.type = "checkbox";
|
|
309
|
-
input.checked = value === true || value === "true";
|
|
310
309
|
}
|
|
311
310
|
input.id = id;
|
|
312
311
|
input.name = field.name;
|
package/dist/window.js
CHANGED
|
@@ -421,6 +421,7 @@ function renderActions(document, items) {
|
|
|
421
421
|
//#region src/protocol.ts
|
|
422
422
|
const AI_CHAT_PROTOCOL_VERSION = 1;
|
|
423
423
|
const MAX_TURN_TOKEN_LENGTH = 1024;
|
|
424
|
+
const MAX_SIGNATURE_LENGTH = 1024;
|
|
424
425
|
function isRecord(value) {
|
|
425
426
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
426
427
|
}
|
|
@@ -428,6 +429,11 @@ function stringValue(data, key) {
|
|
|
428
429
|
const value = data[key];
|
|
429
430
|
return typeof value === "string" ? value : null;
|
|
430
431
|
}
|
|
432
|
+
const MAX_TOOL_LABEL_LENGTH = 120;
|
|
433
|
+
function signatureEntry(data, key) {
|
|
434
|
+
const value = stringValue(data, key);
|
|
435
|
+
return value && value.length <= MAX_SIGNATURE_LENGTH ? { [key]: value } : {};
|
|
436
|
+
}
|
|
431
437
|
function parseStreamEvent(name, data) {
|
|
432
438
|
if (!isRecord(data)) return null;
|
|
433
439
|
switch (name) {
|
|
@@ -441,11 +447,14 @@ function parseStreamEvent(name, data) {
|
|
|
441
447
|
case "tool": {
|
|
442
448
|
const id = stringValue(data, "id");
|
|
443
449
|
const toolName = stringValue(data, "name");
|
|
444
|
-
|
|
450
|
+
if (id === null || toolName === null) return null;
|
|
451
|
+
const label = stringValue(data, "label");
|
|
452
|
+
return {
|
|
445
453
|
name,
|
|
446
454
|
data: {
|
|
447
455
|
id,
|
|
448
|
-
name: toolName
|
|
456
|
+
name: toolName,
|
|
457
|
+
...label ? { label: label.slice(0, MAX_TOOL_LABEL_LENGTH) } : {}
|
|
449
458
|
}
|
|
450
459
|
};
|
|
451
460
|
}
|
|
@@ -460,7 +469,8 @@ function parseStreamEvent(name, data) {
|
|
|
460
469
|
id,
|
|
461
470
|
name: toolName,
|
|
462
471
|
data: data.data,
|
|
463
|
-
reply
|
|
472
|
+
reply,
|
|
473
|
+
...signatureEntry(data, "replySignature")
|
|
464
474
|
}
|
|
465
475
|
};
|
|
466
476
|
}
|
|
@@ -489,7 +499,10 @@ function parseStreamEvent(name, data) {
|
|
|
489
499
|
const turn = stringValue(data, "turn");
|
|
490
500
|
return {
|
|
491
501
|
name,
|
|
492
|
-
data: {
|
|
502
|
+
data: {
|
|
503
|
+
turn: turn && turn.length <= MAX_TURN_TOKEN_LENGTH ? turn : null,
|
|
504
|
+
...signatureEntry(data, "signature")
|
|
505
|
+
}
|
|
493
506
|
};
|
|
494
507
|
}
|
|
495
508
|
case "error": {
|
|
@@ -504,6 +517,7 @@ function parseStreamEvent(name, data) {
|
|
|
504
517
|
}
|
|
505
518
|
//#endregion
|
|
506
519
|
//#region src/stream.ts
|
|
520
|
+
const MAX_RECORD_LENGTH = 1048576;
|
|
507
521
|
function findRecordBoundary(buffer) {
|
|
508
522
|
const lfIndex = buffer.indexOf("\n\n");
|
|
509
523
|
const crlfIndex = buffer.indexOf("\r\n\r\n");
|
|
@@ -568,6 +582,7 @@ async function* readEventStream(response, signal) {
|
|
|
568
582
|
}
|
|
569
583
|
boundary = findRecordBoundary(buffer);
|
|
570
584
|
}
|
|
585
|
+
if (buffer.length > MAX_RECORD_LENGTH) throw new Error("AI chat event stream sent an event that is too large");
|
|
571
586
|
if (readerDone) throw new Error("AI chat event stream ended before a terminal event");
|
|
572
587
|
}
|
|
573
588
|
} finally {
|
|
@@ -589,6 +604,16 @@ function afterNextPaint() {
|
|
|
589
604
|
requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
|
|
590
605
|
});
|
|
591
606
|
}
|
|
607
|
+
function signed(content, signature) {
|
|
608
|
+
return signature ? {
|
|
609
|
+
role: "assistant",
|
|
610
|
+
content,
|
|
611
|
+
signature
|
|
612
|
+
} : {
|
|
613
|
+
role: "assistant",
|
|
614
|
+
content
|
|
615
|
+
};
|
|
616
|
+
}
|
|
592
617
|
function createChatSession(options) {
|
|
593
618
|
const history = [];
|
|
594
619
|
const context = {};
|
|
@@ -628,6 +653,7 @@ function createChatSession(options) {
|
|
|
628
653
|
let error = null;
|
|
629
654
|
let canceled = false;
|
|
630
655
|
let answer = "";
|
|
656
|
+
let answerSignature = null;
|
|
631
657
|
let replies = [];
|
|
632
658
|
timeout = setTimeout(() => requestController.abort(new DOMException("AI chat request timed out", "TimeoutError")), options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
633
659
|
try {
|
|
@@ -647,18 +673,17 @@ function createChatSession(options) {
|
|
|
647
673
|
for (const listener of listeners) listener(event);
|
|
648
674
|
if (event.name === "tool") await afterNextPaint();
|
|
649
675
|
if (event.name === "delta") answer += event.data.text;
|
|
650
|
-
else if (event.name === "tool_result" && event.data.reply) replies.push(event.data.reply);
|
|
651
|
-
else if (event.name === "done")
|
|
652
|
-
|
|
676
|
+
else if (event.name === "tool_result" && event.data.reply) replies.push(signed(event.data.reply, event.data.replySignature));
|
|
677
|
+
else if (event.name === "done") {
|
|
678
|
+
turn = event.data.turn;
|
|
679
|
+
answerSignature = event.data.signature ?? null;
|
|
680
|
+
} else if (event.name === "error") throw new Error(event.data.message);
|
|
653
681
|
}
|
|
654
|
-
if (answer.trim()) replies = [...replies, answer.trim()];
|
|
682
|
+
if (answer.trim()) replies = [...replies, signed(answer.trim(), answerSignature)];
|
|
655
683
|
history.push({
|
|
656
684
|
role: "user",
|
|
657
685
|
content
|
|
658
|
-
}, ...replies
|
|
659
|
-
role: "assistant",
|
|
660
|
-
content: reply
|
|
661
|
-
})));
|
|
686
|
+
}, ...replies);
|
|
662
687
|
} catch (caught) {
|
|
663
688
|
canceled = requestController.signal.aborted;
|
|
664
689
|
error = caught instanceof Error ? caught : /* @__PURE__ */ new Error("AI chat request failed");
|
|
@@ -812,10 +837,11 @@ function registryEntries(registry) {
|
|
|
812
837
|
/** The `data-ai-action` of a button that opens its `data-ai-action-value` in a sheet. */
|
|
813
838
|
const SHEET_ACTION = "sheet";
|
|
814
839
|
const SHEET_CLASS = "vv-ai-chat";
|
|
840
|
+
const SHEET_SANDBOX = "allow-scripts allow-forms allow-popups allow-same-origin";
|
|
815
841
|
/**
|
|
816
|
-
* A page in a sheet over the chat: a dialog with a title,
|
|
817
|
-
* appended to `root`. Escape closes it too, and focus
|
|
818
|
-
* a link could not open either.
|
|
842
|
+
* A page in a sheet over the chat: a dialog with a title, the page's host under it, a close
|
|
843
|
+
* button and the page in a sandboxed frame, appended to `root`. Escape closes it too, and focus
|
|
844
|
+
* goes back to where it was. Null for a URL a link could not open either.
|
|
819
845
|
*/
|
|
820
846
|
function openSheet(root, options) {
|
|
821
847
|
if (!isSafeLinkUrl(options.url)) return null;
|
|
@@ -828,13 +854,18 @@ function openSheet(root, options) {
|
|
|
828
854
|
sheet.setAttribute("aria-modal", "true");
|
|
829
855
|
sheet.setAttribute("aria-labelledby", titleId);
|
|
830
856
|
const header = createPart(document, "div", SHEET_CLASS, "sheet-header");
|
|
857
|
+
const heading = createPart(document, "div", SHEET_CLASS, "sheet-heading");
|
|
831
858
|
const title = createPart(document, "p", SHEET_CLASS, "sheet-title", options.title);
|
|
832
859
|
title.id = titleId;
|
|
860
|
+
const url = new URL(options.url, document.baseURI);
|
|
861
|
+
heading.append(title);
|
|
862
|
+
if (url.origin !== document.location?.origin) heading.append(createPart(document, "p", SHEET_CLASS, "sheet-host", url.host));
|
|
833
863
|
const close = createPart(document, "button", SHEET_CLASS, "sheet-close", options.closeLabel);
|
|
834
864
|
close.type = "button";
|
|
835
|
-
header.append(
|
|
865
|
+
header.append(heading, close);
|
|
836
866
|
const frame = createPart(document, "iframe", SHEET_CLASS, "sheet-frame");
|
|
837
867
|
frame.title = options.title;
|
|
868
|
+
frame.setAttribute("sandbox", SHEET_SANDBOX);
|
|
838
869
|
frame.src = options.url;
|
|
839
870
|
sheet.append(header, frame);
|
|
840
871
|
let open = true;
|
|
@@ -1070,11 +1101,11 @@ function bindChat(root, options = {}) {
|
|
|
1070
1101
|
if (name in labels) return labels[name];
|
|
1071
1102
|
return options.labels?.[name] || root.getAttribute(`data-ai-chat-label-${kebab(name)}`) || "";
|
|
1072
1103
|
}
|
|
1073
|
-
function toolLabel(name) {
|
|
1104
|
+
function toolLabel(name, sent) {
|
|
1074
1105
|
if (!/^[\w-]{1,64}$/.test(name)) return labels.toolRunning;
|
|
1075
|
-
return options.toolLabels?.[name] || root.getAttribute(`data-ai-chat-tool-${kebab(name)}`) || labels.toolRunning;
|
|
1106
|
+
return options.toolLabels?.[name] || root.getAttribute(`data-ai-chat-tool-${kebab(name)}`) || sent || labels.toolRunning;
|
|
1076
1107
|
}
|
|
1077
|
-
function tool(id, name) {
|
|
1108
|
+
function tool(id, name, sent) {
|
|
1078
1109
|
const element = root.ownerDocument.createElement("div");
|
|
1079
1110
|
element.dataset.aiChatTool = id;
|
|
1080
1111
|
element.className = part("tool");
|
|
@@ -1083,7 +1114,7 @@ function bindChat(root, options = {}) {
|
|
|
1083
1114
|
dot.setAttribute("aria-hidden", "true");
|
|
1084
1115
|
const label = root.ownerDocument.createElement("span");
|
|
1085
1116
|
label.className = part("tool-label");
|
|
1086
|
-
label.append(root.ownerDocument.createTextNode(toolLabel(name)));
|
|
1117
|
+
label.append(root.ownerDocument.createTextNode(toolLabel(name, sent)));
|
|
1087
1118
|
const status = root.ownerDocument.createElement("span");
|
|
1088
1119
|
status.className = part("tool-status");
|
|
1089
1120
|
element.append(dot, label, status);
|
|
@@ -1160,7 +1191,7 @@ function bindChat(root, options = {}) {
|
|
|
1160
1191
|
}
|
|
1161
1192
|
} else if (event.name === "tool") {
|
|
1162
1193
|
clearPendingTool();
|
|
1163
|
-
tool(event.data.id, event.data.name);
|
|
1194
|
+
tool(event.data.id, event.data.name, event.data.label);
|
|
1164
1195
|
} else if (event.name === "tool_result") {
|
|
1165
1196
|
clearPendingTool();
|
|
1166
1197
|
if (activeAssistant) {
|
|
@@ -1514,7 +1545,8 @@ function bindChat(root, options = {}) {
|
|
|
1514
1545
|
async function requestHandover() {
|
|
1515
1546
|
if (!handoverEndpoint) return {
|
|
1516
1547
|
transcript: null,
|
|
1517
|
-
context: {}
|
|
1548
|
+
context: {},
|
|
1549
|
+
ticket: null
|
|
1518
1550
|
};
|
|
1519
1551
|
try {
|
|
1520
1552
|
const response = await fetch(handoverEndpoint, {
|
|
@@ -1531,15 +1563,19 @@ function bindChat(root, options = {}) {
|
|
|
1531
1563
|
});
|
|
1532
1564
|
if (!response.ok) throw new Error(`AI chat handover request failed with ${response.status}`);
|
|
1533
1565
|
const body = await response.json();
|
|
1566
|
+
const transcript = isPlainObject(body) && typeof body.transcript === "string" ? body.transcript.trim() : "";
|
|
1567
|
+
const ticket = isPlainObject(body) && typeof body.ticket === "string" && body.ticket ? body.ticket : null;
|
|
1534
1568
|
return {
|
|
1535
|
-
transcript:
|
|
1536
|
-
context: stringRecord(isPlainObject(body) ? body.context : null)
|
|
1569
|
+
transcript: transcript || null,
|
|
1570
|
+
context: stringRecord(isPlainObject(body) ? body.context : null),
|
|
1571
|
+
ticket
|
|
1537
1572
|
};
|
|
1538
1573
|
} catch (caught) {
|
|
1539
1574
|
options.onError?.(caught instanceof Error ? caught : /* @__PURE__ */ new Error("AI chat handover request failed"));
|
|
1540
1575
|
return {
|
|
1541
1576
|
transcript: null,
|
|
1542
|
-
context: {}
|
|
1577
|
+
context: {},
|
|
1578
|
+
ticket: null
|
|
1543
1579
|
};
|
|
1544
1580
|
}
|
|
1545
1581
|
}
|
|
@@ -1606,7 +1642,9 @@ function bindChat(root, options = {}) {
|
|
|
1606
1642
|
...server.context,
|
|
1607
1643
|
...handover.context
|
|
1608
1644
|
},
|
|
1609
|
-
openingMessage: server.transcript ?? void 0
|
|
1645
|
+
openingMessage: server.transcript ?? void 0,
|
|
1646
|
+
ticket: server.ticket ?? void 0,
|
|
1647
|
+
addedContext: { ...handover.context }
|
|
1610
1648
|
});
|
|
1611
1649
|
if (stale()) {
|
|
1612
1650
|
await provider.end();
|