@vettvangur/ai-sdk 0.5.4 → 0.7.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/dist/index.d.ts CHANGED
@@ -1,275 +1,5 @@
1
1
  import { a as AiLiveChatProvider, i as AiLiveChatMessage, n as AiLiveChatAvailability, o as AiLiveChatStartOptions, s as AiLiveChatSurveyField, 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
- liveChatConnecting: string;
127
- liveChatClosed: string;
128
- liveChatEnded: string;
129
- liveChatFailed: string;
130
- liveChatTyping: string;
131
- liveChatAgent: string;
132
- /** A divider once the chat has started. */
133
- liveChatStarted: string;
134
- /** A divider when the agent joins; `{name}` is their first name. */
135
- liveChatJoined: string;
136
- /** A divider when the agent ends the chat; `{name}` is their first name. */
137
- liveChatAgentEnded: string;
138
- /** The card shown while the visitor waits for an agent, and the text under it when not empty. */
139
- liveChatWaiting: string;
140
- liveChatWaitingText: string;
141
- /** Shown after the agent's name above their messages when not empty, such as their team. */
142
- liveChatTeam: string;
143
- surveySubmit: string;
144
- surveyCancel: string;
145
- surveyRequired: string;
146
- surveyInvalidEmail: string;
147
- /** Above the survey's fields, each left out when empty. */
148
- surveyTitle: string;
149
- surveyIntro: string;
150
- /** Under a field the host's prefill filled in, when not empty. */
151
- surveyPrefilled: string;
152
- error: string;
153
- timeout: string;
154
- toolRunning: string;
155
- toolComplete: string;
156
- toolFailed: string;
157
- toolInterrupted: string;
158
- };
159
- type AiChatExtensionContext = {
160
- document: Document;
161
- root: HTMLElement;
162
- tool: string;
163
- data: unknown;
164
- reply: string | null;
165
- labels: AiChatLabels;
166
- };
167
- type AiChatRenderer = (context: AiChatExtensionContext) => Node | null;
168
- type AiChatSurface = (context: AiChatExtensionContext) => Node | null;
169
- type AiChatActionContext = {
170
- root: HTMLElement;
171
- element: HTMLElement;
172
- value: string | null;
173
- session: AiChatSession;
174
- };
175
- type AiChatAction = (context: AiChatActionContext) => undefined | {
176
- context: [string, string];
177
- } | Promise<undefined | {
178
- context: [string, string];
179
- }>;
180
- type AiChatRegistry = {
181
- registerRenderer(name: string, renderer: AiChatRenderer): () => void;
182
- registerSurface(name: string, surface: AiChatSurface): () => void;
183
- registerAction(name: string, action: AiChatAction): () => void;
184
- };
185
- type AiChatBindings = {
186
- endpoint?: string;
187
- provider?: string;
188
- antiforgeryHeader?: string;
189
- antiforgeryToken?: string;
190
- timeoutMs?: number;
191
- labels?: Partial<AiChatLabels>;
192
- toolLabels?: Record<string, string>;
193
- feedbackEndpoint?: string;
194
- /** Where `handover()` asks the server for the transcript and context to give the agent. */
195
- handoverEndpoint?: string;
196
- /** Keeps the thread in sessionStorage under this key, so it carries on across page loads. */
197
- storageKey?: string;
198
- /** The provider a stored live chat reconnects through, and `handover()` uses by default. */
199
- liveChat?: AiLiveChatProvider;
200
- offsetElement?: HTMLElement | null;
201
- registry?: AiChatRegistry;
202
- onEmbedded?: (element: HTMLElement) => void;
203
- onError?: (error: Error) => void;
204
- };
205
- /** Who the composer talks to: the AI, a handover on its way, or a person. */
206
- type AiChatMode = 'ai' | 'handover' | 'live';
207
- type AiChatHandoverOptions = {
208
- provider?: AiLiveChatProvider;
209
- /** Values for the agent, over those the handover endpoint returns. */
210
- context?: Record<string, string>;
211
- /** Survey answers to start with, by field name or id. */
212
- prefill?: AiLiveChatAnswers;
213
- };
214
- type AiChatHandoverResult = {
215
- status: 'live';
216
- } | {
217
- status: 'closed';
218
- reason: 'out-of-hours' | 'no-agents';
219
- } | {
220
- status: 'canceled';
221
- } | {
222
- status: 'failed';
223
- error: Error;
224
- };
225
- type AiChatEventMap = {
226
- open: undefined;
227
- close: undefined;
228
- handover: undefined;
229
- /** An agent took the chat; `name` is their full display name when the provider has one. */
230
- agent: {
231
- name: string | null;
232
- };
233
- end: {
234
- reason: 'agent' | 'visitor' | 'disconnected';
235
- };
236
- };
237
- type AiChatBinding = {
238
- session: AiChatSession;
239
- labels: Readonly<AiChatLabels>;
240
- readonly mode: AiChatMode;
241
- readonly isOpen: boolean;
242
- /** The provider of the live chat in progress, so a host can offer its optional capabilities. */
243
- readonly liveChat: AiLiveChatProvider | null;
244
- appendAssistantReply(text: string): void;
245
- setFormContext(name: string, summary: string, followUp?: string | null): void;
246
- open(): void;
247
- close(): void;
248
- handover(options?: AiChatHandoverOptions): Promise<AiChatHandoverResult>;
249
- endLiveChat(): Promise<void>;
250
- on<K extends keyof AiChatEventMap>(name: K, listener: (detail: AiChatEventMap[K]) => void): () => void;
251
- destroy(): void;
252
- };
253
- type AiCardImage = {
254
- url: string;
255
- url2x?: string | null;
256
- alt?: string | null;
257
- width?: number | null;
258
- height?: number | null;
259
- };
260
- type AiChatActionItem = {
261
- label: string;
262
- action?: string | null;
263
- value?: string | null;
264
- url?: string | null;
265
- primary?: boolean;
266
- };
267
- type AiTemplateItemSurfaceOptions = {
268
- template: string;
269
- attribute: string;
270
- key: string;
271
- };
272
- //#endregion
2
+ import { A as AiHandoverPayload, C as AiChatSession, D as AiChatStreamEvent, E as AiChatSessionStatus, M as AiLiveChatFactoryContext, N as AiTemplateItemSurfaceOptions, O as AiChatSurface, P as AiTemplateUrlSurfaceOptions, S as AiChatRequest, T as AiChatSessionResult, _ as AiChatLabels, a as AiAnswerCardSection, b as AiChatRegistry, c as AiChatAction, d as AiChatBindings, f as AiChatContext, g as AiChatHandoverResult, h as AiChatHandoverOptions, i as AiAnswerCardLink, j as AiLauncherTeaserOptions, k as AiFormContextOptions, l as AiChatActionItem, m as AiChatExtensionContext, n as AiAnswerCardFact, o as AiAnswerCardTable, p as AiChatEventMap, r as AiAnswerCardLabels, s as AiCardImage, t as AiAnswerCard, u as AiChatBinding, v as AiChatMessage, w as AiChatSessionOptions, x as AiChatRenderer, y as AiChatMode } from "./contracts.js";
273
3
  //#region src/answer-card.d.ts
274
4
  declare const MAX_ANSWER_CARD_SECTIONS = 12;
275
5
  declare const MAX_ANSWER_CARD_FACTS = 24;
@@ -293,6 +23,12 @@ declare const ANSWER_CARD_CLASS = "vv-ai-answer-card";
293
23
  */
294
24
  declare function isAnswerCard(value: unknown): value is AiAnswerCard;
295
25
  declare function renderAnswerCard(document: Document, data: unknown, labels: AiAnswerCardLabels): HTMLElement | null;
26
+ /**
27
+ * Throws when `card` is not an answer card `renderAnswerCard` will draw, or when a field it shows
28
+ * as plain text (titles, facts, table cells, notes) holds Markdown that would show literally. For
29
+ * a server's or a test's own cards; the message names what is wrong.
30
+ */
31
+ declare function assertAnswerCardContract(card: unknown): asserts card is AiAnswerCard;
296
32
  //#endregion
297
33
  //#region src/bind.d.ts
298
34
  /**
@@ -332,6 +68,28 @@ declare function createSafeImage(document: Document, image: AiCardImage | null |
332
68
  /** An element carrying the BEM class for `part` of `block`, and `text` when there is any. */
333
69
  declare function createPart<K extends keyof HTMLElementTagNameMap>(document: Document, tag: K, block: string, part?: string | null, text?: string | null): HTMLElementTagNameMap[K];
334
70
  //#endregion
71
+ //#region src/form-context.d.ts
72
+ /**
73
+ * Tells the AI what the visitor entered into a form embedded in the thread. Whenever `element`
74
+ * changes and holds `options.form`, its filled-in `fields` are summarised as `label: value` and
75
+ * sent with every later question through `binding.setFormContext`, framed as context rather than
76
+ * something the visitor said. Returns a function that stops watching.
77
+ */
78
+ declare function watchFormContext(binding: AiChatBinding, element: HTMLElement, options: AiFormContextOptions): () => void;
79
+ //#endregion
80
+ //#region src/handover.d.ts
81
+ /** The action the handover card's button runs; the binding hands over to a person with it. */
82
+ declare const LIVE_CHAT_ACTION = "livechat";
83
+ /** The tool the Vettvangur.AiChat engine hands over with, from before its payload named its kind. */
84
+ declare const HANDOVER_TOOL = "request_human_agent";
85
+ /** A payload of kind `handover`, or one from the handover tool that names no kind. */
86
+ declare function isHandoverPayload(tool: string, data: unknown): data is AiHandoverPayload;
87
+ /**
88
+ * The visitor's ways to a person: a primary `liveChat` button that runs the `livechat` action when
89
+ * a live chat is available, and a `contact` link to a safe contact URL. Null when neither applies.
90
+ */
91
+ declare function renderHandoverCard(document: Document, data: AiHandoverPayload, labels: Pick<AiChatLabels, 'liveChat' | 'contact'>): HTMLElement | null;
92
+ //#endregion
335
93
  //#region src/protocol.d.ts
336
94
  declare const AI_CHAT_PROTOCOL_VERSION = 1;
337
95
  declare function parseStreamEvent(name: string, data: unknown): AiChatStreamEvent | null;
@@ -349,6 +107,30 @@ declare function readEventStream(response: Response, signal: AbortSignal): Async
349
107
  * rest of the template stays as authored, so one item is the same markup the whole list shows.
350
108
  * Null when the template is missing or no item matches.
351
109
  */
352
- declare function createTemplateItemSurface(options: AiTemplateItemSurfaceOptions): (context: AiChatExtensionContext) => DocumentFragment | null;
110
+ declare function createTemplateItemSurface(options: AiTemplateItemSurfaceOptions): (context: Pick<AiChatExtensionContext, 'root' | 'data'>) => DocumentFragment | null;
111
+ /**
112
+ * A surface of which only the latest copy stays in the thread, for an embedded component whose
113
+ * script binds to the first match in the document or whose fields share ids. What it draws is
114
+ * marked with `data-ai-chat-single` under `key`, and drawing it again removes the earlier copies.
115
+ */
116
+ declare function createSingleSurface<C extends Pick<AiChatExtensionContext, 'root'>, N extends Node>(key: string, surface: (context: C) => N | null): (context: C) => N | null;
117
+ //#endregion
118
+ //#region src/window.d.ts
119
+ /**
120
+ * The binding as a window over the page. Anywhere in the document, a `data-ai-chat-window-open`
121
+ * element opens it, and one that is also `data-ai-chat-window-toggle` closes it again; toggles
122
+ * carry `aria-expanded` and get focus back when it closes. Escape closes the root's
123
+ * `details[data-ai-chat-menu]` first and then the window, a click anywhere but the menu's summary
124
+ * closes the menu, and a `data-ai-chat-expand` link closes the window before it navigates, so the
125
+ * next page does not open it again from the stored thread. Returns a function that undoes it all.
126
+ */
127
+ declare function bindChatWindow(binding: AiChatBinding): () => void;
128
+ /**
129
+ * The `data-ai-launcher-teaser` inside `container`, shown once a session to a visitor who has not
130
+ * started a chat: not while the window is open or its thread has messages. A
131
+ * `data-ai-launcher-dismiss` button in it hides it, as opening the window does. When storage
132
+ * cannot be read the teaser is not shown. Returns a function that undoes it.
133
+ */
134
+ declare function bindLauncherTeaser(binding: AiChatBinding, container: ParentNode, options?: AiLauncherTeaserOptions): () => void;
353
135
  //#endregion
354
- 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 AiLiveChatAnswers, type AiLiveChatAvailability, type AiLiveChatMessage, type AiLiveChatProvider, type AiLiveChatStartOptions, type AiLiveChatSurveyField, type AiTemplateItemSurfaceOptions, CARD_LIST_CLASS, CHAT_CLASS, 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, bindChat, createAiChatRegistry, createChatSession, createPart, createSafeImage, createSafeLink, createTemplateItemSurface, isAnswerCard, isSafeLinkUrl, parseStreamEvent, readEventStream, renderActions, renderAnswerCard, renderCardList };
136
+ 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 AiLiveChatAnswers, type AiLiveChatAvailability, type AiLiveChatFactoryContext, type AiLiveChatMessage, type AiLiveChatProvider, 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, createAiChatRegistry, createChatSession, createPart, createSafeImage, createSafeLink, createSingleSurface, createTemplateItemSurface, isAnswerCard, isHandoverPayload, isSafeLinkUrl, parseStreamEvent, readEventStream, renderActions, renderAnswerCard, renderCardList, renderHandoverCard, watchFormContext };