@vettvangur/ai-sdk 0.5.4 → 0.7.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.
- package/README.md +113 -6
- package/dist/binmx.d.ts +17 -0
- package/dist/binmx.js +40 -0
- package/dist/contracts.d.ts +336 -0
- package/dist/dom.js +15 -1
- package/dist/index.d.ts +55 -273
- package/dist/index.js +244 -71
- package/dist/live-chat/aichat.d.ts +10 -1
- package/dist/live-chat/aichat.js +23 -1
- package/dist/styles.css +163 -0
- package/dist/surfaces.js +37 -0
- package/package.json +15 -3
package/README.md
CHANGED
|
@@ -4,7 +4,8 @@ Framework-agnostic, unstyled AI chat primitives for server-rendered shells.
|
|
|
4
4
|
|
|
5
5
|
It exports protocol-v1 contracts, parsing and SSE stream-reading primitives, a headless chat session,
|
|
6
6
|
a DOM binding for a server-rendered shell, and the few generic pieces a host draws tool results with. It
|
|
7
|
-
ships no styles: every element it renders carries a BEM class, and the project styles
|
|
7
|
+
ships no styles by default: every element it renders carries a BEM class, and the project styles
|
|
8
|
+
those. An opt-in base stylesheet holds the mechanics (see [Styles](#styles)).
|
|
8
9
|
|
|
9
10
|
```html
|
|
10
11
|
<section data-ai-chat data-ai-chat-endpoint="/webapi/aichat/stream"
|
|
@@ -50,7 +51,7 @@ const chat = bindChat(document.querySelector('[data-ai-chat]')!, { registry })
|
|
|
50
51
|
| `data-ai-chat-feedback-endpoint` | Where ratings are posted, as `{ turn, rating }` with the antiforgery header. The `feedbackEndpoint` option wins over the attribute. |
|
|
51
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. |
|
|
52
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. |
|
|
53
|
-
| `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`. The `labels` option wins over the attribute. |
|
|
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)`. |
|
|
54
55
|
| `data-ai-chat-tool-*` | The progress label for one tool, with underscores in its name written as hyphens. Other tools show `tool-running`. The `toolLabels` option wins over the attribute. |
|
|
55
56
|
|
|
56
57
|
The binding also keeps `--vv-ai-chat-keyboard` on the root at the height an on-screen keyboard
|
|
@@ -73,6 +74,19 @@ with every later question. An unchanged summary is not sent again, and `followUp
|
|
|
73
74
|
assistant reply only the first time. Renderers and surfaces receive the resolved `labels`, as does
|
|
74
75
|
`binding.labels`.
|
|
75
76
|
|
|
77
|
+
`label(name)`, on the binding and in a renderer's or surface's context, reads a label the SDK has no
|
|
78
|
+
key for, such as a host card's `estimate`: the `labels` option's `estimate`, else
|
|
79
|
+
`data-ai-chat-label-estimate`, else an empty string. A known name returns the resolved label.
|
|
80
|
+
`binding.headers` is the antiforgery header the binding sends (empty without one) and
|
|
81
|
+
`binding.root` the shell, for a host's own requests.
|
|
82
|
+
|
|
83
|
+
`watchFormContext(binding, element, { name, form, title, fields, values, note, followUp })` does
|
|
84
|
+
`setFormContext` for an embedded form. It reads the `form` selector inside `element` now and
|
|
85
|
+
whenever the element changes, as a form that answers by swapping its content does, and sends
|
|
86
|
+
`Context, not a message from the visitor: they filled in {title} with {label}: {value}, ….` followed
|
|
87
|
+
by `note`. Empty fields are left out, `values` turns raw values such as an option's into words, and
|
|
88
|
+
nothing is sent while every field is empty. It returns a function that stops watching.
|
|
89
|
+
|
|
76
90
|
`createChatSession()` is available when a host owns its own rendering. It sends JSON POST requests
|
|
77
91
|
to the configured same-origin endpoint and reads its SSE response. Supply the antiforgery header
|
|
78
92
|
through `bindChat` options or the root attributes; blank header names or tokens are omitted.
|
|
@@ -90,6 +104,20 @@ The same binding drives a compact window on every page. `binding.open()` and `bi
|
|
|
90
104
|
which it is. `binding.on('open' | 'close' | 'handover' | 'end', listener)` returns an unsubscribe;
|
|
91
105
|
the binding knows nothing of analytics or page classes, so the host does those from these events.
|
|
92
106
|
|
|
107
|
+
`bindChatWindow(binding)` wires the page around it and returns a function that undoes it:
|
|
108
|
+
|
|
109
|
+
| Markup | What it does |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| `data-ai-chat-window-open` | Anywhere in the document, opens the window. |
|
|
112
|
+
| `data-ai-chat-window-toggle` | With `data-ai-chat-window-open`, closes it again while open. Every toggle carries `aria-expanded`, and the first gets focus when the window closes. |
|
|
113
|
+
| `details[data-ai-chat-menu]` | A menu in the root. A click anywhere but its `summary` closes it; Escape closes it first, then the window. |
|
|
114
|
+
| `data-ai-chat-expand` | A link in the root, to the full chat page, that closes the window before it navigates, so the stored thread does not open it there too. |
|
|
115
|
+
|
|
116
|
+
`bindLauncherTeaser(binding, container, { storageKey })` shows the `data-ai-launcher-teaser` in
|
|
117
|
+
`container` once a session, to a visitor who has not started a chat: not while the window is open
|
|
118
|
+
or its thread has messages, and not when `sessionStorage` cannot be read. `storageKey` defaults to
|
|
119
|
+
the attribute's value. A `data-ai-launcher-dismiss` button hides it, as opening the window does.
|
|
120
|
+
|
|
93
121
|
With `storageKey` (or `data-ai-chat-storage-key`), the thread is kept in `sessionStorage` under that
|
|
94
122
|
key: the questions, each answer's text and tool results, the people's messages, the notices, the
|
|
95
123
|
conversation id, the open state and a live chat's resume token. The next page in the same tab draws
|
|
@@ -105,7 +133,16 @@ visitor, and checks that an email field holds an address (`survey-invalid-email`
|
|
|
105
133
|
antiforgery header and expects `{ transcript, context }`: the transcript is sent as the visitor's
|
|
106
134
|
opening message and the context goes to the agent, under the `context` the host passes. A failed
|
|
107
135
|
request hands over without them, so a visitor always reaches a person. It resolves to `live`,
|
|
108
|
-
`closed` with a reason, `canceled` or `failed`.
|
|
136
|
+
`closed` with a reason, `canceled` or `failed`. Every handover starts from the `handoverPrefill`
|
|
137
|
+
option, or the `data-ai-chat-live-prefill` attribute as a JSON object of answers, when `prefill` is
|
|
138
|
+
not passed; JSON that does not parse is reported to `onError` and left out.
|
|
139
|
+
|
|
140
|
+
A `tool_result` with `kind: 'handover'`, or from the `request_human_agent` tool (`HANDOVER_TOOL`)
|
|
141
|
+
without a kind, is drawn as a handover card when no renderer is registered for the tool:
|
|
142
|
+
`{ liveChatAvailable, contactUrl }` become a primary `live-chat` button and a `contact` link, in
|
|
143
|
+
`renderActions` markup. The button's action is `livechat` (`LIVE_CHAT_ACTION`), which the binding
|
|
144
|
+
runs as `handover()` unless the host registers its own. `isHandoverPayload` and
|
|
145
|
+
`renderHandoverCard(document, data, labels)` are exported for a host that draws it elsewhere.
|
|
109
146
|
|
|
110
147
|
`binding.mode`, also `data-ai-chat-mode` on the root, is `ai`, `handover` (the input is disabled
|
|
111
148
|
while the survey is asked) or `live`. While live, the composer sends to the provider and the
|
|
@@ -123,7 +160,9 @@ provider's own messages are `__notice--system`. While live the input shows its
|
|
|
123
160
|
or the visitor does with a `data-ai-chat-end` button (shown only while live) or
|
|
124
161
|
`binding.endLiveChat()`, the thread returns to the AI under an `__notice--ended`. A stored live chat
|
|
125
162
|
reconnects on the next page through the `liveChat` option, leaving out messages already shown.
|
|
126
|
-
`
|
|
163
|
+
The `liveChat` option is a provider, or a function the binding calls once with
|
|
164
|
+
`{ root, headers, onError }` to build one from the shell, so it gets the antiforgery header the
|
|
165
|
+
binding read. `binding.liveChat` is the provider in use, for a host to offer its optional `emailTranscript` and
|
|
127
166
|
`upload`. Labels: `live-chat-connecting`, `live-chat-closed`, `live-chat-ended`, `live-chat-failed`,
|
|
128
167
|
`live-chat-typing`, `live-chat-agent`, `live-chat-started`, `live-chat-joined` and
|
|
129
168
|
`live-chat-agent-ended` (with `{name}`), `live-chat-waiting`, `live-chat-waiting-text`,
|
|
@@ -162,12 +201,56 @@ import { createAiChatLiveProvider } from '@vettvangur/ai-sdk/live-chat/aichat'
|
|
|
162
201
|
const liveChat = createAiChatLiveProvider({ endpoint: '/webapi/aichat', headers: { RequestVerificationToken: token } })
|
|
163
202
|
```
|
|
164
203
|
|
|
204
|
+
`liveChatFromRoot` builds the same provider from the shell, as a `liveChat` factory:
|
|
205
|
+
`data-ai-chat-live-endpoint` is the endpoint, and `data-ai-chat-live-survey`, when present, a JSON
|
|
206
|
+
`{ fields, yes, no }`. Without the endpoint there is no provider. A survey that does not parse or has
|
|
207
|
+
no `fields` array is reported to `onError` and the chat starts without one.
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
import { liveChatFromRoot } from '@vettvangur/ai-sdk/live-chat/aichat'
|
|
211
|
+
|
|
212
|
+
const chat = bindChat(root, { liveChat: liveChatFromRoot })
|
|
213
|
+
```
|
|
214
|
+
|
|
165
215
|
## Surfaces
|
|
166
216
|
|
|
167
217
|
`createTemplateItemSurface({ template, attribute, key })` shows one item from an authored list
|
|
168
218
|
template: it clones `template[data-template]` and removes every element carrying `attribute` whose
|
|
169
219
|
value is not the payload's `key` field. Register it with `registry.registerSurface`.
|
|
170
220
|
|
|
221
|
+
`createSingleSurface(key, surface)` keeps only the latest copy of a surface in the thread: it marks
|
|
222
|
+
what the surface draws with `data-ai-chat-single`, and drawing it again removes the earlier copy.
|
|
223
|
+
Use it for an embedded form whose script finds its fields by id across the page.
|
|
224
|
+
|
|
225
|
+
`@vettvangur/ai-sdk/binmx` is for hosts that load embedded content with
|
|
226
|
+
[binmx](https://www.npmjs.com/package/@bgunnarsson/binmx), an optional peer dependency:
|
|
227
|
+
|
|
228
|
+
- `activateEmbedded(element)` has binmx process a clone's `bin-*` attributes, which it does not do
|
|
229
|
+
for content it did not swap in itself, then dispatches `bin:afterSwap` on the body with
|
|
230
|
+
`{ target: element }` so scripts that wait for a swap bind to it. Pass it as `onEmbedded`.
|
|
231
|
+
- `createTemplateUrlSurface({ template, target, from, query, clear, single })` clones
|
|
232
|
+
`template[data-template]` and points the `target` element in it at a URL: the target's `from`
|
|
233
|
+
attribute (`bin-get` when not given) with `query(data)` added as its query string. The target
|
|
234
|
+
gets `bin-get`, `bin-trigger="load"` and `bin-swap="innerHTML"`, so it loads once activated;
|
|
235
|
+
`clear` empties it first, and `single` keeps only the latest copy. `query` returns null when the
|
|
236
|
+
payload cannot be shown, and the surface draws nothing.
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
import { activateEmbedded, createTemplateUrlSurface } from '@vettvangur/ai-sdk/binmx'
|
|
240
|
+
|
|
241
|
+
registry.registerSurface('office-country', createTemplateUrlSurface({
|
|
242
|
+
template: 'office-countries',
|
|
243
|
+
target: '.offices__body',
|
|
244
|
+
from: 'data-offices-url',
|
|
245
|
+
clear: true,
|
|
246
|
+
query: (data) => {
|
|
247
|
+
const { country } = (data ?? {}) as { country?: unknown }
|
|
248
|
+
return typeof country === 'string' ? { countryCode: country } : null
|
|
249
|
+
},
|
|
250
|
+
}))
|
|
251
|
+
const chat = bindChat(root, { registry, onEmbedded: activateEmbedded })
|
|
252
|
+
```
|
|
253
|
+
|
|
171
254
|
## Cards
|
|
172
255
|
|
|
173
256
|
Each returns null when there is nothing to show. Cards for a project's own payloads belong to the
|
|
@@ -198,6 +281,30 @@ of plain fields. Style by these classes rather than by element order.
|
|
|
198
281
|
plain text; a link belongs in `links`, `actions` or `sources`. `isSafeLinkUrl()` is the rule every
|
|
199
282
|
link passes, exported so a host's own renderers can apply it too.
|
|
200
283
|
|
|
284
|
+
`assertAnswerCardContract(card)` is for a server's tests over the cards it sends: it throws when the
|
|
285
|
+
card fails `isAnswerCard()`, and when a plain-text field (a title, note, fact, table label, header
|
|
286
|
+
or cell) holds Markdown, which the card would show as its characters.
|
|
287
|
+
|
|
288
|
+
## Styles
|
|
289
|
+
|
|
290
|
+
`@vettvangur/ai-sdk/styles.css` is an opt-in base for what the SDK renders: the mechanics a thread
|
|
291
|
+
needs, not a look. It keeps `hidden` on `data-ai-chat-send`, `-stop`, `-end`,
|
|
292
|
+
`data-ai-launcher-teaser` and survey errors whatever display a host gives them, pulses the tool and
|
|
293
|
+
typing dots (and stops under reduced motion), hides the typing label for all but screen readers,
|
|
294
|
+
draws the `started`, `joined`, `agent-ended`, `ended` and `system` notices as dividers, scrolls a
|
|
295
|
+
wide answer-card table inside its region, and draws the survey's toggle as a switch. Load it before
|
|
296
|
+
the host's own styles; its selectors are as plain as each rule allows, so a later host rule of the
|
|
297
|
+
same weight wins. Colours and sizes come from custom properties, set on the root or any ancestor:
|
|
298
|
+
|
|
299
|
+
| Property | Default |
|
|
300
|
+
| --- | --- |
|
|
301
|
+
| `--vv-ai-chat-dot-size` | `0.375rem` |
|
|
302
|
+
| `--vv-ai-chat-dot-color` (typing dots) | `currentColor` |
|
|
303
|
+
| `--vv-ai-chat-divider-color` | `currentColor` |
|
|
304
|
+
| `--vv-ai-chat-divider-gap` | `0.625rem` |
|
|
305
|
+
| `--vv-ai-survey-toggle-width`, `-height`, `-inset` | `2.125rem`, `1.25rem`, `0.125rem` |
|
|
306
|
+
| `--vv-ai-survey-toggle-off`, `-on`, `-knob` | `#9ca3af`, `currentColor`, `#fff` |
|
|
307
|
+
|
|
201
308
|
The package accepts protocol-v1 events (`delta`, `tool`, `tool_result`, `tool_done`, `suggestions`,
|
|
202
|
-
`done` with an optional `turn` token, and `error`) and ignores any other. Host-specific card
|
|
203
|
-
|
|
309
|
+
`done` with an optional `turn` token, and `error`) and ignores any other. Host-specific card
|
|
310
|
+
rendering and the look of the chat remain host responsibilities.
|
package/dist/binmx.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { P as AiTemplateUrlSurfaceOptions, m as AiChatExtensionContext } from "./contracts.js";
|
|
2
|
+
//#region src/binmx.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Makes cloned markup live. binmx processes only the page it started on and what it swaps in
|
|
5
|
+
* itself, so the `bin-*` attributes of a template copy wait until it looks again; `bin:afterSwap`
|
|
6
|
+
* then tells the page's own scripts, as a swap of binmx's own would.
|
|
7
|
+
*/
|
|
8
|
+
declare function activateEmbedded(element: HTMLElement): void;
|
|
9
|
+
/**
|
|
10
|
+
* A surface that clones an authored template and has binmx load one of its elements from a URL
|
|
11
|
+
* with the payload's query. The URL starts from the target's `from` attribute, `bin-get` unless
|
|
12
|
+
* given; the target gets it as `bin-get`, with `bin-trigger="load"` and `bin-swap="innerHTML"`.
|
|
13
|
+
* Null when the template or target is missing or `query` refuses the payload.
|
|
14
|
+
*/
|
|
15
|
+
declare function createTemplateUrlSurface(options: AiTemplateUrlSurfaceOptions): (context: Pick<AiChatExtensionContext, 'root' | 'data'>) => DocumentFragment | null;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { activateEmbedded, createTemplateUrlSurface };
|
package/dist/binmx.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { t as createSingleSurface } from "./surfaces.js";
|
|
2
|
+
import { process } from "@bgunnarsson/binmx";
|
|
3
|
+
//#region src/binmx.ts
|
|
4
|
+
/**
|
|
5
|
+
* Makes cloned markup live. binmx processes only the page it started on and what it swaps in
|
|
6
|
+
* itself, so the `bin-*` attributes of a template copy wait until it looks again; `bin:afterSwap`
|
|
7
|
+
* then tells the page's own scripts, as a swap of binmx's own would.
|
|
8
|
+
*/
|
|
9
|
+
function activateEmbedded(element) {
|
|
10
|
+
process(element);
|
|
11
|
+
element.ownerDocument.body.dispatchEvent(new CustomEvent("bin:afterSwap", { detail: { target: element } }));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A surface that clones an authored template and has binmx load one of its elements from a URL
|
|
15
|
+
* with the payload's query. The URL starts from the target's `from` attribute, `bin-get` unless
|
|
16
|
+
* given; the target gets it as `bin-get`, with `bin-trigger="load"` and `bin-swap="innerHTML"`.
|
|
17
|
+
* Null when the template or target is missing or `query` refuses the payload.
|
|
18
|
+
*/
|
|
19
|
+
function createTemplateUrlSurface(options) {
|
|
20
|
+
const from = options.from ?? "bin-get";
|
|
21
|
+
const surface = ({ root, data }) => {
|
|
22
|
+
const query = options.query(data);
|
|
23
|
+
const template = root.querySelector(`template[data-template="${CSS.escape(options.template)}"]`);
|
|
24
|
+
if (!query || !template) return null;
|
|
25
|
+
const content = template.content.cloneNode(true);
|
|
26
|
+
const target = content.querySelector(options.target);
|
|
27
|
+
const start = target?.getAttribute(from);
|
|
28
|
+
if (!target || !start) return null;
|
|
29
|
+
const url = new URL(start, root.ownerDocument.baseURI);
|
|
30
|
+
for (const [name, value] of Object.entries(query)) url.searchParams.set(name, value);
|
|
31
|
+
if (options.clear) target.replaceChildren();
|
|
32
|
+
target.setAttribute("bin-get", url.pathname + url.search);
|
|
33
|
+
target.setAttribute("bin-trigger", "load");
|
|
34
|
+
target.setAttribute("bin-swap", "innerHTML");
|
|
35
|
+
return content;
|
|
36
|
+
};
|
|
37
|
+
return options.single ? createSingleSurface(options.template, surface) : surface;
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
export { activateEmbedded, createTemplateUrlSurface };
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import { a as AiLiveChatProvider, t as AiLiveChatAnswers } from "./provider.js";
|
|
2
|
+
//#region src/contracts.d.ts
|
|
3
|
+
type AiChatMessage = {
|
|
4
|
+
role: 'user' | 'assistant';
|
|
5
|
+
content: string;
|
|
6
|
+
};
|
|
7
|
+
type AiChatRequest = {
|
|
8
|
+
messages: AiChatMessage[];
|
|
9
|
+
provider?: string;
|
|
10
|
+
context?: Record<string, string>;
|
|
11
|
+
};
|
|
12
|
+
type AiChatContext = Record<string, string>;
|
|
13
|
+
type AiChatSessionStatus = 'idle' | 'sending' | 'disposed';
|
|
14
|
+
type AiChatSessionResult = {
|
|
15
|
+
turn: string | null;
|
|
16
|
+
canceled: boolean;
|
|
17
|
+
error: Error | null;
|
|
18
|
+
};
|
|
19
|
+
type AiChatSessionOptions = {
|
|
20
|
+
endpoint: string;
|
|
21
|
+
provider?: string;
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
timeoutMs?: number;
|
|
24
|
+
fetch?: typeof fetch;
|
|
25
|
+
onEvent?: (event: AiChatStreamEvent) => void;
|
|
26
|
+
};
|
|
27
|
+
type AiChatSession = {
|
|
28
|
+
readonly history: readonly AiChatMessage[];
|
|
29
|
+
readonly context: Readonly<AiChatContext>;
|
|
30
|
+
readonly status: AiChatSessionStatus;
|
|
31
|
+
send(message: string): Promise<AiChatSessionResult>;
|
|
32
|
+
cancel(): void;
|
|
33
|
+
reset(): void;
|
|
34
|
+
setContext(name: string, value: string): void;
|
|
35
|
+
setContextMessage(name: string, content: string): void;
|
|
36
|
+
addAssistantReply(content: string): void;
|
|
37
|
+
/** Replaces the history with messages kept from an earlier page, such as a stored thread. */
|
|
38
|
+
restoreHistory(messages: readonly AiChatMessage[]): void;
|
|
39
|
+
clearContext(name: string): void;
|
|
40
|
+
subscribe(listener: (event: AiChatStreamEvent) => void): () => void;
|
|
41
|
+
dispose(): void;
|
|
42
|
+
};
|
|
43
|
+
type AiAnswerCardFact = {
|
|
44
|
+
label: string;
|
|
45
|
+
value: string;
|
|
46
|
+
};
|
|
47
|
+
type AiAnswerCardLink = {
|
|
48
|
+
label: string;
|
|
49
|
+
url: string;
|
|
50
|
+
};
|
|
51
|
+
type AiAnswerCardTable = {
|
|
52
|
+
label?: string | null;
|
|
53
|
+
headers: string[];
|
|
54
|
+
rows: string[][];
|
|
55
|
+
};
|
|
56
|
+
type AiAnswerCardSection = {
|
|
57
|
+
title?: string | null;
|
|
58
|
+
text?: string | null;
|
|
59
|
+
facts?: AiAnswerCardFact[] | null;
|
|
60
|
+
tables?: AiAnswerCardTable[] | null;
|
|
61
|
+
links?: AiAnswerCardLink[] | null;
|
|
62
|
+
note?: string | null;
|
|
63
|
+
collapsible?: boolean;
|
|
64
|
+
};
|
|
65
|
+
type AiAnswerCard = {
|
|
66
|
+
kind: 'answer';
|
|
67
|
+
title: string;
|
|
68
|
+
summary?: string | null;
|
|
69
|
+
highlight?: AiAnswerCardFact | null;
|
|
70
|
+
sections?: AiAnswerCardSection[] | null;
|
|
71
|
+
note?: string | null;
|
|
72
|
+
actions?: AiAnswerCardLink[] | null;
|
|
73
|
+
sources?: AiAnswerCardLink[] | null;
|
|
74
|
+
sourceNote?: string | null;
|
|
75
|
+
};
|
|
76
|
+
type AiAnswerCardLabels = {
|
|
77
|
+
sources: string;
|
|
78
|
+
details: string;
|
|
79
|
+
table: string;
|
|
80
|
+
tableScroll: string;
|
|
81
|
+
};
|
|
82
|
+
type AiChatStreamEvent = {
|
|
83
|
+
name: 'delta';
|
|
84
|
+
data: {
|
|
85
|
+
text: string;
|
|
86
|
+
};
|
|
87
|
+
} | {
|
|
88
|
+
name: 'tool';
|
|
89
|
+
data: {
|
|
90
|
+
id: string;
|
|
91
|
+
name: string;
|
|
92
|
+
};
|
|
93
|
+
} | {
|
|
94
|
+
name: 'tool_result';
|
|
95
|
+
data: {
|
|
96
|
+
id: string;
|
|
97
|
+
name: string;
|
|
98
|
+
data: unknown;
|
|
99
|
+
reply: string | null;
|
|
100
|
+
};
|
|
101
|
+
} | {
|
|
102
|
+
name: 'tool_done';
|
|
103
|
+
data: {
|
|
104
|
+
id: string;
|
|
105
|
+
name: string;
|
|
106
|
+
isError: boolean;
|
|
107
|
+
};
|
|
108
|
+
} | {
|
|
109
|
+
name: 'suggestions';
|
|
110
|
+
data: {
|
|
111
|
+
suggestions: string[];
|
|
112
|
+
};
|
|
113
|
+
} | {
|
|
114
|
+
name: 'done';
|
|
115
|
+
data: {
|
|
116
|
+
turn: string | null;
|
|
117
|
+
};
|
|
118
|
+
} | {
|
|
119
|
+
name: 'error';
|
|
120
|
+
data: {
|
|
121
|
+
message: string;
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
type AiChatLabels = AiAnswerCardLabels & {
|
|
125
|
+
followUps: string;
|
|
126
|
+
/** The handover card's button that starts a live chat, and its link to the contact page. */
|
|
127
|
+
liveChat: string;
|
|
128
|
+
contact: string;
|
|
129
|
+
liveChatConnecting: string;
|
|
130
|
+
liveChatClosed: string;
|
|
131
|
+
liveChatEnded: string;
|
|
132
|
+
liveChatFailed: string;
|
|
133
|
+
liveChatTyping: string;
|
|
134
|
+
liveChatAgent: string;
|
|
135
|
+
/** A divider once the chat has started. */
|
|
136
|
+
liveChatStarted: string;
|
|
137
|
+
/** A divider when the agent joins; `{name}` is their first name. */
|
|
138
|
+
liveChatJoined: string;
|
|
139
|
+
/** A divider when the agent ends the chat; `{name}` is their first name. */
|
|
140
|
+
liveChatAgentEnded: string;
|
|
141
|
+
/** The card shown while the visitor waits for an agent, and the text under it when not empty. */
|
|
142
|
+
liveChatWaiting: string;
|
|
143
|
+
liveChatWaitingText: string;
|
|
144
|
+
/** Shown after the agent's name above their messages when not empty, such as their team. */
|
|
145
|
+
liveChatTeam: string;
|
|
146
|
+
surveySubmit: string;
|
|
147
|
+
surveyCancel: string;
|
|
148
|
+
surveyRequired: string;
|
|
149
|
+
surveyInvalidEmail: string;
|
|
150
|
+
/** Above the survey's fields, each left out when empty. */
|
|
151
|
+
surveyTitle: string;
|
|
152
|
+
surveyIntro: string;
|
|
153
|
+
/** Under a field the host's prefill filled in, when not empty. */
|
|
154
|
+
surveyPrefilled: string;
|
|
155
|
+
error: string;
|
|
156
|
+
timeout: string;
|
|
157
|
+
toolRunning: string;
|
|
158
|
+
toolComplete: string;
|
|
159
|
+
toolFailed: string;
|
|
160
|
+
toolInterrupted: string;
|
|
161
|
+
};
|
|
162
|
+
type AiChatExtensionContext = {
|
|
163
|
+
document: Document;
|
|
164
|
+
root: HTMLElement;
|
|
165
|
+
tool: string;
|
|
166
|
+
data: unknown;
|
|
167
|
+
reply: string | null;
|
|
168
|
+
labels: AiChatLabels;
|
|
169
|
+
/** A label the SDK has no key for, from the `labels` option or `data-ai-chat-label-{kebab}`. */
|
|
170
|
+
label(name: string): string;
|
|
171
|
+
};
|
|
172
|
+
type AiChatRenderer = (context: AiChatExtensionContext) => Node | null;
|
|
173
|
+
type AiChatSurface = (context: AiChatExtensionContext) => Node | null;
|
|
174
|
+
type AiChatActionContext = {
|
|
175
|
+
root: HTMLElement;
|
|
176
|
+
element: HTMLElement;
|
|
177
|
+
value: string | null;
|
|
178
|
+
session: AiChatSession;
|
|
179
|
+
};
|
|
180
|
+
type AiChatAction = (context: AiChatActionContext) => undefined | {
|
|
181
|
+
context: [string, string];
|
|
182
|
+
} | Promise<undefined | {
|
|
183
|
+
context: [string, string];
|
|
184
|
+
}>;
|
|
185
|
+
type AiChatRegistry = {
|
|
186
|
+
registerRenderer(name: string, renderer: AiChatRenderer): () => void;
|
|
187
|
+
registerSurface(name: string, surface: AiChatSurface): () => void;
|
|
188
|
+
registerAction(name: string, action: AiChatAction): () => void;
|
|
189
|
+
};
|
|
190
|
+
type AiChatBindings = {
|
|
191
|
+
endpoint?: string;
|
|
192
|
+
provider?: string;
|
|
193
|
+
antiforgeryHeader?: string;
|
|
194
|
+
antiforgeryToken?: string;
|
|
195
|
+
timeoutMs?: number;
|
|
196
|
+
/** Labels over the root's attributes, with any a host's own renderers read through `label()`. */
|
|
197
|
+
labels?: Partial<AiChatLabels> & {
|
|
198
|
+
[name: string]: string | undefined;
|
|
199
|
+
};
|
|
200
|
+
toolLabels?: Record<string, string>;
|
|
201
|
+
feedbackEndpoint?: string;
|
|
202
|
+
/** Where `handover()` asks the server for the transcript and context to give the agent. */
|
|
203
|
+
handoverEndpoint?: string;
|
|
204
|
+
/** Keeps the thread in sessionStorage under this key, so it carries on across page loads. */
|
|
205
|
+
storageKey?: string;
|
|
206
|
+
/**
|
|
207
|
+
* The provider a stored live chat reconnects through, and `handover()` uses by default. A
|
|
208
|
+
* function is called once with the root and the antiforgery headers the binding read.
|
|
209
|
+
*/
|
|
210
|
+
liveChat?: AiLiveChatProvider | ((context: AiLiveChatFactoryContext) => AiLiveChatProvider | undefined);
|
|
211
|
+
/** Survey answers every handover starts with; `data-ai-chat-live-prefill` as JSON otherwise. */
|
|
212
|
+
handoverPrefill?: AiLiveChatAnswers;
|
|
213
|
+
offsetElement?: HTMLElement | null;
|
|
214
|
+
registry?: AiChatRegistry;
|
|
215
|
+
onEmbedded?: (element: HTMLElement) => void;
|
|
216
|
+
onError?: (error: Error) => void;
|
|
217
|
+
};
|
|
218
|
+
type AiLiveChatFactoryContext = {
|
|
219
|
+
root: HTMLElement;
|
|
220
|
+
headers: Readonly<Record<string, string>>;
|
|
221
|
+
onError: (error: Error) => void;
|
|
222
|
+
};
|
|
223
|
+
/** Who the composer talks to: the AI, a handover on its way, or a person. */
|
|
224
|
+
type AiChatMode = 'ai' | 'handover' | 'live';
|
|
225
|
+
type AiChatHandoverOptions = {
|
|
226
|
+
provider?: AiLiveChatProvider;
|
|
227
|
+
/** Values for the agent, over those the handover endpoint returns. */
|
|
228
|
+
context?: Record<string, string>;
|
|
229
|
+
/** Survey answers to start with, by field name or id. */
|
|
230
|
+
prefill?: AiLiveChatAnswers;
|
|
231
|
+
};
|
|
232
|
+
type AiChatHandoverResult = {
|
|
233
|
+
status: 'live';
|
|
234
|
+
} | {
|
|
235
|
+
status: 'closed';
|
|
236
|
+
reason: 'out-of-hours' | 'no-agents';
|
|
237
|
+
} | {
|
|
238
|
+
status: 'canceled';
|
|
239
|
+
} | {
|
|
240
|
+
status: 'failed';
|
|
241
|
+
error: Error;
|
|
242
|
+
};
|
|
243
|
+
type AiChatEventMap = {
|
|
244
|
+
open: undefined;
|
|
245
|
+
close: undefined;
|
|
246
|
+
handover: undefined;
|
|
247
|
+
/** An agent took the chat; `name` is their full display name when the provider has one. */
|
|
248
|
+
agent: {
|
|
249
|
+
name: string | null;
|
|
250
|
+
};
|
|
251
|
+
end: {
|
|
252
|
+
reason: 'agent' | 'visitor' | 'disconnected';
|
|
253
|
+
};
|
|
254
|
+
};
|
|
255
|
+
type AiChatBinding = {
|
|
256
|
+
root: HTMLElement;
|
|
257
|
+
session: AiChatSession;
|
|
258
|
+
labels: Readonly<AiChatLabels>;
|
|
259
|
+
/** The antiforgery header the binding sends, empty when the shell has none. */
|
|
260
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
261
|
+
readonly mode: AiChatMode;
|
|
262
|
+
readonly isOpen: boolean;
|
|
263
|
+
/** The provider of the live chat in progress, so a host can offer its optional capabilities. */
|
|
264
|
+
readonly liveChat: AiLiveChatProvider | null;
|
|
265
|
+
/** A label the SDK has no key for, from the `labels` option or `data-ai-chat-label-{kebab}`. */
|
|
266
|
+
label(name: string): string;
|
|
267
|
+
appendAssistantReply(text: string): void;
|
|
268
|
+
setFormContext(name: string, summary: string, followUp?: string | null): void;
|
|
269
|
+
open(): void;
|
|
270
|
+
close(): void;
|
|
271
|
+
handover(options?: AiChatHandoverOptions): Promise<AiChatHandoverResult>;
|
|
272
|
+
endLiveChat(): Promise<void>;
|
|
273
|
+
on<K extends keyof AiChatEventMap>(name: K, listener: (detail: AiChatEventMap[K]) => void): () => void;
|
|
274
|
+
destroy(): void;
|
|
275
|
+
};
|
|
276
|
+
type AiCardImage = {
|
|
277
|
+
url: string;
|
|
278
|
+
url2x?: string | null;
|
|
279
|
+
alt?: string | null;
|
|
280
|
+
width?: number | null;
|
|
281
|
+
height?: number | null;
|
|
282
|
+
};
|
|
283
|
+
type AiChatActionItem = {
|
|
284
|
+
label: string;
|
|
285
|
+
action?: string | null;
|
|
286
|
+
value?: string | null;
|
|
287
|
+
url?: string | null;
|
|
288
|
+
primary?: boolean;
|
|
289
|
+
};
|
|
290
|
+
type AiTemplateItemSurfaceOptions = {
|
|
291
|
+
template: string;
|
|
292
|
+
attribute: string;
|
|
293
|
+
key: string;
|
|
294
|
+
};
|
|
295
|
+
/** What a server sends to offer the visitor a person: a live chat, a contact page, or both. */
|
|
296
|
+
type AiHandoverPayload = {
|
|
297
|
+
kind?: 'handover';
|
|
298
|
+
liveChatAvailable?: boolean;
|
|
299
|
+
contactUrl?: string | null;
|
|
300
|
+
};
|
|
301
|
+
type AiTemplateUrlSurfaceOptions = {
|
|
302
|
+
/** The `template[data-template]` to clone. */
|
|
303
|
+
template: string;
|
|
304
|
+
/** The element in the clone that loads its content. */
|
|
305
|
+
target: string;
|
|
306
|
+
/** The attribute on the target holding the URL to start from; `bin-get` when not given. */
|
|
307
|
+
from?: string;
|
|
308
|
+
/** The query to add to the URL from the payload, or null when the payload cannot be shown. */
|
|
309
|
+
query: (data: unknown) => Record<string, string> | null;
|
|
310
|
+
/** Empties the target before it loads, for a target that starts on other content. */
|
|
311
|
+
clear?: boolean;
|
|
312
|
+
/** Keeps only the latest copy in the thread. */
|
|
313
|
+
single?: boolean;
|
|
314
|
+
};
|
|
315
|
+
type AiFormContextOptions = {
|
|
316
|
+
/** The context message's name, as `setFormContext` takes it. */
|
|
317
|
+
name: string;
|
|
318
|
+
/** The form to read, by selector, inside the watched element. */
|
|
319
|
+
form: string;
|
|
320
|
+
/** What the visitor filled in, as it reads after "they filled in", such as `the freight calculator`. */
|
|
321
|
+
title: string;
|
|
322
|
+
/** Form field names and the label each is told with, in order. Empty fields are left out. */
|
|
323
|
+
fields: ReadonlyArray<readonly [field: string, label: string]>;
|
|
324
|
+
/** Readable values for raw ones, such as an option's value. */
|
|
325
|
+
values?: Readonly<Record<string, string>>;
|
|
326
|
+
/** A sentence after the values, such as what the form has already shown the visitor. */
|
|
327
|
+
note?: string;
|
|
328
|
+
/** Added as an assistant reply the first time the form reports. */
|
|
329
|
+
followUp?: string | null;
|
|
330
|
+
};
|
|
331
|
+
type AiLauncherTeaserOptions = {
|
|
332
|
+
/** The sessionStorage key that marks the teaser shown; `data-ai-launcher-teaser` otherwise. */
|
|
333
|
+
storageKey?: string;
|
|
334
|
+
};
|
|
335
|
+
//#endregion
|
|
336
|
+
export { AiHandoverPayload as A, AiChatSession as C, AiChatStreamEvent as D, AiChatSessionStatus as E, AiLiveChatFactoryContext as M, AiTemplateItemSurfaceOptions as N, AiChatSurface as O, AiTemplateUrlSurfaceOptions as P, AiChatRequest as S, AiChatSessionResult as T, AiChatLabels as _, AiAnswerCardSection as a, AiChatRegistry as b, AiChatAction as c, AiChatBindings as d, AiChatContext as f, AiChatHandoverResult as g, AiChatHandoverOptions as h, AiAnswerCardLink as i, AiLauncherTeaserOptions as j, AiFormContextOptions as k, AiChatActionItem as l, AiChatExtensionContext as m, AiAnswerCardFact as n, AiAnswerCardTable as o, AiChatEventMap as p, AiAnswerCardLabels as r, AiCardImage as s, AiAnswerCard as t, AiChatBinding as u, AiChatMessage as v, AiChatSessionOptions as w, AiChatRenderer as x, AiChatMode as y };
|
package/dist/dom.js
CHANGED
|
@@ -58,5 +58,19 @@ function createPart(document, tag, block, part, text) {
|
|
|
58
58
|
if (text) element.textContent = text;
|
|
59
59
|
return element;
|
|
60
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* An attribute's JSON, or undefined when it is missing. A malformed value is reported and read as
|
|
63
|
+
* missing, so one bad attribute does not stop the rest of the chat from working.
|
|
64
|
+
*/
|
|
65
|
+
function readJsonAttribute(element, name, onError) {
|
|
66
|
+
const value = element.getAttribute(name);
|
|
67
|
+
if (!value) return;
|
|
68
|
+
try {
|
|
69
|
+
return JSON.parse(value);
|
|
70
|
+
} catch {
|
|
71
|
+
onError?.(/* @__PURE__ */ new Error(`AI chat could not read ${name} as JSON`));
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
61
75
|
//#endregion
|
|
62
|
-
export { isSafeLinkUrl as a, isPlainObject as i, createSafeImage as n, createSafeLink as r, createPart as t };
|
|
76
|
+
export { isSafeLinkUrl as a, isPlainObject as i, createSafeImage as n, readJsonAttribute as o, createSafeLink as r, createPart as t };
|