@keemakr/ui-sdk 1.3.0-dev-20261006093420 → 1.3.0-dev-20261006115752
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 +4 -0
- package/dist/index.js +2 -0
- package/dist/website-chat.d.ts +36 -0
- package/dist/website-chat.js +54 -0
- package/dist/widget.d.ts +41 -0
- package/dist/widget.js +95 -0
- package/package.json +6 -1
package/dist/index.d.ts
CHANGED
|
@@ -5,3 +5,7 @@ export type { ContainerChild, ContainerNodeType, KnownNodeType } from './nodes.j
|
|
|
5
5
|
export { pageRangeLabel, pageWindow, pressWithValue } from './pager.js';
|
|
6
6
|
export { defineScreen, defineSurface } from './define.js';
|
|
7
7
|
export type { ScreenDefinition, ScreenParams, LoadHandler, ActionHandler } from './define.js';
|
|
8
|
+
export { loadWebsiteChat, websiteChatSnippet } from './website-chat.js';
|
|
9
|
+
export type { ChatDocument, ChatScriptElement, WebsiteChatOptions } from './website-chat.js';
|
|
10
|
+
export { createWidgetClient } from './widget.js';
|
|
11
|
+
export type { WidgetClient, WidgetClientOptions, WidgetFetch, WidgetResponse, WidgetStorage, } from './widget.js';
|
package/dist/index.js
CHANGED
|
@@ -10,3 +10,5 @@ export { UI_SCHEMA_VERSION, pressSchema, buttonSchema, cellSchema, nodeSchema, s
|
|
|
10
10
|
export { childNodesOf, KNOWN_CONTAINERS, KNOWN_NODE_TYPES } from './nodes.js';
|
|
11
11
|
export { pageRangeLabel, pageWindow, pressWithValue } from './pager.js';
|
|
12
12
|
export { defineScreen, defineSurface } from './define.js';
|
|
13
|
+
export { loadWebsiteChat, websiteChatSnippet } from './website-chat.js';
|
|
14
|
+
export { createWidgetClient } from './widget.js';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** The parts of a DOM document these helpers touch; this package ships without DOM types. */
|
|
2
|
+
export interface ChatScriptElement {
|
|
3
|
+
src: string;
|
|
4
|
+
async: boolean;
|
|
5
|
+
setAttribute(name: string, value: string): void;
|
|
6
|
+
remove(): void;
|
|
7
|
+
}
|
|
8
|
+
export interface ChatDocument {
|
|
9
|
+
createElement(tag: 'script'): ChatScriptElement;
|
|
10
|
+
querySelector(selector: string): {
|
|
11
|
+
remove(): void;
|
|
12
|
+
} | null;
|
|
13
|
+
head: {
|
|
14
|
+
appendChild(node: ChatScriptElement): unknown;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export interface WebsiteChatOptions {
|
|
18
|
+
/** Core's origin, for example https://dash.v2.keemakr.ai. */
|
|
19
|
+
coreUrl: string;
|
|
20
|
+
/** The widget key, `wk_...`, from the widget account. */
|
|
21
|
+
key: string;
|
|
22
|
+
/** The button text. */
|
|
23
|
+
label?: string;
|
|
24
|
+
/** The document to add the script to; defaults to the global one. */
|
|
25
|
+
document?: ChatDocument;
|
|
26
|
+
}
|
|
27
|
+
/** The HTML snippet to paste into a site, as the widget settings show it. */
|
|
28
|
+
export declare function websiteChatSnippet(options: Omit<WebsiteChatOptions, 'document'>): string;
|
|
29
|
+
/**
|
|
30
|
+
* Add the website chat to the current page. Calling it again with the same key does
|
|
31
|
+
* nothing; a different key replaces the chat. `remove()` takes the chat off the page:
|
|
32
|
+
* its button, its frame and the script.
|
|
33
|
+
*/
|
|
34
|
+
export declare function loadWebsiteChat(options: WebsiteChatOptions): {
|
|
35
|
+
remove(): void;
|
|
36
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// The website chat, for a page or app that wants it without pasting the snippet by hand.
|
|
2
|
+
//
|
|
3
|
+
// The chat runs in a frame served by Core (/embed/v1/frame); the only thing a host page
|
|
4
|
+
// adds is Core's loader script with the widget key. The loader draws a button with no
|
|
5
|
+
// network call and creates the frame only when the visitor opens it. These helpers write
|
|
6
|
+
// that one script tag, once.
|
|
7
|
+
const KEY = /^wk_[A-Za-z0-9_-]{8,100}$/;
|
|
8
|
+
function loaderUrl(coreUrl) {
|
|
9
|
+
const origin = new URL(coreUrl).origin;
|
|
10
|
+
return `${origin}/embed/v1/loader.js`;
|
|
11
|
+
}
|
|
12
|
+
function checkKey(key) {
|
|
13
|
+
if (!KEY.test(key))
|
|
14
|
+
throw new Error('a website chat key looks like wk_...');
|
|
15
|
+
}
|
|
16
|
+
function escapeAttribute(value) {
|
|
17
|
+
return value.replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<');
|
|
18
|
+
}
|
|
19
|
+
/** The HTML snippet to paste into a site, as the widget settings show it. */
|
|
20
|
+
export function websiteChatSnippet(options) {
|
|
21
|
+
checkKey(options.key);
|
|
22
|
+
const label = options.label ? ` data-label="${escapeAttribute(options.label)}"` : '';
|
|
23
|
+
return `<script src="${loaderUrl(options.coreUrl)}" data-key="${options.key}"${label} async></script>`;
|
|
24
|
+
}
|
|
25
|
+
/** Take off the chat this script made, and nothing a later load of the same key made. */
|
|
26
|
+
function takeOff(script) {
|
|
27
|
+
const loaded = globalThis.__keeChat;
|
|
28
|
+
if (loaded && loaded.script === script)
|
|
29
|
+
loaded.destroy();
|
|
30
|
+
script.remove();
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Add the website chat to the current page. Calling it again with the same key does
|
|
34
|
+
* nothing; a different key replaces the chat. `remove()` takes the chat off the page:
|
|
35
|
+
* its button, its frame and the script.
|
|
36
|
+
*/
|
|
37
|
+
export function loadWebsiteChat(options) {
|
|
38
|
+
checkKey(options.key);
|
|
39
|
+
const doc = options.document ?? globalThis.document;
|
|
40
|
+
if (!doc)
|
|
41
|
+
throw new Error('loadWebsiteChat needs a document');
|
|
42
|
+
const existing = doc.querySelector(`script[data-kee-chat="${options.key}"]`);
|
|
43
|
+
if (existing)
|
|
44
|
+
return { remove: () => takeOff(existing) };
|
|
45
|
+
const script = doc.createElement('script');
|
|
46
|
+
script.src = loaderUrl(options.coreUrl);
|
|
47
|
+
script.async = true;
|
|
48
|
+
script.setAttribute('data-key', options.key);
|
|
49
|
+
script.setAttribute('data-kee-chat', options.key);
|
|
50
|
+
if (options.label)
|
|
51
|
+
script.setAttribute('data-label', options.label);
|
|
52
|
+
doc.head.appendChild(script);
|
|
53
|
+
return { remove: () => takeOff(script) };
|
|
54
|
+
}
|
package/dist/widget.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export interface WidgetStorage {
|
|
2
|
+
getItem(key: string): string | null;
|
|
3
|
+
setItem(key: string, value: string): void;
|
|
4
|
+
removeItem(key: string): void;
|
|
5
|
+
}
|
|
6
|
+
export interface WidgetResponse {
|
|
7
|
+
ok: boolean;
|
|
8
|
+
status: number;
|
|
9
|
+
json(): Promise<unknown>;
|
|
10
|
+
}
|
|
11
|
+
export type WidgetFetch = (url: string, init: {
|
|
12
|
+
method: 'GET' | 'POST';
|
|
13
|
+
headers?: Record<string, string>;
|
|
14
|
+
body?: unknown;
|
|
15
|
+
}) => Promise<WidgetResponse>;
|
|
16
|
+
export interface WidgetClientOptions {
|
|
17
|
+
coreUrl: string;
|
|
18
|
+
widgetId: string;
|
|
19
|
+
routeKey?: string;
|
|
20
|
+
storage?: WidgetStorage;
|
|
21
|
+
fetch?: WidgetFetch;
|
|
22
|
+
}
|
|
23
|
+
export interface WidgetClient {
|
|
24
|
+
bootstrap(): Promise<void>;
|
|
25
|
+
getMessages(): Promise<unknown>;
|
|
26
|
+
sendMessage(input: {
|
|
27
|
+
text: string;
|
|
28
|
+
idempotencyKey?: string;
|
|
29
|
+
attachmentIds?: string[];
|
|
30
|
+
}): Promise<unknown>;
|
|
31
|
+
assertHandle(kind: 'email' | 'phone', value: string): Promise<unknown>;
|
|
32
|
+
verifyHandle(challengeId: string, code: string): Promise<unknown>;
|
|
33
|
+
uploadAttachment(bytes: Uint8Array, mediaType: string): Promise<unknown>;
|
|
34
|
+
forget(): void;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* @deprecated The website chat now runs in a frame served by Core, and the routes this
|
|
38
|
+
* client calls (`/api/channels/widget/*`) no longer exist. Use `loadWebsiteChat` or
|
|
39
|
+
* `websiteChatSnippet` instead.
|
|
40
|
+
*/
|
|
41
|
+
export declare function createWidgetClient(options: WidgetClientOptions): WidgetClient;
|
package/dist/widget.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
function browserStorage() {
|
|
2
|
+
const storage = globalThis.localStorage;
|
|
3
|
+
if (!storage)
|
|
4
|
+
throw new Error('widget storage is unavailable');
|
|
5
|
+
return storage;
|
|
6
|
+
}
|
|
7
|
+
function browserFetch() {
|
|
8
|
+
const value = globalThis.fetch;
|
|
9
|
+
if (typeof value !== 'function')
|
|
10
|
+
throw new Error('widget fetch is unavailable');
|
|
11
|
+
return value.bind(globalThis);
|
|
12
|
+
}
|
|
13
|
+
function randomId() {
|
|
14
|
+
const crypto = globalThis.crypto;
|
|
15
|
+
if (!crypto?.randomUUID)
|
|
16
|
+
throw new Error('crypto.randomUUID is unavailable');
|
|
17
|
+
return crypto.randomUUID();
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated The website chat now runs in a frame served by Core, and the routes this
|
|
21
|
+
* client calls (`/api/channels/widget/*`) no longer exist. Use `loadWebsiteChat` or
|
|
22
|
+
* `websiteChatSnippet` instead.
|
|
23
|
+
*/
|
|
24
|
+
export function createWidgetClient(options) {
|
|
25
|
+
const coreUrl = options.coreUrl.replace(/\/$/, '');
|
|
26
|
+
const routeKey = options.routeKey ?? 'default';
|
|
27
|
+
const storage = options.storage ?? browserStorage();
|
|
28
|
+
const request = options.fetch ?? browserFetch();
|
|
29
|
+
const storageKey = `kee:widget:${options.widgetId}:${routeKey}`;
|
|
30
|
+
const query = new URLSearchParams({
|
|
31
|
+
widget_id: options.widgetId,
|
|
32
|
+
route_key: routeKey,
|
|
33
|
+
}).toString();
|
|
34
|
+
async function decode(response) {
|
|
35
|
+
const body = await response.json();
|
|
36
|
+
if (!response.ok)
|
|
37
|
+
throw new Error(`widget request failed (${response.status})`);
|
|
38
|
+
return body;
|
|
39
|
+
}
|
|
40
|
+
async function bootstrap(retry = true) {
|
|
41
|
+
const visitorToken = storage.getItem(storageKey);
|
|
42
|
+
const response = await request(`${coreUrl}/api/channels/widget/bootstrap?${query}`, {
|
|
43
|
+
method: 'POST',
|
|
44
|
+
headers: { 'content-type': 'application/json' },
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
widget_id: options.widgetId,
|
|
47
|
+
route_key: routeKey,
|
|
48
|
+
...(visitorToken ? { visitor_token: visitorToken } : {}),
|
|
49
|
+
}),
|
|
50
|
+
});
|
|
51
|
+
if (!response.ok && response.status === 401 && visitorToken && retry) {
|
|
52
|
+
storage.removeItem(storageKey);
|
|
53
|
+
return bootstrap(false);
|
|
54
|
+
}
|
|
55
|
+
const body = (await decode(response));
|
|
56
|
+
if (typeof body.visitor_token !== 'string')
|
|
57
|
+
throw new Error('widget bootstrap token is missing');
|
|
58
|
+
storage.setItem(storageKey, body.visitor_token);
|
|
59
|
+
}
|
|
60
|
+
async function authorizedRequest(path, init, retry = true) {
|
|
61
|
+
if (!storage.getItem(storageKey))
|
|
62
|
+
await bootstrap();
|
|
63
|
+
const token = storage.getItem(storageKey);
|
|
64
|
+
const response = await request(`${coreUrl}/api/channels/widget/${path}?${query}`, {
|
|
65
|
+
...init,
|
|
66
|
+
headers: { authorization: `Bearer ${token}`, ...init.headers },
|
|
67
|
+
});
|
|
68
|
+
if (!response.ok && response.status === 401 && retry) {
|
|
69
|
+
storage.removeItem(storageKey);
|
|
70
|
+
await bootstrap(false);
|
|
71
|
+
return authorizedRequest(path, init, false);
|
|
72
|
+
}
|
|
73
|
+
return decode(response);
|
|
74
|
+
}
|
|
75
|
+
async function post(path, body, headers) {
|
|
76
|
+
return authorizedRequest(path, { method: 'POST', headers, body });
|
|
77
|
+
}
|
|
78
|
+
async function get(path) {
|
|
79
|
+
return authorizedRequest(path, { method: 'GET' });
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
bootstrap,
|
|
83
|
+
getMessages: () => get('messages'),
|
|
84
|
+
sendMessage: ({ text, idempotencyKey = randomId(), attachmentIds = [] }) => post('messages', JSON.stringify({ text, idempotency_key: idempotencyKey, attachment_ids: attachmentIds }), { 'content-type': 'application/json' }),
|
|
85
|
+
assertHandle: (kind, value) => post('handles', JSON.stringify({ kind, value }), { 'content-type': 'application/json' }),
|
|
86
|
+
verifyHandle: (challengeId, code) => post('handles/verify', JSON.stringify({ challenge_id: challengeId, code }), {
|
|
87
|
+
'content-type': 'application/json',
|
|
88
|
+
}),
|
|
89
|
+
uploadAttachment: (bytes, mediaType) => post('attachments', bytes, {
|
|
90
|
+
'content-type': 'application/octet-stream',
|
|
91
|
+
'x-kee-media-type': mediaType,
|
|
92
|
+
}),
|
|
93
|
+
forget: () => storage.removeItem(storageKey),
|
|
94
|
+
};
|
|
95
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keemakr/ui-sdk",
|
|
3
|
-
"version": "1.3.0-dev-
|
|
3
|
+
"version": "1.3.0-dev-20261006115752",
|
|
4
4
|
"description": "Product screens for keemakr marketplace modules: a versioned, server-driven UI tree schema plus defineScreen() handlers. Module code never ships to the browser — keemakr-core validates the tree and renders it with platform components.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -16,6 +16,11 @@
|
|
|
16
16
|
"types": "./dist/build.d.ts",
|
|
17
17
|
"import": "./dist/build.js",
|
|
18
18
|
"default": "./dist/build.js"
|
|
19
|
+
},
|
|
20
|
+
"./widget": {
|
|
21
|
+
"types": "./dist/widget.d.ts",
|
|
22
|
+
"import": "./dist/widget.js",
|
|
23
|
+
"default": "./dist/widget.js"
|
|
19
24
|
}
|
|
20
25
|
},
|
|
21
26
|
"bin": {
|