@vettvangur/ai-sdk 0.14.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -11
- package/dist/binmx.d.ts +1 -1
- package/dist/chat.d.ts +132 -0
- package/dist/chat.js +33 -0
- package/dist/contracts.d.ts +16 -9
- package/dist/dom.d.ts +20 -0
- package/dist/events.js +76 -0
- package/dist/index.d.ts +15 -138
- package/dist/index.js +3 -33
- package/dist/live-chat/aichat.d.ts +1 -1
- package/dist/markdown.js +230 -0
- package/dist/mount.d.ts +1 -1
- package/dist/omnichannel.js +1 -0
- package/dist/styles.css +43 -2
- package/dist/survey.js +3 -230
- package/dist/window.js +59 -81
- package/package.json +6 -2
package/dist/window.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { a as isSafeLinkUrl, i as isPlainObject, o as readJsonAttribute, r as createSafeLink, t as createPart } from "./dom.js";
|
|
2
|
-
import {
|
|
2
|
+
import { t as readServerEvents } from "./events.js";
|
|
3
|
+
import { n as countMarkdownStructuralItems, r as renderMarkdown } from "./markdown.js";
|
|
4
|
+
import { n as renderSurveyForm } from "./survey.js";
|
|
3
5
|
//#region src/answer-card.ts
|
|
4
6
|
const ANSWER_CARD_KEYS = /* @__PURE__ */ new Set([
|
|
5
7
|
"kind",
|
|
@@ -422,6 +424,7 @@ function renderActions(document, items) {
|
|
|
422
424
|
const AI_CHAT_PROTOCOL_VERSION = 1;
|
|
423
425
|
const MAX_TURN_TOKEN_LENGTH = 1024;
|
|
424
426
|
const MAX_SIGNATURE_LENGTH = 1024;
|
|
427
|
+
const MAX_FOLLOW_UP_ACTION_LENGTH = 4096;
|
|
425
428
|
function isRecord(value) {
|
|
426
429
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
427
430
|
}
|
|
@@ -490,9 +493,23 @@ function parseStreamEvent(name, data) {
|
|
|
490
493
|
case "suggestions": {
|
|
491
494
|
const suggestions = data.suggestions;
|
|
492
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);
|
|
493
507
|
return {
|
|
494
508
|
name,
|
|
495
|
-
data: {
|
|
509
|
+
data: {
|
|
510
|
+
suggestions: valid.map((followUp) => followUp.text),
|
|
511
|
+
followUps: valid
|
|
512
|
+
}
|
|
496
513
|
};
|
|
497
514
|
}
|
|
498
515
|
case "done": {
|
|
@@ -517,81 +534,22 @@ function parseStreamEvent(name, data) {
|
|
|
517
534
|
}
|
|
518
535
|
//#endregion
|
|
519
536
|
//#region src/stream.ts
|
|
520
|
-
|
|
521
|
-
function findRecordBoundary(buffer) {
|
|
522
|
-
const lfIndex = buffer.indexOf("\n\n");
|
|
523
|
-
const crlfIndex = buffer.indexOf("\r\n\r\n");
|
|
524
|
-
if (lfIndex === -1 && crlfIndex === -1) return null;
|
|
525
|
-
if (lfIndex === -1 || crlfIndex !== -1 && crlfIndex < lfIndex) return {
|
|
526
|
-
index: crlfIndex,
|
|
527
|
-
length: 4
|
|
528
|
-
};
|
|
529
|
-
return {
|
|
530
|
-
index: lfIndex,
|
|
531
|
-
length: 2
|
|
532
|
-
};
|
|
533
|
-
}
|
|
534
|
-
function fieldValue(value) {
|
|
535
|
-
return value.startsWith(" ") ? value.slice(1) : value;
|
|
536
|
-
}
|
|
537
|
-
function parseRecord(record) {
|
|
538
|
-
let name;
|
|
539
|
-
const dataLines = [];
|
|
540
|
-
for (const line of record.split(/\r?\n/)) if (line.startsWith("event:")) name = fieldValue(line.slice(6));
|
|
541
|
-
else if (line.startsWith("data:")) dataLines.push(fieldValue(line.slice(5)));
|
|
542
|
-
if (!name || dataLines.length === 0) return null;
|
|
537
|
+
function parseEvent(name, data) {
|
|
543
538
|
try {
|
|
544
|
-
return parseStreamEvent(name, JSON.parse(
|
|
539
|
+
return parseStreamEvent(name, JSON.parse(data));
|
|
545
540
|
} catch {
|
|
546
541
|
return null;
|
|
547
542
|
}
|
|
548
543
|
}
|
|
549
|
-
function abortError$1() {
|
|
550
|
-
return /* @__PURE__ */ new Error("AI chat event stream was aborted");
|
|
551
|
-
}
|
|
552
544
|
async function* readEventStream(response, signal) {
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
let readerDone = false;
|
|
559
|
-
const cancelReader = () => {
|
|
560
|
-
reader.cancel().catch(() => void 0);
|
|
561
|
-
};
|
|
562
|
-
signal.addEventListener("abort", cancelReader, { once: true });
|
|
563
|
-
try {
|
|
564
|
-
while (true) {
|
|
565
|
-
if (signal.aborted) throw abortError$1();
|
|
566
|
-
const { done, value } = await reader.read();
|
|
567
|
-
if (signal.aborted) throw abortError$1();
|
|
568
|
-
if (done) {
|
|
569
|
-
readerDone = true;
|
|
570
|
-
buffer += decoder.decode();
|
|
571
|
-
} else buffer += decoder.decode(value, { stream: true });
|
|
572
|
-
let boundary = findRecordBoundary(buffer);
|
|
573
|
-
while (boundary) {
|
|
574
|
-
const event = parseRecord(buffer.slice(0, boundary.index));
|
|
575
|
-
buffer = buffer.slice(boundary.index + boundary.length);
|
|
576
|
-
if (event) {
|
|
577
|
-
if (event.name === "done" || event.name === "error") {
|
|
578
|
-
yield event;
|
|
579
|
-
return;
|
|
580
|
-
}
|
|
581
|
-
yield event;
|
|
582
|
-
}
|
|
583
|
-
boundary = findRecordBoundary(buffer);
|
|
584
|
-
}
|
|
585
|
-
if (buffer.length > MAX_RECORD_LENGTH) throw new Error("AI chat event stream sent an event that is too large");
|
|
586
|
-
if (readerDone) throw new Error("AI chat event stream ended before a terminal event");
|
|
545
|
+
for await (const record of readServerEvents(response, signal)) {
|
|
546
|
+
const event = parseEvent(record.name, record.data);
|
|
547
|
+
if (event) {
|
|
548
|
+
yield event;
|
|
549
|
+
if (event.name === "done" || event.name === "error") return;
|
|
587
550
|
}
|
|
588
|
-
} finally {
|
|
589
|
-
signal.removeEventListener("abort", cancelReader);
|
|
590
|
-
if (!readerDone) try {
|
|
591
|
-
await reader.cancel();
|
|
592
|
-
} catch {}
|
|
593
|
-
reader.releaseLock();
|
|
594
551
|
}
|
|
552
|
+
throw new Error("AI chat event stream ended before a terminal event");
|
|
595
553
|
}
|
|
596
554
|
//#endregion
|
|
597
555
|
//#region src/chat.ts
|
|
@@ -632,7 +590,7 @@ function createChatSession(options) {
|
|
|
632
590
|
get status() {
|
|
633
591
|
return status;
|
|
634
592
|
},
|
|
635
|
-
async send(message) {
|
|
593
|
+
async send(message, sendOptions) {
|
|
636
594
|
const content = message.trim();
|
|
637
595
|
if (!content) throw new Error("AI chat messages cannot be empty");
|
|
638
596
|
if (status === "disposed") throw new Error("AI chat session is disposed");
|
|
@@ -663,7 +621,8 @@ function createChatSession(options) {
|
|
|
663
621
|
body: JSON.stringify({
|
|
664
622
|
messages: requestHistory,
|
|
665
623
|
provider: options.provider,
|
|
666
|
-
context
|
|
624
|
+
context,
|
|
625
|
+
followUpAction: sendOptions?.followUpAction ?? void 0
|
|
667
626
|
}),
|
|
668
627
|
signal: requestController.signal
|
|
669
628
|
});
|
|
@@ -1208,7 +1167,7 @@ function bindChat(root, options = {}) {
|
|
|
1208
1167
|
activeEntryText = null;
|
|
1209
1168
|
}
|
|
1210
1169
|
} else if (event.name === "tool_done") finishTool(event.data.id, event.data.isError);
|
|
1211
|
-
else if (event.name === "suggestions") followUps = event.data.
|
|
1170
|
+
else if (event.name === "suggestions") followUps = event.data.followUps;
|
|
1212
1171
|
else if (event.name === "done" || event.name === "error") clearPendingTool();
|
|
1213
1172
|
});
|
|
1214
1173
|
function setBusy(busy) {
|
|
@@ -1237,12 +1196,28 @@ function bindChat(root, options = {}) {
|
|
|
1237
1196
|
const button = root.ownerDocument.createElement("button");
|
|
1238
1197
|
button.type = "button";
|
|
1239
1198
|
button.className = part("follow-up");
|
|
1240
|
-
button.dataset.aiChatSuggestion = suggestion;
|
|
1241
|
-
button.
|
|
1199
|
+
button.dataset.aiChatSuggestion = suggestion.text;
|
|
1200
|
+
if (suggestion.action) button.dataset.aiChatFollowUpAction = suggestion.action;
|
|
1201
|
+
button.append(root.ownerDocument.createTextNode(suggestion.text));
|
|
1242
1202
|
list.append(button);
|
|
1243
1203
|
}
|
|
1244
1204
|
parent.append(list);
|
|
1245
1205
|
}
|
|
1206
|
+
const announcer = root.ownerDocument.createElement("div");
|
|
1207
|
+
announcer.className = part("announcer");
|
|
1208
|
+
announcer.dataset.aiChatAnnouncer = "true";
|
|
1209
|
+
announcer.setAttribute("role", "status");
|
|
1210
|
+
announcer.setAttribute("aria-live", "polite");
|
|
1211
|
+
announcer.setAttribute("aria-atomic", "true");
|
|
1212
|
+
root.append(announcer);
|
|
1213
|
+
cleanups.push(() => announcer.remove());
|
|
1214
|
+
function announce(message) {
|
|
1215
|
+
announcer.textContent = "";
|
|
1216
|
+
const text = answerText(message);
|
|
1217
|
+
if (text) requestAnimationFrame(() => {
|
|
1218
|
+
announcer.textContent = text;
|
|
1219
|
+
});
|
|
1220
|
+
}
|
|
1246
1221
|
function appendReplyActions(parent, turn) {
|
|
1247
1222
|
const template = root.querySelector("template[data-ai-chat-reply-actions]");
|
|
1248
1223
|
if (!template) return;
|
|
@@ -1253,11 +1228,13 @@ function bindChat(root, options = {}) {
|
|
|
1253
1228
|
else for (const button of actions.querySelectorAll("[data-ai-chat-feedback]")) button.remove();
|
|
1254
1229
|
if (actions.children.length) parent.append(actions);
|
|
1255
1230
|
}
|
|
1231
|
+
function answerText(message) {
|
|
1232
|
+
return Array.from(message?.querySelectorAll("[data-ai-chat-text], [data-ai-chat-card]") ?? [], (element) => (element.innerText ?? element.textContent ?? "").trim()).filter(Boolean).join("\n\n");
|
|
1233
|
+
}
|
|
1256
1234
|
async function copyReply(button) {
|
|
1257
|
-
const
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
await navigator.clipboard.writeText(parts.join("\n\n"));
|
|
1235
|
+
const text = answerText(button.closest("[data-ai-chat-message]"));
|
|
1236
|
+
if (!text) return;
|
|
1237
|
+
await navigator.clipboard.writeText(text);
|
|
1261
1238
|
button.dataset.aiChatCopied = "true";
|
|
1262
1239
|
setTimeout(() => delete button.dataset.aiChatCopied, 2e3);
|
|
1263
1240
|
}
|
|
@@ -1699,7 +1676,7 @@ function bindChat(root, options = {}) {
|
|
|
1699
1676
|
scrollToEnd();
|
|
1700
1677
|
await live.provider.send(text);
|
|
1701
1678
|
}
|
|
1702
|
-
const submit = async (event) => {
|
|
1679
|
+
const submit = async (event, followUpAction) => {
|
|
1703
1680
|
event?.preventDefault();
|
|
1704
1681
|
const question = input.value.trim();
|
|
1705
1682
|
if (!question || mode === "handover") return;
|
|
@@ -1734,7 +1711,7 @@ function bindChat(root, options = {}) {
|
|
|
1734
1711
|
setBusy(true);
|
|
1735
1712
|
const assistant = activeAssistant;
|
|
1736
1713
|
const entry = activeEntry;
|
|
1737
|
-
const result = await session.send(question);
|
|
1714
|
+
const result = await session.send(question, { followUpAction });
|
|
1738
1715
|
if (destroyed) return;
|
|
1739
1716
|
setBusy(false);
|
|
1740
1717
|
entry.turn = result.turn;
|
|
@@ -1748,6 +1725,7 @@ function bindChat(root, options = {}) {
|
|
|
1748
1725
|
if (!result.error && assistant.isConnected && assistant.childNodes.length) {
|
|
1749
1726
|
appendReplyActions(assistant, result.turn);
|
|
1750
1727
|
appendFollowUps(assistant, followUps);
|
|
1728
|
+
announce(assistant);
|
|
1751
1729
|
}
|
|
1752
1730
|
focusInput();
|
|
1753
1731
|
if (result.canceled) activeAssistant?.querySelectorAll(`[data-ai-chat-tool]:not(.${part("tool--done")})`).forEach((item) => {
|
|
@@ -1759,7 +1737,7 @@ function bindChat(root, options = {}) {
|
|
|
1759
1737
|
const suggestion = target?.closest("[data-ai-chat-suggestion]");
|
|
1760
1738
|
if (suggestion) {
|
|
1761
1739
|
input.value = suggestion.dataset.aiChatSuggestion ?? suggestion.textContent?.trim() ?? "";
|
|
1762
|
-
submit();
|
|
1740
|
+
submit(void 0, suggestion.dataset.aiChatFollowUpAction);
|
|
1763
1741
|
return;
|
|
1764
1742
|
}
|
|
1765
1743
|
if (target?.closest("[data-ai-chat-stop]")) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vettvangur/ai-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Framework-agnostic AI chat
|
|
3
|
+
"version": "0.16.0",
|
|
4
|
+
"description": "Framework-agnostic AI client primitives, and an AI chat for server-rendered shells",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"access": "public",
|
|
7
7
|
"author": "Vettvangur",
|
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
"types": "./dist/index.d.ts",
|
|
19
19
|
"import": "./dist/index.js"
|
|
20
20
|
},
|
|
21
|
+
"./chat": {
|
|
22
|
+
"types": "./dist/chat.d.ts",
|
|
23
|
+
"import": "./dist/chat.js"
|
|
24
|
+
},
|
|
21
25
|
"./live-chat": {
|
|
22
26
|
"types": "./dist/live-chat.d.ts",
|
|
23
27
|
"import": "./dist/live-chat.js"
|