@vettvangur/ai-sdk 0.1.13 → 0.1.14
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 +17 -0
- package/dist/index.d.ts +25 -1
- package/dist/index.js +65 -36
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,6 +38,23 @@ from authored `data-ai-action` markup and receive an optional authored `data-ai-
|
|
|
38
38
|
Neither server payloads nor model output become executable markup. Generic answer cards, links and
|
|
39
39
|
Markdown are rendered as safe DOM nodes. Host callbacks must return DOM nodes, not HTML strings.
|
|
40
40
|
|
|
41
|
+
## Answer cards
|
|
42
|
+
|
|
43
|
+
`renderAnswerCard()` draws a `kind: 'answer'` payload as DOM, or returns null when it breaks any
|
|
44
|
+
limit; the whole card is refused, not trimmed. `isAnswerCard()` applies the same check and the
|
|
45
|
+
`MAX_ANSWER_CARD_*` constants name the limits, so a server or a test can hold its cards to them.
|
|
46
|
+
|
|
47
|
+
Every element carries a BEM class under `ANSWER_CARD_CLASS` (`vv-ai-answer-card`): `__title`,
|
|
48
|
+
`__summary`, `__highlight`, `__section`, `__section-title`, `__text`, `__facts`, `__fact-label`,
|
|
49
|
+
`__fact-value`, `__table-region`, `__table-hint`, `__table`, `__table-header`, `__table-cell`,
|
|
50
|
+
`__links`, `__link`, `__note`, `__details`, `__details-summary`, `__actions`, `__sources`,
|
|
51
|
+
`__sources-title`, `__sources-list`, `__source-note`, and `--generic` on the card drawn for a payload
|
|
52
|
+
of plain fields. Style by these classes rather than by element order.
|
|
53
|
+
|
|
54
|
+
`summary` and section `text` are Markdown. Titles, facts, table cells, `note` and `sourceNote` are
|
|
55
|
+
plain text; a link belongs in `links`, `actions` or `sources`. `isSafeLinkUrl()` is the rule every
|
|
56
|
+
link passes, exported so a host's own renderers can apply it too.
|
|
57
|
+
|
|
41
58
|
The package accepts protocol-v1 events (`delta`, `tool`, `tool_result`, `tool_done`, `done`, and
|
|
42
59
|
`error`). Host-specific card rendering, embedded-component activation and styling remain host
|
|
43
60
|
responsibilities. Eimskip migration is deliberately outside this package.
|
package/dist/index.d.ts
CHANGED
|
@@ -173,6 +173,27 @@ type AiChatBinding = {
|
|
|
173
173
|
};
|
|
174
174
|
//#endregion
|
|
175
175
|
//#region src/answer-card.d.ts
|
|
176
|
+
declare const MAX_ANSWER_CARD_SECTIONS = 12;
|
|
177
|
+
declare const MAX_ANSWER_CARD_FACTS = 24;
|
|
178
|
+
declare const MAX_ANSWER_CARD_LINKS = 24;
|
|
179
|
+
declare const MAX_ANSWER_CARD_TABLES = 8;
|
|
180
|
+
declare const MAX_ANSWER_CARD_TABLE_ROWS = 50;
|
|
181
|
+
declare const MAX_ANSWER_CARD_TABLE_CELLS = 12;
|
|
182
|
+
declare const MAX_ANSWER_CARD_TEXT_LENGTH = 8000;
|
|
183
|
+
declare const MAX_ANSWER_CARD_URL_LENGTH = 2048;
|
|
184
|
+
declare const MAX_ANSWER_CARD_TOTAL_TEXT_CHARACTERS = 24000;
|
|
185
|
+
declare const MAX_ANSWER_CARD_TOTAL_STRUCTURAL_ITEMS = 512;
|
|
186
|
+
declare const MAX_ANSWER_CARD_TOTAL_TABLE_CELLS = 512;
|
|
187
|
+
/**
|
|
188
|
+
* Every element an answer card renders carries a class under this block, so a host styles the card
|
|
189
|
+
* by class rather than by the order its elements happen to come in.
|
|
190
|
+
*/
|
|
191
|
+
declare const ANSWER_CARD_CLASS = "vv-ai-answer-card";
|
|
192
|
+
/**
|
|
193
|
+
* Whether `renderAnswerCard` will draw `value` as a card. A host that builds cards elsewhere, a
|
|
194
|
+
* server or a test, can hold its output to the same limits the renderer applies.
|
|
195
|
+
*/
|
|
196
|
+
declare function isAnswerCard(value: unknown): value is AiAnswerCard;
|
|
176
197
|
declare function renderAnswerCard(document: Document, data: unknown, labels: AiAnswerCardLabels): HTMLElement | null;
|
|
177
198
|
//#endregion
|
|
178
199
|
//#region src/bind.d.ts
|
|
@@ -181,6 +202,9 @@ declare function bindChat(root: HTMLElement, options?: AiChatBindings): AiChatBi
|
|
|
181
202
|
//#region src/chat.d.ts
|
|
182
203
|
declare function createChatSession(options: AiChatSessionOptions): AiChatSession;
|
|
183
204
|
//#endregion
|
|
205
|
+
//#region src/dom.d.ts
|
|
206
|
+
declare function isSafeLinkUrl(url: string): boolean;
|
|
207
|
+
//#endregion
|
|
184
208
|
//#region src/protocol.d.ts
|
|
185
209
|
declare const AI_CHAT_PROTOCOL_VERSION = 1;
|
|
186
210
|
declare function parseStreamEvent(name: string, data: unknown): AiChatStreamEvent | null;
|
|
@@ -191,4 +215,4 @@ declare function createAiChatRegistry(): AiChatRegistry;
|
|
|
191
215
|
//#region src/stream.d.ts
|
|
192
216
|
declare function readEventStream(response: Response, signal: AbortSignal): AsyncGenerator<AiChatStreamEvent>;
|
|
193
217
|
//#endregion
|
|
194
|
-
export { AI_CHAT_PROTOCOL_VERSION, type AiAnswerCard, type AiAnswerCardFact, type AiAnswerCardLabels, type AiAnswerCardLink, type AiAnswerCardSection, type AiAnswerCardTable, type AiChatAction, type AiChatBinding, type AiChatBindings, type AiChatClasses, type AiChatContext, type AiChatExtensionContext, type AiChatMessage, type AiChatRegistry, type AiChatRenderer, type AiChatRequest, type AiChatSession, type AiChatSessionOptions, type AiChatSessionResult, type AiChatSessionStatus, type AiChatStreamEvent, type AiChatSurface, bindChat, createAiChatRegistry, createChatSession, parseStreamEvent, readEventStream, renderAnswerCard };
|
|
218
|
+
export { AI_CHAT_PROTOCOL_VERSION, ANSWER_CARD_CLASS, type AiAnswerCard, type AiAnswerCardFact, type AiAnswerCardLabels, type AiAnswerCardLink, type AiAnswerCardSection, type AiAnswerCardTable, type AiChatAction, type AiChatBinding, type AiChatBindings, type AiChatClasses, type AiChatContext, type AiChatExtensionContext, type AiChatMessage, type AiChatRegistry, type AiChatRenderer, type AiChatRequest, type AiChatSession, type AiChatSessionOptions, type AiChatSessionResult, type AiChatSessionStatus, type AiChatStreamEvent, type AiChatSurface, 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, isAnswerCard, isSafeLinkUrl, parseStreamEvent, readEventStream, renderAnswerCard };
|
package/dist/index.js
CHANGED
|
@@ -291,7 +291,22 @@ const TABLE_KEYS = /* @__PURE__ */ new Set([
|
|
|
291
291
|
"rows"
|
|
292
292
|
]);
|
|
293
293
|
const LINK_KEYS = /* @__PURE__ */ new Set(["label", "url"]);
|
|
294
|
+
const MAX_ANSWER_CARD_SECTIONS = 12;
|
|
295
|
+
const MAX_ANSWER_CARD_FACTS = 24;
|
|
296
|
+
const MAX_ANSWER_CARD_LINKS = 24;
|
|
297
|
+
const MAX_ANSWER_CARD_TABLES = 8;
|
|
298
|
+
const MAX_ANSWER_CARD_TABLE_ROWS = 50;
|
|
299
|
+
const MAX_ANSWER_CARD_TABLE_CELLS = 12;
|
|
300
|
+
const MAX_ANSWER_CARD_TEXT_LENGTH = 8e3;
|
|
301
|
+
const MAX_ANSWER_CARD_URL_LENGTH = 2048;
|
|
294
302
|
const MAX_ANSWER_CARD_TOTAL_TEXT_CHARACTERS = 24e3;
|
|
303
|
+
const MAX_ANSWER_CARD_TOTAL_STRUCTURAL_ITEMS = 512;
|
|
304
|
+
const MAX_ANSWER_CARD_TOTAL_TABLE_CELLS = 512;
|
|
305
|
+
/**
|
|
306
|
+
* Every element an answer card renders carries a class under this block, so a host styles the card
|
|
307
|
+
* by class rather than by the order its elements happen to come in.
|
|
308
|
+
*/
|
|
309
|
+
const ANSWER_CARD_CLASS = "vv-ai-answer-card";
|
|
295
310
|
function hasOnlyKeys(value, keys) {
|
|
296
311
|
let keyCount = 0;
|
|
297
312
|
for (const key in value) {
|
|
@@ -359,6 +374,10 @@ function isSection(value, budget) {
|
|
|
359
374
|
if (!isPlainObject(value) || !hasOnlyKeys(value, SECTION_KEYS) || !consumeStructuralItems(budget, 1)) return false;
|
|
360
375
|
return hasOptionalNullableText(value, "title", budget) && hasOptionalNullableMarkdown(value, "text", budget) && hasOptionalNullableArray(value, "facts", 24, (fact) => isFact(fact, budget)) && hasOptionalNullableArray(value, "tables", 8, (table) => isTable(table, budget)) && hasOptionalNullableArray(value, "links", 24, (link) => isLink(link, budget)) && hasOptionalNullableText(value, "note", budget) && hasOptionalBoolean(value, "collapsible");
|
|
361
376
|
}
|
|
377
|
+
/**
|
|
378
|
+
* Whether `renderAnswerCard` will draw `value` as a card. A host that builds cards elsewhere, a
|
|
379
|
+
* server or a test, can hold its output to the same limits the renderer applies.
|
|
380
|
+
*/
|
|
362
381
|
function isAnswerCard(value) {
|
|
363
382
|
const budget = {
|
|
364
383
|
textCharacters: 0,
|
|
@@ -367,45 +386,55 @@ function isAnswerCard(value) {
|
|
|
367
386
|
};
|
|
368
387
|
return isPlainObject(value) && hasOnlyKeys(value, ANSWER_CARD_KEYS) && value.kind === "answer" && consumeStructuralItems(budget, 1) && isAnswerCardText(value.title, budget) && hasOptionalNullableMarkdown(value, "summary", budget) && (!Object.hasOwn(value, "highlight") || value.highlight === null || isFact(value.highlight, budget) && hasOnlyKeys(value.highlight, HIGHLIGHT_KEYS)) && hasOptionalNullableArray(value, "sections", 12, (section) => isSection(section, budget)) && hasOptionalNullableText(value, "note", budget) && hasOptionalNullableArray(value, "actions", 24, (link) => isLink(link, budget)) && hasOptionalNullableArray(value, "sources", 24, (link) => isLink(link, budget)) && hasOptionalNullableText(value, "sourceNote", budget);
|
|
369
388
|
}
|
|
370
|
-
function
|
|
371
|
-
|
|
389
|
+
function cardElement(document, tag, part) {
|
|
390
|
+
const created = document.createElement(tag);
|
|
391
|
+
created.className = part ? `${ANSWER_CARD_CLASS}__${part}` : ANSWER_CARD_CLASS;
|
|
392
|
+
return created;
|
|
372
393
|
}
|
|
373
|
-
function
|
|
394
|
+
function appendMarkdown(document, parent, text, part) {
|
|
395
|
+
if (text !== null && text !== void 0) {
|
|
396
|
+
const wrapper = cardElement(document, "div", part);
|
|
397
|
+
wrapper.append(renderMarkdown(document, text));
|
|
398
|
+
parent.append(wrapper);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
function appendFacts(document, parent, facts, part = "facts") {
|
|
374
402
|
if (facts.length === 0) return;
|
|
375
|
-
const definitions = document
|
|
403
|
+
const definitions = cardElement(document, "dl", part);
|
|
376
404
|
for (const fact of facts) {
|
|
377
|
-
const term = document
|
|
405
|
+
const term = cardElement(document, "dt", "fact-label");
|
|
378
406
|
term.append(document.createTextNode(fact.label));
|
|
379
|
-
const definition = document
|
|
407
|
+
const definition = cardElement(document, "dd", "fact-value");
|
|
380
408
|
definition.append(document.createTextNode(fact.value));
|
|
381
409
|
definitions.append(term, definition);
|
|
382
410
|
}
|
|
383
411
|
parent.append(definitions);
|
|
384
412
|
}
|
|
385
|
-
function appendLinks(document, parent, links) {
|
|
413
|
+
function appendLinks(document, parent, links, part) {
|
|
386
414
|
if (links.length === 0) return;
|
|
387
|
-
const list = document
|
|
415
|
+
const list = cardElement(document, "ul", part);
|
|
388
416
|
for (const item of links) {
|
|
389
|
-
const listItem = document
|
|
417
|
+
const listItem = cardElement(document, "li", `${part}-item`);
|
|
390
418
|
const link = createSafeLink(document, item.label, item.url);
|
|
419
|
+
link?.classList.add(`${ANSWER_CARD_CLASS}__link`);
|
|
391
420
|
listItem.append(link ?? document.createTextNode(item.label));
|
|
392
421
|
list.append(listItem);
|
|
393
422
|
}
|
|
394
423
|
parent.append(list);
|
|
395
424
|
}
|
|
396
|
-
function appendNote(document, parent, note) {
|
|
425
|
+
function appendNote(document, parent, note, part = "note") {
|
|
397
426
|
if (note === null || note === void 0) return;
|
|
398
|
-
const paragraph = document
|
|
427
|
+
const paragraph = cardElement(document, "p", part);
|
|
399
428
|
paragraph.append(document.createTextNode(note));
|
|
400
429
|
parent.append(paragraph);
|
|
401
430
|
}
|
|
402
431
|
function createTable(document, table) {
|
|
403
|
-
const element = document
|
|
432
|
+
const element = cardElement(document, "table", "table");
|
|
404
433
|
if (table.headers.length > 0) {
|
|
405
434
|
const head = document.createElement("thead");
|
|
406
435
|
const row = document.createElement("tr");
|
|
407
436
|
for (const header of table.headers) {
|
|
408
|
-
const cell = document
|
|
437
|
+
const cell = cardElement(document, "th", "table-header");
|
|
409
438
|
cell.scope = "col";
|
|
410
439
|
cell.append(document.createTextNode(header));
|
|
411
440
|
row.append(cell);
|
|
@@ -418,7 +447,7 @@ function createTable(document, table) {
|
|
|
418
447
|
for (const cells of table.rows) {
|
|
419
448
|
const row = document.createElement("tr");
|
|
420
449
|
for (const value of cells) {
|
|
421
|
-
const cell = document
|
|
450
|
+
const cell = cardElement(document, "td", "table-cell");
|
|
422
451
|
cell.append(document.createTextNode(value));
|
|
423
452
|
row.append(cell);
|
|
424
453
|
}
|
|
@@ -448,14 +477,13 @@ function appendTables(document, parent, tables, sectionTitle, labels, names) {
|
|
|
448
477
|
const ordinal = (names.ordinals.get(name) ?? 0) + 1;
|
|
449
478
|
names.ordinals.set(name, ordinal);
|
|
450
479
|
const element = createTable(document, table);
|
|
451
|
-
const region = document
|
|
452
|
-
region.className = "vv-ai-answer-card__table-region";
|
|
480
|
+
const region = cardElement(document, "div", "table-region");
|
|
453
481
|
region.setAttribute("data-ai-table-region", "true");
|
|
454
482
|
region.setAttribute("role", "region");
|
|
455
483
|
region.setAttribute("tabindex", "0");
|
|
456
484
|
region.setAttribute("aria-label", (names.counts.get(name) ?? 0) > 1 ? `${name} (${ordinal})` : name);
|
|
457
485
|
region.style.overflowX = "auto";
|
|
458
|
-
const hint = document
|
|
486
|
+
const hint = cardElement(document, "p", "table-hint");
|
|
459
487
|
hint.append(document.createTextNode(labels.tableScroll));
|
|
460
488
|
region.append(hint);
|
|
461
489
|
region.append(element);
|
|
@@ -463,45 +491,45 @@ function appendTables(document, parent, tables, sectionTitle, labels, names) {
|
|
|
463
491
|
}
|
|
464
492
|
}
|
|
465
493
|
function appendSection(document, article, section, labels, names) {
|
|
466
|
-
const content = document
|
|
494
|
+
const content = cardElement(document, "section", "section");
|
|
467
495
|
if (!section.collapsible && section.title !== null && section.title !== void 0) {
|
|
468
|
-
const heading = document
|
|
496
|
+
const heading = cardElement(document, "h3", "section-title");
|
|
469
497
|
heading.append(document.createTextNode(section.title));
|
|
470
498
|
content.append(heading);
|
|
471
499
|
}
|
|
472
|
-
appendMarkdown(document, content, section.text);
|
|
500
|
+
appendMarkdown(document, content, section.text, "text");
|
|
473
501
|
appendFacts(document, content, section.facts ?? []);
|
|
474
502
|
appendTables(document, content, section.tables ?? [], section.title, labels, names);
|
|
475
|
-
appendLinks(document, content, section.links ?? []);
|
|
503
|
+
appendLinks(document, content, section.links ?? [], "links");
|
|
476
504
|
appendNote(document, content, section.note);
|
|
477
505
|
if (!section.collapsible) {
|
|
478
506
|
article.append(content);
|
|
479
507
|
return;
|
|
480
508
|
}
|
|
481
|
-
const details = document
|
|
482
|
-
const summary = document
|
|
509
|
+
const details = cardElement(document, "details", "details");
|
|
510
|
+
const summary = cardElement(document, "summary", "details-summary");
|
|
483
511
|
summary.append(document.createTextNode(section.title?.trim() ? section.title : labels.details));
|
|
484
512
|
details.append(summary, content);
|
|
485
513
|
article.append(details);
|
|
486
514
|
}
|
|
487
515
|
function renderKnownAnswerCard(document, card, labels) {
|
|
488
|
-
const article = document
|
|
489
|
-
const heading = document
|
|
516
|
+
const article = cardElement(document, "article", "");
|
|
517
|
+
const heading = cardElement(document, "h2", "title");
|
|
490
518
|
heading.append(document.createTextNode(card.title));
|
|
491
519
|
article.append(heading);
|
|
492
|
-
appendMarkdown(document, article, card.summary);
|
|
493
|
-
if (card.highlight) appendFacts(document, article, [card.highlight]);
|
|
520
|
+
appendMarkdown(document, article, card.summary, "summary");
|
|
521
|
+
if (card.highlight) appendFacts(document, article, [card.highlight], "highlight");
|
|
494
522
|
const names = tableRegionNames(card.sections ?? [], labels);
|
|
495
523
|
for (const section of card.sections ?? []) appendSection(document, article, section, labels, names);
|
|
496
524
|
appendNote(document, article, card.note);
|
|
497
|
-
appendLinks(document, article, card.actions ?? []);
|
|
525
|
+
appendLinks(document, article, card.actions ?? [], "actions");
|
|
498
526
|
if ((card.sources?.length ?? 0) > 0 || card.sourceNote !== null && card.sourceNote !== void 0) {
|
|
499
|
-
const sources = document
|
|
500
|
-
const sourceHeading = document
|
|
527
|
+
const sources = cardElement(document, "section", "sources");
|
|
528
|
+
const sourceHeading = cardElement(document, "h3", "sources-title");
|
|
501
529
|
sourceHeading.append(document.createTextNode(labels.sources));
|
|
502
530
|
sources.append(sourceHeading);
|
|
503
|
-
appendLinks(document, sources, card.sources ?? []);
|
|
504
|
-
appendNote(document, sources, card.sourceNote);
|
|
531
|
+
appendLinks(document, sources, card.sources ?? [], "sources-list");
|
|
532
|
+
appendNote(document, sources, card.sourceNote, "source-note");
|
|
505
533
|
article.append(sources);
|
|
506
534
|
}
|
|
507
535
|
return article;
|
|
@@ -552,10 +580,11 @@ function genericEntries(data) {
|
|
|
552
580
|
function renderGenericAnswerCard(document, data) {
|
|
553
581
|
const entries = genericEntries(data);
|
|
554
582
|
if (entries.length === 0) return null;
|
|
555
|
-
const article = document
|
|
556
|
-
|
|
583
|
+
const article = cardElement(document, "article", "");
|
|
584
|
+
article.classList.add(`${ANSWER_CARD_CLASS}--generic`);
|
|
585
|
+
const list = cardElement(document, "ul", "entries");
|
|
557
586
|
for (const fields of entries) {
|
|
558
|
-
const item = document
|
|
587
|
+
const item = cardElement(document, "li", "entry");
|
|
559
588
|
appendFacts(document, item, fields);
|
|
560
589
|
list.append(item);
|
|
561
590
|
}
|
|
@@ -1141,4 +1170,4 @@ function bindChat(root, options = {}) {
|
|
|
1141
1170
|
};
|
|
1142
1171
|
}
|
|
1143
1172
|
//#endregion
|
|
1144
|
-
export { AI_CHAT_PROTOCOL_VERSION, bindChat, createAiChatRegistry, createChatSession, parseStreamEvent, readEventStream, renderAnswerCard };
|
|
1173
|
+
export { AI_CHAT_PROTOCOL_VERSION, ANSWER_CARD_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, isAnswerCard, isSafeLinkUrl, parseStreamEvent, readEventStream, renderAnswerCard };
|