@vettvangur/ai-sdk 0.2.1 → 0.4.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 +6 -11
- package/dist/index.d.ts +1 -36
- package/dist/index.js +1 -87
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
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
|
-
a DOM binding for a server-rendered shell, and generic
|
|
6
|
+
a DOM binding for a server-rendered shell, and the few generic pieces a host draws tool results with. It
|
|
7
7
|
ships no styles: every element it renders carries a BEM class, and the project styles those.
|
|
8
8
|
|
|
9
9
|
```html
|
|
@@ -80,21 +80,16 @@ value is not the payload's `key` field. Register it with `registry.registerSurfa
|
|
|
80
80
|
|
|
81
81
|
## Cards
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
Each returns null when there is nothing to show. Cards for a project's own payloads belong to the
|
|
84
|
+
project; build them with the helpers below rather than adding them here.
|
|
85
85
|
|
|
86
|
-
- `renderContactCard` (`vv-ai-contact-card`): name, subtitle, image (such as a flag), address, phone
|
|
87
|
-
and email links, and labelled groups of hours. Parts: `__head`, `__image`, `__title`, `__name`,
|
|
88
|
-
`__subtitle`, `__address`, `__links`, `__link`, `__hours`, `__hours-label`, `__hours-row`,
|
|
89
|
-
`__hours-days`, `__hours-time`, `__hours-note`.
|
|
90
|
-
- `renderMediaCard` (`vv-ai-media-card`, `--with-image`): a title and facts led by an image. Parts:
|
|
91
|
-
`__figure`, `__image`, `__body`, `__title`, `__facts`, `__fact`, `__fact-label`, `__fact-value`.
|
|
92
86
|
- `renderActions` (`vv-ai-actions`): buttons that run a registered action and safe links, each
|
|
93
87
|
`__action` with `--primary` or `--secondary`.
|
|
94
88
|
- `renderCardList` (`vv-ai-card-list`, `--{modifier}`): several cards from one result.
|
|
95
89
|
|
|
96
|
-
`createSafeImage
|
|
97
|
-
|
|
90
|
+
`createSafeImage` (web and site-relative URLs only, with srcset and size when given),
|
|
91
|
+
`createSafeLink` (the `isSafeLinkUrl` rule, new tab for web URLs) and `createPart` (an element with
|
|
92
|
+
a BEM class and text) are exported for a host's own renderers.
|
|
98
93
|
|
|
99
94
|
## Answer cards
|
|
100
95
|
|
package/dist/index.d.ts
CHANGED
|
@@ -174,29 +174,6 @@ type AiCardImage = {
|
|
|
174
174
|
width?: number | null;
|
|
175
175
|
height?: number | null;
|
|
176
176
|
};
|
|
177
|
-
type AiContactCardHoursRow = {
|
|
178
|
-
label: string;
|
|
179
|
-
value: string;
|
|
180
|
-
note?: string | null;
|
|
181
|
-
};
|
|
182
|
-
type AiContactCardHours = {
|
|
183
|
-
label?: string | null;
|
|
184
|
-
rows: AiContactCardHoursRow[];
|
|
185
|
-
};
|
|
186
|
-
type AiContactCard = {
|
|
187
|
-
name: string;
|
|
188
|
-
subtitle?: string | null;
|
|
189
|
-
image?: AiCardImage | null;
|
|
190
|
-
address?: string | null;
|
|
191
|
-
phone?: string | null;
|
|
192
|
-
email?: string | null;
|
|
193
|
-
hours?: AiContactCardHours[] | null;
|
|
194
|
-
};
|
|
195
|
-
type AiMediaCard = {
|
|
196
|
-
title: string;
|
|
197
|
-
image?: AiCardImage | null;
|
|
198
|
-
facts?: AiAnswerCardFact[] | null;
|
|
199
|
-
};
|
|
200
177
|
type AiChatActionItem = {
|
|
201
178
|
label: string;
|
|
202
179
|
action?: string | null;
|
|
@@ -245,24 +222,12 @@ declare function bindChat(root: HTMLElement, options?: AiChatBindings): AiChatBi
|
|
|
245
222
|
//#region src/cards.d.ts
|
|
246
223
|
/** Block classes of the cards below. Every element a card renders carries a class under its block. */
|
|
247
224
|
declare const CARD_LIST_CLASS = "vv-ai-card-list";
|
|
248
|
-
declare const CONTACT_CARD_CLASS = "vv-ai-contact-card";
|
|
249
|
-
declare const MEDIA_CARD_CLASS = "vv-ai-media-card";
|
|
250
225
|
declare const ACTIONS_CLASS = "vv-ai-actions";
|
|
251
226
|
/**
|
|
252
227
|
* Several cards from one tool result, under `vv-ai-card-list` and `vv-ai-card-list--{modifier}`.
|
|
253
228
|
* Items that render nothing are skipped, and a list with nothing in it is null.
|
|
254
229
|
*/
|
|
255
230
|
declare function renderCardList<T>(document: Document, items: unknown, render: (item: T) => Node | null, modifier?: string): HTMLElement | null;
|
|
256
|
-
/**
|
|
257
|
-
* A place or person to get in touch with: name, an optional subtitle and image (such as a flag),
|
|
258
|
-
* address, phone and email links, and any number of labelled groups of hours. Null without a name.
|
|
259
|
-
*/
|
|
260
|
-
declare function renderContactCard(document: Document, data: unknown): HTMLElement | null;
|
|
261
|
-
/**
|
|
262
|
-
* A thing best recognised by its picture, with labelled facts beside it. The image leads, and the
|
|
263
|
-
* card carries `--with-image` when it has one. Null without a title.
|
|
264
|
-
*/
|
|
265
|
-
declare function renderMediaCard(document: Document, data: unknown): HTMLElement | null;
|
|
266
231
|
/**
|
|
267
232
|
* A row of things the visitor can do next. An item with `action` is a button that runs the action
|
|
268
233
|
* registered under that name, with `value` as its `data-ai-action-value`; an item with `url` is a
|
|
@@ -303,4 +268,4 @@ declare function readEventStream(response: Response, signal: AbortSignal): Async
|
|
|
303
268
|
*/
|
|
304
269
|
declare function createTemplateItemSurface(options: AiTemplateItemSurfaceOptions): (context: AiChatExtensionContext) => DocumentFragment | null;
|
|
305
270
|
//#endregion
|
|
306
|
-
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 AiChatExtensionContext, type AiChatMessage, type AiChatRegistry, type AiChatRenderer, type AiChatRequest, type AiChatSession, type AiChatSessionOptions, type AiChatSessionResult, type AiChatSessionStatus, type AiChatStreamEvent, type AiChatSurface, type
|
|
271
|
+
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 AiChatExtensionContext, type AiChatMessage, type AiChatRegistry, type AiChatRenderer, type AiChatRequest, type AiChatSession, type AiChatSessionOptions, type AiChatSessionResult, type AiChatSessionStatus, type AiChatStreamEvent, type AiChatSurface, 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 };
|
package/dist/index.js
CHANGED
|
@@ -1286,8 +1286,6 @@ function bindChat(root, options = {}) {
|
|
|
1286
1286
|
//#region src/cards.ts
|
|
1287
1287
|
/** Block classes of the cards below. Every element a card renders carries a class under its block. */
|
|
1288
1288
|
const CARD_LIST_CLASS = "vv-ai-card-list";
|
|
1289
|
-
const CONTACT_CARD_CLASS = "vv-ai-contact-card";
|
|
1290
|
-
const MEDIA_CARD_CLASS = "vv-ai-media-card";
|
|
1291
1289
|
const ACTIONS_CLASS = "vv-ai-actions";
|
|
1292
1290
|
function text(value) {
|
|
1293
1291
|
return typeof value === "string" && value.trim() ? value : null;
|
|
@@ -1305,90 +1303,6 @@ function renderCardList(document, items, render, modifier) {
|
|
|
1305
1303
|
list.append(...cards);
|
|
1306
1304
|
return list;
|
|
1307
1305
|
}
|
|
1308
|
-
function appendHours(document, card, hours) {
|
|
1309
|
-
const rows = Array.isArray(hours.rows) ? hours.rows.filter((row) => isPlainObject(row) && text(row.value)) : [];
|
|
1310
|
-
if (!rows.length) return;
|
|
1311
|
-
const block = createPart(document, "div", CONTACT_CARD_CLASS, "hours");
|
|
1312
|
-
const label = text(hours.label);
|
|
1313
|
-
if (label) block.append(createPart(document, "span", CONTACT_CARD_CLASS, "hours-label", label));
|
|
1314
|
-
for (const row of rows) {
|
|
1315
|
-
const line = createPart(document, "div", CONTACT_CARD_CLASS, "hours-row");
|
|
1316
|
-
const time = createPart(document, "span", CONTACT_CARD_CLASS, "hours-time", row.value);
|
|
1317
|
-
const note = text(row.note);
|
|
1318
|
-
if (note) time.append(" ", createPart(document, "span", CONTACT_CARD_CLASS, "hours-note", note));
|
|
1319
|
-
line.append(createPart(document, "span", CONTACT_CARD_CLASS, "hours-days", text(row.label)), time);
|
|
1320
|
-
block.append(line);
|
|
1321
|
-
}
|
|
1322
|
-
card.append(block);
|
|
1323
|
-
}
|
|
1324
|
-
/**
|
|
1325
|
-
* A place or person to get in touch with: name, an optional subtitle and image (such as a flag),
|
|
1326
|
-
* address, phone and email links, and any number of labelled groups of hours. Null without a name.
|
|
1327
|
-
*/
|
|
1328
|
-
function renderContactCard(document, data) {
|
|
1329
|
-
if (!isPlainObject(data)) return null;
|
|
1330
|
-
const contact = data;
|
|
1331
|
-
const name = text(contact.name);
|
|
1332
|
-
if (!name) return null;
|
|
1333
|
-
const card = createPart(document, "article", CONTACT_CARD_CLASS);
|
|
1334
|
-
const head = createPart(document, "header", CONTACT_CARD_CLASS, "head");
|
|
1335
|
-
const image = createSafeImage(document, contact.image);
|
|
1336
|
-
if (image) {
|
|
1337
|
-
image.className = `${CONTACT_CARD_CLASS}__image`;
|
|
1338
|
-
head.append(image);
|
|
1339
|
-
}
|
|
1340
|
-
const title = createPart(document, "span", CONTACT_CARD_CLASS, "title");
|
|
1341
|
-
title.append(createPart(document, "span", CONTACT_CARD_CLASS, "name", name));
|
|
1342
|
-
const subtitle = text(contact.subtitle);
|
|
1343
|
-
if (subtitle) title.append(createPart(document, "span", CONTACT_CARD_CLASS, "subtitle", subtitle));
|
|
1344
|
-
head.append(title);
|
|
1345
|
-
card.append(head);
|
|
1346
|
-
const address = text(contact.address);
|
|
1347
|
-
if (address) card.append(createPart(document, "p", CONTACT_CARD_CLASS, "address", address));
|
|
1348
|
-
const links = createPart(document, "p", CONTACT_CARD_CLASS, "links");
|
|
1349
|
-
const phone = text(contact.phone);
|
|
1350
|
-
const email = text(contact.email);
|
|
1351
|
-
for (const link of [phone ? createSafeLink(document, phone, `tel:${phone.replace(/[^+\d]/g, "")}`) : null, email ? createSafeLink(document, email, `mailto:${email.trim()}`) : null]) if (link) {
|
|
1352
|
-
link.className = `${CONTACT_CARD_CLASS}__link`;
|
|
1353
|
-
links.append(link);
|
|
1354
|
-
}
|
|
1355
|
-
if (links.children.length) card.append(links);
|
|
1356
|
-
for (const hours of Array.isArray(contact.hours) ? contact.hours : []) if (isPlainObject(hours)) appendHours(document, card, hours);
|
|
1357
|
-
return card;
|
|
1358
|
-
}
|
|
1359
|
-
/**
|
|
1360
|
-
* A thing best recognised by its picture, with labelled facts beside it. The image leads, and the
|
|
1361
|
-
* card carries `--with-image` when it has one. Null without a title.
|
|
1362
|
-
*/
|
|
1363
|
-
function renderMediaCard(document, data) {
|
|
1364
|
-
if (!isPlainObject(data)) return null;
|
|
1365
|
-
const media = data;
|
|
1366
|
-
const title = text(media.title);
|
|
1367
|
-
if (!title) return null;
|
|
1368
|
-
const card = createPart(document, "article", MEDIA_CARD_CLASS);
|
|
1369
|
-
const image = createSafeImage(document, media.image);
|
|
1370
|
-
if (image) {
|
|
1371
|
-
image.className = `${MEDIA_CARD_CLASS}__image`;
|
|
1372
|
-
const figure = createPart(document, "div", MEDIA_CARD_CLASS, "figure");
|
|
1373
|
-
figure.append(image);
|
|
1374
|
-
card.classList.add(`${MEDIA_CARD_CLASS}--with-image`);
|
|
1375
|
-
card.append(figure);
|
|
1376
|
-
}
|
|
1377
|
-
const body = createPart(document, "div", MEDIA_CARD_CLASS, "body");
|
|
1378
|
-
body.append(createPart(document, "h3", MEDIA_CARD_CLASS, "title", title));
|
|
1379
|
-
const facts = (Array.isArray(media.facts) ? media.facts : []).filter((fact) => isPlainObject(fact) && text(fact.value));
|
|
1380
|
-
if (facts.length) {
|
|
1381
|
-
const list = createPart(document, "div", MEDIA_CARD_CLASS, "facts");
|
|
1382
|
-
for (const fact of facts) {
|
|
1383
|
-
const row = createPart(document, "div", MEDIA_CARD_CLASS, "fact");
|
|
1384
|
-
row.append(createPart(document, "span", MEDIA_CARD_CLASS, "fact-label", text(fact.label)), createPart(document, "span", MEDIA_CARD_CLASS, "fact-value", fact.value));
|
|
1385
|
-
list.append(row);
|
|
1386
|
-
}
|
|
1387
|
-
body.append(list);
|
|
1388
|
-
}
|
|
1389
|
-
card.append(body);
|
|
1390
|
-
return card;
|
|
1391
|
-
}
|
|
1392
1306
|
/**
|
|
1393
1307
|
* A row of things the visitor can do next. An item with `action` is a button that runs the action
|
|
1394
1308
|
* registered under that name, with `value` as its `data-ai-action-value`; an item with `url` is a
|
|
@@ -1444,4 +1358,4 @@ function createTemplateItemSurface(options) {
|
|
|
1444
1358
|
};
|
|
1445
1359
|
}
|
|
1446
1360
|
//#endregion
|
|
1447
|
-
export { ACTIONS_CLASS, AI_CHAT_PROTOCOL_VERSION, ANSWER_CARD_CLASS, CARD_LIST_CLASS, CHAT_CLASS,
|
|
1361
|
+
export { ACTIONS_CLASS, AI_CHAT_PROTOCOL_VERSION, ANSWER_CARD_CLASS, 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 };
|