@vettvangur/ai-sdk 0.13.0 → 0.15.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 +59 -12
- 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 +2 -8
- 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/livechat.js +3 -1
- package/dist/markdown.js +230 -0
- package/dist/mount.d.ts +1 -1
- package/dist/omnichannel.js +3 -1
- package/dist/styles.css +42 -3
- package/dist/survey.js +3 -230
- package/dist/window.js +22 -75
- 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",
|
|
@@ -429,6 +431,7 @@ function stringValue(data, key) {
|
|
|
429
431
|
const value = data[key];
|
|
430
432
|
return typeof value === "string" ? value : null;
|
|
431
433
|
}
|
|
434
|
+
const MAX_TOOL_LABEL_LENGTH = 120;
|
|
432
435
|
function signatureEntry(data, key) {
|
|
433
436
|
const value = stringValue(data, key);
|
|
434
437
|
return value && value.length <= MAX_SIGNATURE_LENGTH ? { [key]: value } : {};
|
|
@@ -446,11 +449,14 @@ function parseStreamEvent(name, data) {
|
|
|
446
449
|
case "tool": {
|
|
447
450
|
const id = stringValue(data, "id");
|
|
448
451
|
const toolName = stringValue(data, "name");
|
|
449
|
-
|
|
452
|
+
if (id === null || toolName === null) return null;
|
|
453
|
+
const label = stringValue(data, "label");
|
|
454
|
+
return {
|
|
450
455
|
name,
|
|
451
456
|
data: {
|
|
452
457
|
id,
|
|
453
|
-
name: toolName
|
|
458
|
+
name: toolName,
|
|
459
|
+
...label ? { label: label.slice(0, MAX_TOOL_LABEL_LENGTH) } : {}
|
|
454
460
|
}
|
|
455
461
|
};
|
|
456
462
|
}
|
|
@@ -513,81 +519,22 @@ function parseStreamEvent(name, data) {
|
|
|
513
519
|
}
|
|
514
520
|
//#endregion
|
|
515
521
|
//#region src/stream.ts
|
|
516
|
-
|
|
517
|
-
function findRecordBoundary(buffer) {
|
|
518
|
-
const lfIndex = buffer.indexOf("\n\n");
|
|
519
|
-
const crlfIndex = buffer.indexOf("\r\n\r\n");
|
|
520
|
-
if (lfIndex === -1 && crlfIndex === -1) return null;
|
|
521
|
-
if (lfIndex === -1 || crlfIndex !== -1 && crlfIndex < lfIndex) return {
|
|
522
|
-
index: crlfIndex,
|
|
523
|
-
length: 4
|
|
524
|
-
};
|
|
525
|
-
return {
|
|
526
|
-
index: lfIndex,
|
|
527
|
-
length: 2
|
|
528
|
-
};
|
|
529
|
-
}
|
|
530
|
-
function fieldValue(value) {
|
|
531
|
-
return value.startsWith(" ") ? value.slice(1) : value;
|
|
532
|
-
}
|
|
533
|
-
function parseRecord(record) {
|
|
534
|
-
let name;
|
|
535
|
-
const dataLines = [];
|
|
536
|
-
for (const line of record.split(/\r?\n/)) if (line.startsWith("event:")) name = fieldValue(line.slice(6));
|
|
537
|
-
else if (line.startsWith("data:")) dataLines.push(fieldValue(line.slice(5)));
|
|
538
|
-
if (!name || dataLines.length === 0) return null;
|
|
522
|
+
function parseEvent(name, data) {
|
|
539
523
|
try {
|
|
540
|
-
return parseStreamEvent(name, JSON.parse(
|
|
524
|
+
return parseStreamEvent(name, JSON.parse(data));
|
|
541
525
|
} catch {
|
|
542
526
|
return null;
|
|
543
527
|
}
|
|
544
528
|
}
|
|
545
|
-
function abortError$1() {
|
|
546
|
-
return /* @__PURE__ */ new Error("AI chat event stream was aborted");
|
|
547
|
-
}
|
|
548
529
|
async function* readEventStream(response, signal) {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
let readerDone = false;
|
|
555
|
-
const cancelReader = () => {
|
|
556
|
-
reader.cancel().catch(() => void 0);
|
|
557
|
-
};
|
|
558
|
-
signal.addEventListener("abort", cancelReader, { once: true });
|
|
559
|
-
try {
|
|
560
|
-
while (true) {
|
|
561
|
-
if (signal.aborted) throw abortError$1();
|
|
562
|
-
const { done, value } = await reader.read();
|
|
563
|
-
if (signal.aborted) throw abortError$1();
|
|
564
|
-
if (done) {
|
|
565
|
-
readerDone = true;
|
|
566
|
-
buffer += decoder.decode();
|
|
567
|
-
} else buffer += decoder.decode(value, { stream: true });
|
|
568
|
-
let boundary = findRecordBoundary(buffer);
|
|
569
|
-
while (boundary) {
|
|
570
|
-
const event = parseRecord(buffer.slice(0, boundary.index));
|
|
571
|
-
buffer = buffer.slice(boundary.index + boundary.length);
|
|
572
|
-
if (event) {
|
|
573
|
-
if (event.name === "done" || event.name === "error") {
|
|
574
|
-
yield event;
|
|
575
|
-
return;
|
|
576
|
-
}
|
|
577
|
-
yield event;
|
|
578
|
-
}
|
|
579
|
-
boundary = findRecordBoundary(buffer);
|
|
580
|
-
}
|
|
581
|
-
if (buffer.length > MAX_RECORD_LENGTH) throw new Error("AI chat event stream sent an event that is too large");
|
|
582
|
-
if (readerDone) throw new Error("AI chat event stream ended before a terminal event");
|
|
530
|
+
for await (const record of readServerEvents(response, signal)) {
|
|
531
|
+
const event = parseEvent(record.name, record.data);
|
|
532
|
+
if (event) {
|
|
533
|
+
yield event;
|
|
534
|
+
if (event.name === "done" || event.name === "error") return;
|
|
583
535
|
}
|
|
584
|
-
} finally {
|
|
585
|
-
signal.removeEventListener("abort", cancelReader);
|
|
586
|
-
if (!readerDone) try {
|
|
587
|
-
await reader.cancel();
|
|
588
|
-
} catch {}
|
|
589
|
-
reader.releaseLock();
|
|
590
536
|
}
|
|
537
|
+
throw new Error("AI chat event stream ended before a terminal event");
|
|
591
538
|
}
|
|
592
539
|
//#endregion
|
|
593
540
|
//#region src/chat.ts
|
|
@@ -1097,11 +1044,11 @@ function bindChat(root, options = {}) {
|
|
|
1097
1044
|
if (name in labels) return labels[name];
|
|
1098
1045
|
return options.labels?.[name] || root.getAttribute(`data-ai-chat-label-${kebab(name)}`) || "";
|
|
1099
1046
|
}
|
|
1100
|
-
function toolLabel(name) {
|
|
1047
|
+
function toolLabel(name, sent) {
|
|
1101
1048
|
if (!/^[\w-]{1,64}$/.test(name)) return labels.toolRunning;
|
|
1102
|
-
return options.toolLabels?.[name] || root.getAttribute(`data-ai-chat-tool-${kebab(name)}`) || labels.toolRunning;
|
|
1049
|
+
return options.toolLabels?.[name] || root.getAttribute(`data-ai-chat-tool-${kebab(name)}`) || sent || labels.toolRunning;
|
|
1103
1050
|
}
|
|
1104
|
-
function tool(id, name) {
|
|
1051
|
+
function tool(id, name, sent) {
|
|
1105
1052
|
const element = root.ownerDocument.createElement("div");
|
|
1106
1053
|
element.dataset.aiChatTool = id;
|
|
1107
1054
|
element.className = part("tool");
|
|
@@ -1110,7 +1057,7 @@ function bindChat(root, options = {}) {
|
|
|
1110
1057
|
dot.setAttribute("aria-hidden", "true");
|
|
1111
1058
|
const label = root.ownerDocument.createElement("span");
|
|
1112
1059
|
label.className = part("tool-label");
|
|
1113
|
-
label.append(root.ownerDocument.createTextNode(toolLabel(name)));
|
|
1060
|
+
label.append(root.ownerDocument.createTextNode(toolLabel(name, sent)));
|
|
1114
1061
|
const status = root.ownerDocument.createElement("span");
|
|
1115
1062
|
status.className = part("tool-status");
|
|
1116
1063
|
element.append(dot, label, status);
|
|
@@ -1187,7 +1134,7 @@ function bindChat(root, options = {}) {
|
|
|
1187
1134
|
}
|
|
1188
1135
|
} else if (event.name === "tool") {
|
|
1189
1136
|
clearPendingTool();
|
|
1190
|
-
tool(event.data.id, event.data.name);
|
|
1137
|
+
tool(event.data.id, event.data.name, event.data.label);
|
|
1191
1138
|
} else if (event.name === "tool_result") {
|
|
1192
1139
|
clearPendingTool();
|
|
1193
1140
|
if (activeAssistant) {
|
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.15.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"
|