@ahmedramez-org/chat 0.0.0-stage → 0.6.0-beta.2
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 +373 -2
- package/contract/bridge.d.ts +119 -0
- package/contract/bridge.js +249 -0
- package/contract/conformance.d.ts +51 -0
- package/contract/conformance.js +335 -0
- package/contract/host.d.ts +213 -0
- package/contract/host.js +380 -0
- package/contract/index.d.ts +922 -0
- package/contract/index.js +94 -0
- package/contract/launch.d.ts +110 -0
- package/contract/launch.js +164 -0
- package/index.d.ts +51 -0
- package/index.js +424 -0
- package/package.json +77 -4
- package/react.d.ts +129 -0
- package/react.js +312 -0
- package/window.js +515 -0
package/index.js
ADDED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
// @incorta/chat — the connector that loads and mounts the chat in any
|
|
2
|
+
// app, on any framework. The chat itself is a prebuilt Module Federation
|
|
3
|
+
// remote with its own React (`shared: {}`), loaded at runtime from one of:
|
|
4
|
+
//
|
|
5
|
+
// - IncortaCopilot (default): `<url>/nexus-chat/`, always the version
|
|
6
|
+
// that Copilot runs, so the UI matches its backend;
|
|
7
|
+
// - any URL that serves a release's chat build (`nexus-chat-dist.tar.gz` on
|
|
8
|
+
// the GitHub Release, extracted), pinned to that release.
|
|
9
|
+
//
|
|
10
|
+
// `assetsUrl` picks the source; without it the chat comes from Copilot.
|
|
11
|
+
// The host contract ships alongside: its types here, its values and helpers
|
|
12
|
+
// under `./contract`, `./conformance` and `./host`, and a React component
|
|
13
|
+
// under `./react`.
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The host contract version this connector speaks: the value of `CONTRACT_VERSION` in
|
|
17
|
+
* `@incorta/chat/contract` (the connector does not import the contract).
|
|
18
|
+
*/
|
|
19
|
+
export const CONTRACT_VERSION = 1;
|
|
20
|
+
|
|
21
|
+
const RETRY_DELAY_MS = 1_000;
|
|
22
|
+
const DEFAULT_TIMEOUT_MS = 20_000;
|
|
23
|
+
|
|
24
|
+
const loads = new Map();
|
|
25
|
+
const attempts = new Map();
|
|
26
|
+
/** Stylesheets of the chat that failed to load, reloaded by the next attempt. */
|
|
27
|
+
const failedStylesheets = new Set();
|
|
28
|
+
let loadedVersion;
|
|
29
|
+
|
|
30
|
+
// A loop, not `/\/+$/`: that regex backtracks quadratically on many slashes.
|
|
31
|
+
function withoutTrailingSlashes(url) {
|
|
32
|
+
let end = url.length;
|
|
33
|
+
while (url[end - 1] === '/') end -= 1;
|
|
34
|
+
return url.slice(0, end);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function entryUrl({ url, assetsUrl }) {
|
|
38
|
+
const base = assetsUrl ?? (url && `${withoutTrailingSlashes(url)}/nexus-chat/`);
|
|
39
|
+
if (!base) throw new Error('The chat needs a url or an assetsUrl.');
|
|
40
|
+
return new URL('remoteEntry.js', new URL(base, window.location.href)).href;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// A failed CSS chunk rejects the load, and Vite's preload never asks for a
|
|
44
|
+
// sheet it has seen again. Remember the chat's failed sheets so the next
|
|
45
|
+
// attempt requests them anew. A sheet failing again fails that attempt but is
|
|
46
|
+
// forgotten, so one a deploy removed blocks a single attempt, not every one.
|
|
47
|
+
function captureFailedStylesheets(base) {
|
|
48
|
+
if (captureFailedStylesheets.installed) return;
|
|
49
|
+
captureFailedStylesheets.installed = true;
|
|
50
|
+
window.addEventListener(
|
|
51
|
+
'error',
|
|
52
|
+
({ target }) => {
|
|
53
|
+
if (!(target instanceof HTMLLinkElement) || target.rel !== 'stylesheet') return;
|
|
54
|
+
if (target.dataset.nexusChatRetry === undefined && target.href.startsWith(base)) {
|
|
55
|
+
failedStylesheets.add(target.href);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
true,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function reloadFailedStylesheets() {
|
|
63
|
+
const reloads = [...failedStylesheets].map((href) => {
|
|
64
|
+
failedStylesheets.delete(href);
|
|
65
|
+
document.querySelectorAll('link[rel="stylesheet"]').forEach((link) => {
|
|
66
|
+
if (link.href === href) link.remove();
|
|
67
|
+
});
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
const link = Object.assign(document.createElement('link'), { rel: 'stylesheet', href });
|
|
70
|
+
link.dataset.nexusChatRetry = '';
|
|
71
|
+
link.onload = resolve;
|
|
72
|
+
link.onerror = () => {
|
|
73
|
+
link.remove();
|
|
74
|
+
reject(new Error(`The chat's stylesheet failed to load: ${href}`));
|
|
75
|
+
};
|
|
76
|
+
document.head.append(link);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
return Promise.all(reloads);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// A chat served by its Vite dev server ships React Fast Refresh code, which
|
|
83
|
+
// throws unless the page ran @vitejs/plugin-react's preamble. Only a local
|
|
84
|
+
// dev server can be one, so only there is it probed (GET; Vite ignores HEAD).
|
|
85
|
+
async function installReactRefreshPreamble(base) {
|
|
86
|
+
if (window.__vite_plugin_react_preamble_installed__) return;
|
|
87
|
+
if (!/^(localhost|127\.0\.0\.1|\[::1\])$|\.localhost$/.test(new URL(base).hostname)) return;
|
|
88
|
+
try {
|
|
89
|
+
const url = new URL('@react-refresh', base).href;
|
|
90
|
+
const probe = await fetch(url, { signal: AbortSignal.timeout(3_000) });
|
|
91
|
+
if (!probe.ok || !probe.headers.get('content-type')?.includes('javascript')) return;
|
|
92
|
+
const runtime = await import(/* @vite-ignore */ /* webpackIgnore: true */ url);
|
|
93
|
+
runtime.default.injectIntoGlobalHook(window);
|
|
94
|
+
window.$RefreshReg$ = () => {};
|
|
95
|
+
window.$RefreshSig$ = () => (type) => type;
|
|
96
|
+
window.__vite_plugin_react_preamble_installed__ = true;
|
|
97
|
+
} catch {
|
|
98
|
+
// Not a dev-served chat.
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function importMount(entry) {
|
|
103
|
+
// The browser remembers a failed import by URL, so every attempt after the
|
|
104
|
+
// first asks for a new one, or it would never reach the network again.
|
|
105
|
+
const attempt = attempts.get(entry) ?? 0;
|
|
106
|
+
attempts.set(entry, attempt + 1);
|
|
107
|
+
const base = new URL('.', entry).href;
|
|
108
|
+
captureFailedStylesheets(base);
|
|
109
|
+
await installReactRefreshPreamble(base);
|
|
110
|
+
if (attempt) await reloadFailedStylesheets();
|
|
111
|
+
const url = new URL(entry);
|
|
112
|
+
if (attempt) url.searchParams.set('nexus-chat-attempt', String(attempt));
|
|
113
|
+
const remote = await import(/* @vite-ignore */ /* webpackIgnore: true */ url.href);
|
|
114
|
+
await remote.init({});
|
|
115
|
+
const factory = await remote.get('./mount');
|
|
116
|
+
const module = await factory();
|
|
117
|
+
if (typeof module?.default !== 'function') {
|
|
118
|
+
throw new Error('The chat did not expose a mount function.');
|
|
119
|
+
}
|
|
120
|
+
loadedVersion =
|
|
121
|
+
typeof module.NEXUS_CHAT_VERSION === 'string' ? module.NEXUS_CHAT_VERSION : undefined;
|
|
122
|
+
return module.default;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Forgets a loaded chat, so the next load fetches it again: after it failed
|
|
127
|
+
* while running (a redeploy can leave it asking for files that are gone).
|
|
128
|
+
*/
|
|
129
|
+
export function forgetChat(source) {
|
|
130
|
+
// A source with no URL was never loaded.
|
|
131
|
+
if (source.url || source.assetsUrl) loads.delete(entryUrl(source));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The version the last loaded chat reports (for error reports); undefined before a load. */
|
|
135
|
+
export function loadedChatVersion() {
|
|
136
|
+
return loadedVersion;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A load failure as the contract's `ChatError` names it (`isChatError`), built
|
|
141
|
+
* here so the connector does not import the contract.
|
|
142
|
+
*/
|
|
143
|
+
function loadError(code, message, options) {
|
|
144
|
+
return Object.assign(new Error(message, options), { name: 'ChatError', code });
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @internal Rejects (`LOAD_TIMEOUT`) after `timeoutMs`, counted while the page can be seen: a
|
|
149
|
+
* hidden tab runs its timers and requests slowly (Chrome throttles background tabs), so a chat
|
|
150
|
+
* loading there gets a fresh deadline once the tab shows.
|
|
151
|
+
*/
|
|
152
|
+
export function loadDeadline(timeoutMs) {
|
|
153
|
+
return new Promise((_, reject) => {
|
|
154
|
+
const expire = () => {
|
|
155
|
+
if (!document.hidden) {
|
|
156
|
+
return reject(loadError('LOAD_TIMEOUT', 'Timed out loading the chat.'));
|
|
157
|
+
}
|
|
158
|
+
document.addEventListener('visibilitychange', () => setTimeout(expire, timeoutMs), {
|
|
159
|
+
once: true,
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
setTimeout(expire, timeoutMs);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Loads the chat once per source and resolves to its `mount` function. One
|
|
168
|
+
* retry, since a single failure (a 502 during a rolling restart) is not an
|
|
169
|
+
* outage, and one deadline over both, so a stalled load never hangs. A failed
|
|
170
|
+
* load is forgotten: the next call tries again. Calling it early (for example
|
|
171
|
+
* when the page is idle) makes the first open instant. It rejects with code
|
|
172
|
+
* `LOAD_TIMEOUT` past the deadline, or `LOAD_FAILED` (the retry's failure as
|
|
173
|
+
* `cause`, its message kept) when the retry failed too.
|
|
174
|
+
*/
|
|
175
|
+
export function loadChat(options) {
|
|
176
|
+
const entry = entryUrl(options);
|
|
177
|
+
let load = loads.get(entry);
|
|
178
|
+
if (!load) {
|
|
179
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
180
|
+
load = Promise.race([
|
|
181
|
+
importMount(entry)
|
|
182
|
+
.catch(async () => {
|
|
183
|
+
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY_MS));
|
|
184
|
+
return importMount(entry);
|
|
185
|
+
})
|
|
186
|
+
.catch((error) => {
|
|
187
|
+
throw loadError('LOAD_FAILED', String(error?.message ?? error), { cause: error });
|
|
188
|
+
}),
|
|
189
|
+
loadDeadline(timeoutMs),
|
|
190
|
+
]);
|
|
191
|
+
load.catch(() => loads.delete(entry));
|
|
192
|
+
loads.set(entry, load);
|
|
193
|
+
}
|
|
194
|
+
return load;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Loads the chat (from `assetsUrl`, else `connection.url`) and mounts
|
|
199
|
+
* it into `element` with the host contract options. The chat fills the
|
|
200
|
+
* element, so the element's box decides the presentation. Until the chat is
|
|
201
|
+
* up the skeleton holds the element; it leaves at once if the chat cannot load.
|
|
202
|
+
*/
|
|
203
|
+
export async function mountChat(element, { assetsUrl, timeoutMs, ...options }) {
|
|
204
|
+
const skeleton = showChatSkeleton(element, options.environment?.get().theme);
|
|
205
|
+
let handle;
|
|
206
|
+
try {
|
|
207
|
+
const source = { url: options.connection?.url, assetsUrl, timeoutMs };
|
|
208
|
+
const mount = await loadChat(source);
|
|
209
|
+
handle = mountLoadedChat(mount, element, { version: CONTRACT_VERSION, ...options }, source);
|
|
210
|
+
} catch (error) {
|
|
211
|
+
skeleton.remove();
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
skeleton.done();
|
|
215
|
+
return {
|
|
216
|
+
...handle,
|
|
217
|
+
unmount() {
|
|
218
|
+
skeleton.remove();
|
|
219
|
+
handle.unmount();
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* @internal Calls a loaded `mount` for `mountChat` and `<IncortaChat>`. A chat that fails
|
|
226
|
+
* (its `mount` throws, or it reports to the host's `onFatal`) is forgotten, so a retry fetches it
|
|
227
|
+
* afresh; the session holds it still has when it goes (`unmount`, or a throwing `mount`) are
|
|
228
|
+
* released, each once; and the handle has `attach` and `openSession` even from a chat older
|
|
229
|
+
* than them.
|
|
230
|
+
*/
|
|
231
|
+
export function mountLoadedChat(mount, element, options, source) {
|
|
232
|
+
const { onFatal, keepSessionAlive } = options.events ?? {};
|
|
233
|
+
const holds = new Set();
|
|
234
|
+
// Once the chat is gone it holds nothing: a late hold is refused, and one release that throws
|
|
235
|
+
// does not keep the others held.
|
|
236
|
+
let ended = false;
|
|
237
|
+
const released = (release) => {
|
|
238
|
+
try {
|
|
239
|
+
release();
|
|
240
|
+
} catch (error) {
|
|
241
|
+
console.error('The chat could not release a session hold.', error);
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
const releaseHolds = () => {
|
|
245
|
+
ended = true;
|
|
246
|
+
[...holds].forEach(released);
|
|
247
|
+
};
|
|
248
|
+
const wrapped = {
|
|
249
|
+
...(onFatal
|
|
250
|
+
? {
|
|
251
|
+
onFatal: (error) => {
|
|
252
|
+
forgetChat(source);
|
|
253
|
+
options.events.onFatal(error);
|
|
254
|
+
},
|
|
255
|
+
}
|
|
256
|
+
: {}),
|
|
257
|
+
...(keepSessionAlive
|
|
258
|
+
? {
|
|
259
|
+
keepSessionAlive: () => {
|
|
260
|
+
if (ended) return () => {};
|
|
261
|
+
const release = options.events.keepSessionAlive();
|
|
262
|
+
if (typeof release !== 'function') return release;
|
|
263
|
+
// The host ended the chat while handing out this hold: it is over already.
|
|
264
|
+
if (ended) {
|
|
265
|
+
released(release);
|
|
266
|
+
return () => {};
|
|
267
|
+
}
|
|
268
|
+
// One entry per hold, so a host that returns one release for every hold
|
|
269
|
+
// still gets each released.
|
|
270
|
+
const hold = () => {
|
|
271
|
+
if (holds.delete(hold)) release();
|
|
272
|
+
};
|
|
273
|
+
holds.add(hold);
|
|
274
|
+
return hold;
|
|
275
|
+
},
|
|
276
|
+
}
|
|
277
|
+
: {}),
|
|
278
|
+
};
|
|
279
|
+
// The chat calls these as methods of the object it is given. Every member the connector does
|
|
280
|
+
// not wrap is read from the host's own object and called on it, its own or inherited (a class
|
|
281
|
+
// instance's methods are neither own nor enumerable), so a callback that reads `this` sees it.
|
|
282
|
+
const events =
|
|
283
|
+
onFatal || keepSessionAlive
|
|
284
|
+
? new Proxy(options.events, {
|
|
285
|
+
get(host, name) {
|
|
286
|
+
if (Object.prototype.hasOwnProperty.call(wrapped, name)) return wrapped[name];
|
|
287
|
+
const member = host[name];
|
|
288
|
+
return typeof member === 'function' ? member.bind(host) : member;
|
|
289
|
+
},
|
|
290
|
+
})
|
|
291
|
+
: options.events;
|
|
292
|
+
let handle;
|
|
293
|
+
try {
|
|
294
|
+
handle = mount(element, events === options.events ? options : { ...options, events });
|
|
295
|
+
} catch (error) {
|
|
296
|
+
forgetChat(source);
|
|
297
|
+
releaseHolds();
|
|
298
|
+
throw error;
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
...handle,
|
|
302
|
+
attach: handle.attach ?? (() => false),
|
|
303
|
+
openSession: handle.openSession ?? (() => {}),
|
|
304
|
+
unmount() {
|
|
305
|
+
try {
|
|
306
|
+
handle.unmount();
|
|
307
|
+
} finally {
|
|
308
|
+
releaseHolds();
|
|
309
|
+
}
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// ---- the loading face every app shows until the chat is up (<IncortaChat> draws it too)
|
|
315
|
+
|
|
316
|
+
/** Fast loads show no loader; one that shows stays long enough to read, then cross-fades. */
|
|
317
|
+
const SKELETON_DELAY_MS = 300;
|
|
318
|
+
const SKELETON_MIN_MS = 400;
|
|
319
|
+
const SKELETON_FADE_MS = 200;
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* @internal The chat's own ground (so the swap does not flash), the loader's bar track, its
|
|
323
|
+
* window edge.
|
|
324
|
+
*/
|
|
325
|
+
export const PALETTE = {
|
|
326
|
+
light: { ground: '#fcfdfe', track: 'rgba(20,24,70,.08)', line: '#e4e4e7' },
|
|
327
|
+
dark: { ground: '#151515', track: 'rgba(255,255,255,.1)', line: '#2c2c2e' },
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
/** @internal The user asked for reduced motion. */
|
|
331
|
+
export const reducedMotion = () => window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
|
|
332
|
+
|
|
333
|
+
/** @internal The Incorta Intelligence mark. */
|
|
334
|
+
export const ICON = `data:image/svg+xml,${encodeURIComponent(
|
|
335
|
+
"<svg width='107' height='120' viewBox='0 0 107 120' fill='none' xmlns='http://www.w3.org/2000/svg'><g clip-path='url(#clip0_108_873)'><path d='M84 35.4C89.8 37.6 95.2 41.2 99.8 45.9C102.4 48.6 104.6 51.5 106.5 54.6L106.7 35.4C106.7 32 104.9 28.8 102 27L63.1 3.9C66.2 5.8 69.1 8.1 71.7 10.9C78.5 17.8 82.6 26.4 84 35.4Z' fill='url(#paint0_linear_108_873)'/><path d='M22.9 84.8C17.1 82.5 11.7 79 7.1 74.3C4.4 71.5 2.1 68.4 0.2 65.2L0 84.6C0 88.1 1.7 91.2 4.7 93L43.9 116.3C40.8 114.4 37.9 112.1 35.2 109.3C28.4 102.4 24.3 93.8 22.9 84.8Z' fill='url(#paint1_linear_108_873)'/><path d='M106.1 85.8L106.5 54.6C104.6 51.5 102.4 48.6 99.8 45.9C95.2 41.2 89.8 37.6 84 35.4C86.3 49.4 81.9 64.3 71 75C57.9 87.9 38.9 91.1 22.9 84.8C24.3 93.8 28.4 102.4 35.2 109.3C37.9 112.1 40.8 114.4 43.9 116.3L45.2 117.1L47.9 118.7C48.9 119.3 49.9 119.7 51 119.9C53.2 120.3 55.5 119.9 57.5 118.8L101.3 94.1C104.2 92.4 106.1 89.2 106.1 85.8V85.8Z' fill='url(#paint2_linear_108_873)'/><path d='M35.9 45.2C49 32.3 68 29.1 84 35.4C82.6 26.4 78.5 17.8 71.7 10.8C69.1 8.1 66.2 5.8 63.1 3.9L61.7 3.1L58.8 1.3C57.7 0.7 56.6 0.3 55.4 0.1C53.3 -0.2 51.1 0.2 49.2 1.2L5.4 26C2.4 27.6 0.6 30.8 0.6 34.2L0.2 65.2C2.1 68.4 4.4 71.5 7.1 74.3C11.7 79 17.1 82.5 22.9 84.8C20.7 70.7 25 55.9 35.9 45.2V45.2Z' fill='#00A098'/><path d='M22.9 84.8C23 85.3 23.1 85.8 23.2 86.3C23.2 86.5 23.2 86.7 23.3 86.8C23.3 87.2 23.4 87.5 23.5 87.9C23.5 88 23.6 88.2 23.6 88.4C23.7 88.8 23.8 89.1 23.9 89.4C23.9 89.6 24 89.8 24 90C24.1 90.4 24.2 90.8 24.4 91.1C24.4 91.4 24.5 91.6 24.6 91.9C24.7 92.2 24.8 92.5 24.9 92.8C25 93.1 25.1 93.3 25.2 93.6C25.3 93.8 25.4 94 25.5 94.2C25.6 94.5 25.7 94.8 25.8 95C25.9 95.2 25.9 95.4 26 95.6C26.2 95.9 26.3 96.2 26.4 96.5C26.5 96.6 26.5 96.7 26.5 96.7C26.9 97.4 27.2 98.1 27.6 98.8C27.6 98.9 27.6 98.9 27.7 99C27.9 99.3 28 99.7 28.2 100C28.3 100.1 28.4 100.2 28.4 100.3C28.6 100.7 28.8 101 29 101.3C29 101.4 29.1 101.5 29.1 101.6C29.8 102.7 30.6 103.8 31.4 104.9C31.8 104.9 92.7 102.9 96.2 42.5C96.6 36.7 96.4 30.2 95.5 23.2L63.1 3.9C66.2 5.8 69.1 8.1 71.7 10.9C78.5 17.8 82.6 26.4 84 35.4C86.3 49.4 81.9 64.3 71 75C57.9 87.9 38.9 91.1 22.9 84.8V84.8Z' fill='url(#paint3_linear_108_873)'/><path d='M84 35.4C84 34.9 83.9 34.4 83.8 33.8C83.7 33.7 83.7 33.5 83.7 33.4C83.6 33 83.5 32.7 83.4 32.3C83.4 32.1 83.3 31.9 83.3 31.7C83.2 31.4 83.1 31.1 83.1 30.8C83 30.6 83 30.4 82.9 30.2C82.8 29.8 82.7 29.4 82.6 29C82.5 28.8 82.4 28.5 82.3 28.3C82.2 28 82.1 27.7 82 27.4C81.9 27.1 81.8 26.9 81.7 26.6C81.6 26.4 81.5 26.2 81.5 25.9C81.4 25.7 81.2 25.4 81.1 25.1C81.1 24.9 81 24.7 80.9 24.6C80.8 24.3 80.6 24 80.5 23.7C80.5 23.6 80.4 23.5 80.4 23.4C80.1 22.7 79.7 22.1 79.4 21.4C79.3 21.3 79.3 21.2 79.2 21.2C79.1 20.8 78.9 20.5 78.7 20.2C78.6 20.1 78.6 20 78.5 19.9C78.3 19.5 78.1 19.2 77.9 18.9C77.9 18.8 77.8 18.7 77.8 18.6C77.1 17.5 76.3 16.4 75.5 15.3C75.1 15.3 14.2 17.3 10.7 77.6C10.4 83.5 10.6 90 11.4 97L43.8 116.2C40.7 114.3 37.8 112 35.2 109.3C28.4 102.4 24.3 93.8 22.9 84.8C20.7 70.8 25 55.9 35.9 45.2C49 32.3 68 29.1 84 35.4V35.4Z' fill='url(#paint4_linear_108_873)'/></g><defs><linearGradient id='paint0_linear_108_873' x1='104.6' y1='41.1' x2='52.2' y2='1.6' gradientUnits='userSpaceOnUse'><stop stop-color='#4854FE'/><stop offset='1' stop-color='#241C55'/></linearGradient><linearGradient id='paint1_linear_108_873' x1='0' y1='90.8' x2='43.9' y2='90.8' gradientUnits='userSpaceOnUse'><stop stop-color='#4854FE'/><stop offset='1' stop-color='#241C55'/></linearGradient><linearGradient id='paint2_linear_108_873' x1='31.5' y1='40.6' x2='92.7' y2='102.9' gradientUnits='userSpaceOnUse'><stop stop-color='white'/><stop offset='1' stop-color='#4854FE'/></linearGradient><linearGradient id='paint3_linear_108_873' x1='86.3' y1='19.1' x2='41.6' y2='98.7' gradientUnits='userSpaceOnUse'><stop stop-color='#4854FE' stop-opacity='0.93'/><stop offset='1' stop-color='#006EF0' stop-opacity='0'/></linearGradient><linearGradient id='paint4_linear_108_873' x1='19.9' y1='103.3' x2='61' y2='27.5' gradientUnits='userSpaceOnUse'><stop stop-color='#006EF0'/><stop offset='1' stop-color='#006EF0' stop-opacity='0'/></linearGradient><clipPath id='clip0_108_873'><rect width='106.7' height='120' fill='white'/></clipPath></defs></svg>",
|
|
336
|
+
)}`;
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Draws the loader over `box` (D-181): the mark over a gradient bar, a little above the
|
|
340
|
+
* middle (45 % from the top), the sizes of the screen it is on, revealed only once loading has
|
|
341
|
+
* taken SKELETON_DELAY_MS.
|
|
342
|
+
* It stays on top of whatever renders into `box` (the chat's React empties its
|
|
343
|
+
* element when it first paints) until `done` fades it out (after showing for
|
|
344
|
+
* SKELETON_MIN_MS) or `remove` drops it at once. Internal to this package.
|
|
345
|
+
*/
|
|
346
|
+
export function showChatSkeleton(box, theme) {
|
|
347
|
+
const root = document.createElement('div');
|
|
348
|
+
root.setAttribute('role', 'status');
|
|
349
|
+
root.setAttribute('aria-label', 'Loading Incorta Intelligence');
|
|
350
|
+
root.style.cssText = 'position:absolute;inset:0;pointer-events:none';
|
|
351
|
+
// The handoff's phone sizes, or its desktop ones in a box 1024px wide or more: a docked or
|
|
352
|
+
// floating window keeps the smaller face, as embed.html inside it does.
|
|
353
|
+
const [logo, bar, gap] = box.clientWidth >= 1024 ? [56, 128, 20] : [32, 80, 14];
|
|
354
|
+
root.innerHTML = `<div style="position:absolute;top:45%;left:50%;transform:translate(-50%,-50%);display:flex;flex-direction:column;align-items:center;gap:${gap}px"><img alt="" width="${logo}" height="${logo * 1.125}" style="display:block;opacity:.7"><div style="direction:ltr;width:${bar}px;height:3px;border-radius:3px;overflow:hidden"><div style="width:40%;height:100%;border-radius:3px;background:linear-gradient(90deg,#00a098,#006ef0,#4854fe)"></div></div></div>`;
|
|
355
|
+
root.querySelector('img').src = ICON;
|
|
356
|
+
const stack = root.firstChild;
|
|
357
|
+
const track = stack.lastChild;
|
|
358
|
+
const setTheme = (next) => {
|
|
359
|
+
const palette = PALETTE[next === 'dark' ? 'dark' : 'light'];
|
|
360
|
+
root.style.background = palette.ground;
|
|
361
|
+
track.style.background = palette.track;
|
|
362
|
+
};
|
|
363
|
+
setTheme(theme);
|
|
364
|
+
|
|
365
|
+
// An unpositioned box becomes the skeleton's containing block while it shows.
|
|
366
|
+
const position = box.style.position;
|
|
367
|
+
const unpositioned = getComputedStyle(box).position === 'static';
|
|
368
|
+
if (unpositioned) box.style.position = 'relative';
|
|
369
|
+
box.append(root);
|
|
370
|
+
const stay = new MutationObserver(() => root.parentNode === box || box.append(root));
|
|
371
|
+
stay.observe(box, { childList: true });
|
|
372
|
+
|
|
373
|
+
const shownAt = Date.now() + SKELETON_DELAY_MS;
|
|
374
|
+
const animations = [
|
|
375
|
+
stack.animate?.([{ opacity: 0 }, { opacity: 1 }], {
|
|
376
|
+
duration: SKELETON_FADE_MS,
|
|
377
|
+
delay: SKELETON_DELAY_MS,
|
|
378
|
+
fill: 'backwards',
|
|
379
|
+
}),
|
|
380
|
+
// The segment sweeps the track; slower, not still, under reduced motion.
|
|
381
|
+
track.firstChild.animate?.(
|
|
382
|
+
[{ transform: 'translateX(-100%)' }, { transform: 'translateX(250%)' }],
|
|
383
|
+
{
|
|
384
|
+
duration: reducedMotion() ? 3000 : 1330,
|
|
385
|
+
easing: 'cubic-bezier(.6,0,.3,1)',
|
|
386
|
+
iterations: Infinity,
|
|
387
|
+
},
|
|
388
|
+
),
|
|
389
|
+
];
|
|
390
|
+
let timer;
|
|
391
|
+
let gone = false;
|
|
392
|
+
const remove = () => {
|
|
393
|
+
if (gone) return;
|
|
394
|
+
gone = true;
|
|
395
|
+
clearTimeout(timer);
|
|
396
|
+
stay.disconnect();
|
|
397
|
+
animations.forEach((animation) => animation?.cancel());
|
|
398
|
+
root.remove();
|
|
399
|
+
if (unpositioned) box.style.position = position;
|
|
400
|
+
};
|
|
401
|
+
return {
|
|
402
|
+
setTheme,
|
|
403
|
+
remove,
|
|
404
|
+
done() {
|
|
405
|
+
if (gone || timer !== undefined) return;
|
|
406
|
+
const sinceShown = Date.now() - shownAt;
|
|
407
|
+
// Never shown (a fast load): gone at once, before its reveal can start.
|
|
408
|
+
if (sinceShown < 0) return remove();
|
|
409
|
+
// Shown: for at least SKELETON_MIN_MS, then it fades.
|
|
410
|
+
timer = setTimeout(
|
|
411
|
+
() => {
|
|
412
|
+
const fade = root.animate?.([{ opacity: 1 }, { opacity: 0 }], {
|
|
413
|
+
duration: SKELETON_FADE_MS,
|
|
414
|
+
fill: 'forwards',
|
|
415
|
+
});
|
|
416
|
+
if (!fade) return remove();
|
|
417
|
+
animations.push(fade);
|
|
418
|
+
fade.onfinish = remove;
|
|
419
|
+
},
|
|
420
|
+
Math.max(0, SKELETON_MIN_MS - sinceShown),
|
|
421
|
+
);
|
|
422
|
+
},
|
|
423
|
+
};
|
|
424
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,79 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahmedramez-org/chat",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.6.0-beta.2",
|
|
4
|
+
"description": "Incorta's chat for any app: a connector and React component that load the prebuilt chat from IncortaCopilot (or a pinned copy), and the host contract types and checks.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/Incorta/nexus-chat-ui.git",
|
|
9
|
+
"directory": "packages/nexus-chat"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"default": "./index.js"
|
|
17
|
+
},
|
|
18
|
+
"./react": {
|
|
19
|
+
"types": "./react.d.ts",
|
|
20
|
+
"default": "./react.js"
|
|
21
|
+
},
|
|
22
|
+
"./contract": {
|
|
23
|
+
"types": "./contract/index.d.ts",
|
|
24
|
+
"default": "./contract/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./conformance": {
|
|
27
|
+
"types": "./contract/conformance.d.ts",
|
|
28
|
+
"default": "./contract/conformance.js"
|
|
29
|
+
},
|
|
30
|
+
"./host": {
|
|
31
|
+
"types": "./contract/host.d.ts",
|
|
32
|
+
"default": "./contract/host.js"
|
|
33
|
+
},
|
|
34
|
+
"./bridge": {
|
|
35
|
+
"types": "./contract/bridge.d.ts",
|
|
36
|
+
"default": "./contract/bridge.js"
|
|
37
|
+
},
|
|
38
|
+
"./package.json": "./package.json"
|
|
39
|
+
},
|
|
40
|
+
"typesVersions": {
|
|
41
|
+
"*": {
|
|
42
|
+
"react": [
|
|
43
|
+
"./react.d.ts"
|
|
44
|
+
],
|
|
45
|
+
"contract": [
|
|
46
|
+
"./contract/index.d.ts"
|
|
47
|
+
],
|
|
48
|
+
"conformance": [
|
|
49
|
+
"./contract/conformance.d.ts"
|
|
50
|
+
],
|
|
51
|
+
"host": [
|
|
52
|
+
"./contract/host.d.ts"
|
|
53
|
+
],
|
|
54
|
+
"bridge": [
|
|
55
|
+
"./contract/bridge.d.ts"
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"files": [
|
|
60
|
+
"index.js",
|
|
61
|
+
"index.d.ts",
|
|
62
|
+
"react.js",
|
|
63
|
+
"window.js",
|
|
64
|
+
"react.d.ts",
|
|
65
|
+
"contract",
|
|
66
|
+
"README.md"
|
|
67
|
+
],
|
|
68
|
+
"publishConfig": {
|
|
69
|
+
"access": "public"
|
|
70
|
+
},
|
|
71
|
+
"peerDependencies": {
|
|
72
|
+
"react": ">=18"
|
|
73
|
+
},
|
|
74
|
+
"peerDependenciesMeta": {
|
|
75
|
+
"react": {
|
|
76
|
+
"optional": true
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
package/react.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { CSSProperties, ReactElement } from 'react';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
HostDirection,
|
|
5
|
+
HostTheme,
|
|
6
|
+
HostUser,
|
|
7
|
+
MountChatOptions,
|
|
8
|
+
ChatEnvironment,
|
|
9
|
+
ChatHandle,
|
|
10
|
+
ChatWindow,
|
|
11
|
+
ChatWindowMode,
|
|
12
|
+
} from './index.js';
|
|
13
|
+
|
|
14
|
+
interface BaseChatProps extends Omit<MountChatOptions, 'environment' | 'window'> {
|
|
15
|
+
embedUrl?: undefined;
|
|
16
|
+
/** Required unless `environment` is given. */
|
|
17
|
+
theme?: HostTheme;
|
|
18
|
+
/** Default: the browser's language. */
|
|
19
|
+
locale?: string;
|
|
20
|
+
/** Absent: follows `locale`. */
|
|
21
|
+
direction?: HostDirection;
|
|
22
|
+
tenantId?: string;
|
|
23
|
+
user?: HostUser;
|
|
24
|
+
/** The app's own contract environment, used as is instead of the props above. */
|
|
25
|
+
environment?: ChatEnvironment;
|
|
26
|
+
/**
|
|
27
|
+
* The chat is mounted (`ask`, `attach` and `openSession` are available, an older chat's
|
|
28
|
+
* `attach` refusing); `null` once it unmounts.
|
|
29
|
+
*/
|
|
30
|
+
onReady?(chat: ChatHandle | null): void;
|
|
31
|
+
/**
|
|
32
|
+
* The chat could not load (after its retry and deadline): `code` `'LOAD_TIMEOUT'` or
|
|
33
|
+
* `'LOAD_FAILED'` (`isChatError`), the chat's own mount error, or a plain error when there
|
|
34
|
+
* is no `url` or `assetsUrl`.
|
|
35
|
+
*/
|
|
36
|
+
onLoadError?(error: Error): void;
|
|
37
|
+
className?: string;
|
|
38
|
+
style?: CSSProperties;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The chat mounted into this page, sharing its session. It fills its box. */
|
|
42
|
+
export interface DirectChatProps extends BaseChatProps {
|
|
43
|
+
/**
|
|
44
|
+
* The app owns the chat's window (a docked panel, an overlay): the chat
|
|
45
|
+
* shows a dock toggle while `canDock` (or, with `mode`, `modes` and
|
|
46
|
+
* `setMode`, a button per other mode), and a close button while it has
|
|
47
|
+
* `close`. `setMode` and `close` are read at mount. A window named by its
|
|
48
|
+
* mode alone gets the rest from it, for an older chat: `docked` while not
|
|
49
|
+
* full screen, `canDock` while `modes` has full screen and another mode,
|
|
50
|
+
* and `setDocked` as `setMode`. A contract `ChatWindow` is used as is.
|
|
51
|
+
*/
|
|
52
|
+
window?:
|
|
53
|
+
| {
|
|
54
|
+
docked: boolean;
|
|
55
|
+
canDock: boolean;
|
|
56
|
+
mode?: ChatMode;
|
|
57
|
+
modes?: readonly ChatMode[];
|
|
58
|
+
/** Minimized but still mounted: a dictation in progress ends. */
|
|
59
|
+
hidden?: boolean;
|
|
60
|
+
setDocked(docked: boolean): void;
|
|
61
|
+
setMode?(mode: ChatMode): void;
|
|
62
|
+
close?(): void;
|
|
63
|
+
}
|
|
64
|
+
| {
|
|
65
|
+
mode: ChatMode;
|
|
66
|
+
modes: readonly ChatMode[];
|
|
67
|
+
/** Minimized but still mounted: a dictation in progress ends. */
|
|
68
|
+
hidden?: boolean;
|
|
69
|
+
setMode(mode: ChatMode): void;
|
|
70
|
+
close?(): void;
|
|
71
|
+
docked?: boolean;
|
|
72
|
+
canDock?: boolean;
|
|
73
|
+
setDocked?(docked: boolean): void;
|
|
74
|
+
}
|
|
75
|
+
| ChatWindow;
|
|
76
|
+
modes?: undefined;
|
|
77
|
+
defaultMode?: undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The chat mounted into this page, sharing its session, in a window drawn
|
|
82
|
+
* here as for `embedUrl`: a launcher icon that opens it floating, docked on
|
|
83
|
+
* the right, or fullscreen. Giving `modes` or `defaultMode` instead of a
|
|
84
|
+
* `window` opts in.
|
|
85
|
+
*/
|
|
86
|
+
export interface WindowedChatProps extends BaseChatProps {
|
|
87
|
+
/** Where the chat may open, in launcher order. Default: all three. */
|
|
88
|
+
modes?: ChatMode[];
|
|
89
|
+
/** Open on load in this mode. Default: the launcher icon only. */
|
|
90
|
+
defaultMode?: ChatMode;
|
|
91
|
+
window?: undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export type ChatMode = ChatWindowMode;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The chat in an <iframe> served by Incorta, on the user's own Incorta
|
|
98
|
+
* session (the app never holds a token). It draws its window: a launcher
|
|
99
|
+
* icon that opens the chat floating, docked on the right (left on an RTL page), or fullscreen.
|
|
100
|
+
*/
|
|
101
|
+
export interface EmbeddedChatProps {
|
|
102
|
+
/** Incorta's chat page: `<incorta>/copilot/nexus-chat/embed.html`. */
|
|
103
|
+
embedUrl: string;
|
|
104
|
+
/** Where the chat may open, in launcher order. Default: all three. */
|
|
105
|
+
modes?: ChatMode[];
|
|
106
|
+
/** Open on load in this mode. Default: the launcher icon only. */
|
|
107
|
+
defaultMode?: ChatMode;
|
|
108
|
+
/** Default `'light'`; followed live. */
|
|
109
|
+
theme?: HostTheme;
|
|
110
|
+
/** Read once, when the chat first opens. Default: the browser's language. */
|
|
111
|
+
locale?: string;
|
|
112
|
+
/**
|
|
113
|
+
* The user's Incorta tenant, read once. Sign-in then opens that tenant's page: an SSO
|
|
114
|
+
* tenant goes straight to its identity provider. Default: Incorta asks for the tenant.
|
|
115
|
+
* Signing in there ends a session the browser holds on another tenant.
|
|
116
|
+
*/
|
|
117
|
+
tenant?: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export type IncortaChatProps = DirectChatProps | WindowedChatProps | EmbeddedChatProps;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The chat as a React component. Direct: it fills its box; `connection`,
|
|
124
|
+
* `chat`, `actions`, `events`, `instanceId`, `assetsUrl` and `timeoutMs` are
|
|
125
|
+
* read once at mount, and the other props update the running chat. With
|
|
126
|
+
* `modes` or `defaultMode` (and no `window`), or embedded (`embedUrl`): it
|
|
127
|
+
* draws its own window.
|
|
128
|
+
*/
|
|
129
|
+
export function IncortaChat(props: IncortaChatProps): ReactElement;
|