@ahmedramez-org/chat 0.0.0-stage → 0.6.0-beta.1
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/README.md
CHANGED
|
@@ -1,3 +1,374 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @incorta/chat
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Incorta's chat for any app on any framework: `<IncortaChat />` for React, `mountChat()` for
|
|
4
|
+
everything else, one `<script>` tag for a site with no build step, and `embed.html` for an iframe
|
|
5
|
+
or a mobile WebView.
|
|
6
|
+
|
|
7
|
+
The chat always runs as a prebuilt Module Federation bundle with its own React and isolated
|
|
8
|
+
styles. The app's bundler never compiles it, so it never collides with the app's React, its
|
|
9
|
+
dependencies or its CSS. The connector adds about 6 KB gzipped to an app (half of it the Incorta
|
|
10
|
+
Intelligence mark its loader draws), and the React component about 7 KB more. The chat build
|
|
11
|
+
itself is not in this package: IncortaCopilot serves it (below).
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @incorta/chat
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
During the trial the package is published as `@ahmedramez-org/chat`. Install it under the final
|
|
20
|
+
name, so imports do not change later:
|
|
21
|
+
|
|
22
|
+
```json
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@incorta/chat": "npm:@ahmedramez-org/chat@<version>"
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`@beta` installs the newest prerelease. React 18 or 19 is needed only for `@incorta/chat/react`.
|
|
29
|
+
|
|
30
|
+
## React
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
import { IncortaChat } from '@incorta/chat/react';
|
|
34
|
+
|
|
35
|
+
<IncortaChat embedUrl={`${incortaUrl}/copilot/nexus-chat/embed.html`} theme="dark" />;
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
That is the embedded chat: it runs in an iframe served by Incorta, on the user's Incorta session,
|
|
39
|
+
so the app never holds a token. Use it for generated apps and other sites. The direct chat (below)
|
|
40
|
+
mounts into the page and shares its session; Incorta's own UIs use it.
|
|
41
|
+
|
|
42
|
+
### Embedded
|
|
43
|
+
|
|
44
|
+
The component draws its own window: an Incorta Intelligence launcher at the bottom right opens the
|
|
45
|
+
chat floating (a card lifted off the page, opening out of the launcher) or docked flush on the
|
|
46
|
+
right (the page makes room). On a `dir="rtl"` page the launcher and window sit on the left, and
|
|
47
|
+
follow a live `dir` change. The chat's top bar switches between the windows (Float, Dock to the
|
|
48
|
+
side, Enter full screen) and closes it back to the launcher. Below 768px wide the chat covers the
|
|
49
|
+
page. The chat is drawn smaller in a small window.
|
|
50
|
+
|
|
51
|
+
| Prop | What |
|
|
52
|
+
| ------------- | ------------------------------------------------------------------------------------ |
|
|
53
|
+
| `embedUrl` | Required. `<incorta>/copilot/nexus-chat/embed.html`. |
|
|
54
|
+
| `modes` | The windows allowed, in launcher order. Default: `floating`, `docked`, `fullscreen`. |
|
|
55
|
+
| `defaultMode` | Open on load in this window. Default: only the launcher. |
|
|
56
|
+
| `theme` | `light` (default) or `dark`. Followed live. |
|
|
57
|
+
| `locale` | Read when the chat first opens. Default: the browser's language. |
|
|
58
|
+
| `tenant` | The user's Incorta tenant, exactly as Incorta names it. Read once. |
|
|
59
|
+
|
|
60
|
+
The Incorta cluster needs `iframe.inclusion.enabled`, and `frame-ancestors` set to the app's
|
|
61
|
+
domains. Where the browser keeps the Incorta session from the iframe (Safari, Firefox, private
|
|
62
|
+
windows), the chat shows Continue with Incorta: the browser asks the user to allow it once, and the
|
|
63
|
+
chat starts on their Incorta session. A user not signed in signs in through a small Incorta window
|
|
64
|
+
that closes itself afterwards. No browser setting is needed.
|
|
65
|
+
|
|
66
|
+
With `tenant`, that window opens the tenant's own sign-in: an SSO tenant goes straight to its
|
|
67
|
+
identity provider, and the others get the tenant filled in. A tenant Incorta does not have is noted
|
|
68
|
+
in the browser console, and sign-in asks for the tenant as it does without one. Other sign-in
|
|
69
|
+
options, under Sign in, opens Incorta's plain sign-in page (another tenant, or a password on an SSO
|
|
70
|
+
tenant). Pass the user's own tenant: signing in
|
|
71
|
+
there ends a session the browser holds on another tenant.
|
|
72
|
+
|
|
73
|
+
While the user clicks or types in the app, the component tells the chat at most every ten seconds,
|
|
74
|
+
so Incorta's idle timeout does not end the chat's session while the app is in use. If the session
|
|
75
|
+
ends anyway, the chat shows Sign in to Incorta.
|
|
76
|
+
|
|
77
|
+
### Direct
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
// Copilot answers 400 without the signed-in user's token: read it from Incorta's
|
|
81
|
+
// session, and read it again after a 401 or 403.
|
|
82
|
+
let session;
|
|
83
|
+
const incortaHeaders = () =>
|
|
84
|
+
(session ??= fetch('/incorta/service/user/isLoggedIn', { credentials: 'include' }).then(
|
|
85
|
+
async (response) => ({
|
|
86
|
+
Authorization: `Bearer ${(await response.json()).accessToken}`,
|
|
87
|
+
'X-XSRF-TOKEN': response.headers.get('X-XSRF-TOKEN') ?? '',
|
|
88
|
+
}),
|
|
89
|
+
));
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
<IncortaChat
|
|
94
|
+
connection={{
|
|
95
|
+
client: 'general',
|
|
96
|
+
url: '/incorta/copilot',
|
|
97
|
+
credentials: 'include',
|
|
98
|
+
headers: incortaHeaders, // read before each request
|
|
99
|
+
refreshAuth: async () => ((session = undefined), true), // after a 401 or 403: read again, retry
|
|
100
|
+
}}
|
|
101
|
+
theme="dark"
|
|
102
|
+
user={{ id: user.sub, name: user.name }}
|
|
103
|
+
onLoadError={(error) => setError(error.message)}
|
|
104
|
+
/>
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The chat fills the component's box; the app draws any frame around it. Until the chat is mounted,
|
|
108
|
+
a loader holds the box (after 300 ms), and it leaves at once when `onLoadError` fires, so the app's
|
|
109
|
+
own failure takes the box.
|
|
110
|
+
|
|
111
|
+
- `theme`, `locale` (default: the browser's), `direction`, `tenantId`, `user` and `window` update
|
|
112
|
+
the running chat.
|
|
113
|
+
- `connection`, `chat`, `actions`, `events`, `instanceId`, `assetsUrl` and `timeoutMs` are read
|
|
114
|
+
once, at mount.
|
|
115
|
+
- `environment`: an app that already builds the contract's environment passes it instead of
|
|
116
|
+
`theme`, `locale`, `direction`, `tenantId` and `user`.
|
|
117
|
+
- `window` hands the chat's window to the app: `{ docked, canDock, setDocked, close }` gives a dock
|
|
118
|
+
toggle and a close button; with `mode`, `modes` and `setMode` (read at mount) the chat shows a
|
|
119
|
+
button per other mode instead. A window named by `mode`, `modes`, `setMode` and `close` alone is
|
|
120
|
+
enough: the component derives the rest for an older chat. A contract `ChatWindow` is used as is.
|
|
121
|
+
- `onReady` receives the handle (`ask`, `attach`, `openSession`), and `null` once the chat
|
|
122
|
+
unmounts.
|
|
123
|
+
- `onLoadError` receives a load failure (`LOAD_TIMEOUT`, `LOAD_FAILED`), the chat's own mount error
|
|
124
|
+
(`MOUNT_FAILED`, `INCOMPATIBLE_HOST_CONTRACT`…), or a plain error when there is no `url` or
|
|
125
|
+
`assetsUrl`.
|
|
126
|
+
|
|
127
|
+
### Direct, in a window
|
|
128
|
+
|
|
129
|
+
Give `modes` or `defaultMode` instead of `window`, and the direct chat gets the embedded chat's
|
|
130
|
+
window: the launcher, floating, docked and full screen (full screen below 768px). The conversation
|
|
131
|
+
stays while the window is closed to the launcher. An app that passes `window` instead keeps drawing
|
|
132
|
+
its own frame.
|
|
133
|
+
|
|
134
|
+
```tsx
|
|
135
|
+
<IncortaChat
|
|
136
|
+
connection={{
|
|
137
|
+
client: 'general',
|
|
138
|
+
url: '/incorta/copilot',
|
|
139
|
+
credentials: 'include',
|
|
140
|
+
headers: incortaHeaders, // as above
|
|
141
|
+
refreshAuth: async () => ((session = undefined), true),
|
|
142
|
+
}}
|
|
143
|
+
theme="dark"
|
|
144
|
+
modes={['floating', 'docked', 'fullscreen']}
|
|
145
|
+
defaultMode="floating"
|
|
146
|
+
/>
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Any framework
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
import { mountChat } from '@incorta/chat';
|
|
153
|
+
|
|
154
|
+
const chat = await mountChat(element, {
|
|
155
|
+
connection: { client: 'general', url: '/incorta/copilot' },
|
|
156
|
+
environment: {
|
|
157
|
+
get: () => ({ theme: 'light', locale: navigator.language }),
|
|
158
|
+
subscribe: () => () => {},
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
chat.ask('What changed last month?');
|
|
162
|
+
chat.unmount();
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The options are the host contract (below), with `version` filled in for you. `connection` and
|
|
166
|
+
`environment` are required; `chat`, `actions`, `events` and `window` are optional, and the chat
|
|
167
|
+
hides what a host leaves out. The chat fills `element`. Until it is mounted a loader holds the
|
|
168
|
+
element (after 300 ms, in the environment's theme), and it leaves at once if the chat cannot load.
|
|
169
|
+
Mount in the browser only, never during server rendering.
|
|
170
|
+
|
|
171
|
+
`chat: { contexts: ['business-view', 'ai-container', 'file'] }` lets users attach contexts, in that
|
|
172
|
+
menu order. `'file'` uploads; each other built-in kind (`business-view`, `physical-table`,
|
|
173
|
+
`ai-container`, `knowledge-store`) is loaded by the chat from Incorta through
|
|
174
|
+
`connection.incortaApi` (for example `{ type: 'bff', baseUrl: '/incorta/bff/v1' }`). Without
|
|
175
|
+
`incortaApi`, a bare kind is left out with a console warning; the public API has no Knowledge
|
|
176
|
+
Stores. A `ContextSource` of a kind replaces the chat's loader for it.
|
|
177
|
+
|
|
178
|
+
`loadChat(source)` only loads the chat and resolves to its `mount`. Call it when the page is idle
|
|
179
|
+
to make the first open instant. It loads once per source, retries once, and gives up after
|
|
180
|
+
`timeoutMs` (default 20 s, counted while the page is visible: a hidden tab gets a fresh deadline
|
|
181
|
+
once it shows). A failed load is forgotten, so the next call tries again. `forgetChat(source)`
|
|
182
|
+
forgets a loaded chat on purpose, and `loadedChatVersion()` names the loaded build for error
|
|
183
|
+
reports.
|
|
184
|
+
|
|
185
|
+
`mountChat` and `<IncortaChat>` mount the chat the same way: a chat whose `mount` throws, or that
|
|
186
|
+
reports to `events.onFatal`, is forgotten so a retry fetches it afresh; a session hold
|
|
187
|
+
(`events.keepSessionAlive`) the chat still has when it goes is released, once; and the handle
|
|
188
|
+
always has `attach` and `openSession` (on an older chat, `attach` returns `false` and
|
|
189
|
+
`openSession` does nothing).
|
|
190
|
+
|
|
191
|
+
## Any website, no build step
|
|
192
|
+
|
|
193
|
+
```html
|
|
194
|
+
<script src="https://<incorta>/incorta/copilot/nexus-chat/widget.js" defer></script>
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Incorta serves this script next to `embed.html`; it is not in this package. It draws the Incorta
|
|
198
|
+
Intelligence launcher at the bottom right (bottom left on a `dir="rtl"` page) and opens the
|
|
199
|
+
embedded chat floating, docked or full screen, on the user's Incorta session, exactly as
|
|
200
|
+
`<IncortaChat embedUrl>` does. It is one classic script with no imports, so a page on any origin
|
|
201
|
+
loads it without CORS. The Incorta setup is the embedded chat's (above).
|
|
202
|
+
|
|
203
|
+
Optional `data-` attributes on the script tag:
|
|
204
|
+
|
|
205
|
+
| Attribute | What |
|
|
206
|
+
| ------------------- | ---------------------------------------------------------------------------- |
|
|
207
|
+
| `data-theme` | `light` (default) or `dark`. |
|
|
208
|
+
| `data-modes` | The windows allowed, comma-separated, in launcher order. Default: all three. |
|
|
209
|
+
| `data-default-mode` | Open on load in this window. Default: only the launcher. |
|
|
210
|
+
| `data-locale` | Read when the chat first opens. Default: the browser's language. |
|
|
211
|
+
| `data-tenant` | The Incorta tenant sign-in opens. Default: Incorta asks. |
|
|
212
|
+
|
|
213
|
+
The page drives it afterwards through `window.IncortaChat`:
|
|
214
|
+
|
|
215
|
+
```js
|
|
216
|
+
window.IncortaChat.open();
|
|
217
|
+
window.IncortaChat.setMode('docked');
|
|
218
|
+
window.IncortaChat.setTheme('dark');
|
|
219
|
+
window.IncortaChat.close();
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## An iframe or a mobile WebView
|
|
223
|
+
|
|
224
|
+
`<IncortaChat embedUrl>` and `widget.js` place `<incorta>/copilot/nexus-chat/embed.html` for you.
|
|
225
|
+
A page or an app that places it itself talks to it with `postMessage`. Every message names itself
|
|
226
|
+
in `incortaChat`; `@incorta/chat/bridge` types them (`ToHost`, `ToChat`) and its `parse` reads one
|
|
227
|
+
off the wire with every field checked.
|
|
228
|
+
|
|
229
|
+
**The handshake.** The page says `{ incortaChat: 'loaded', v }` as soon as it runs; `v` is the
|
|
230
|
+
contract version (below). Compare it with `CONTRACT_VERSION` from `@incorta/chat/contract`. On a
|
|
231
|
+
mismatch, show your own "update needed" message and send nothing else to that page. Every message
|
|
232
|
+
the page sends carries `v`, and `v` is checked only at the handshake.
|
|
233
|
+
|
|
234
|
+
On the user's Incorta session (the cookie embed), the page follows `style`
|
|
235
|
+
(`{ incortaChat: 'style', theme, locale, window }`), says `ready` when the chat is mounted, and
|
|
236
|
+
sends `mode`, `dock` and `close` when the user presses the chat's window buttons. While the user
|
|
237
|
+
works in your page, send `{ incortaChat: 'active' }` (at most every ten seconds is enough), so
|
|
238
|
+
Incorta's idle timeout does not end the chat's session.
|
|
239
|
+
|
|
240
|
+
**Token mode, for apps.** A mobile app, a desktop app or a browser extension has no Incorta session
|
|
241
|
+
of its own. It signs the user in with Incorta OAuth and shows `embed.html?auth=token`. Use
|
|
242
|
+
`createTokenModeLink` from `@incorta/chat/host` for the app's end: it answers `loaded` with the
|
|
243
|
+
app's `hello` when it gives one (stamped with `v`), hands the chat a token when it asks, and tells the app when the
|
|
244
|
+
user is signed out. A page of another contract version gets no `hello` and no token, and
|
|
245
|
+
`on.ready` hears the launch failed `incompatible`. The link hears nothing before the page's
|
|
246
|
+
`loaded`, so make it before the page loads. When the page loads again, the link drops what the
|
|
247
|
+
old page was still waiting for (a token read that ends late is for the page that asked). The app owns the transport. In an extension's
|
|
248
|
+
frame:
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
import { createTokenModeLink } from '@incorta/chat/host';
|
|
252
|
+
|
|
253
|
+
/** One frame, one link. Returns what closes them. */
|
|
254
|
+
function openChat() {
|
|
255
|
+
const frame = document.createElement('iframe');
|
|
256
|
+
frame.src = chatPage; // embed.html?auth=token&tenant=…
|
|
257
|
+
const link = createTokenModeLink({
|
|
258
|
+
// A removed frame has no window: a token read that ends late reaches no other page.
|
|
259
|
+
send: (message) => frame.contentWindow?.postMessage(message, chatOrigin),
|
|
260
|
+
accessToken: (options) => client.getAccessToken(options),
|
|
261
|
+
// Nor does a removed frame sign the app out.
|
|
262
|
+
onSignedOut: () => frame.isConnected && signOut(),
|
|
263
|
+
hello: { surfaces: [] },
|
|
264
|
+
on: { ready: (mounted, surfaces, reason) => (reason ? showFailure(reason) : show(mounted)) },
|
|
265
|
+
});
|
|
266
|
+
const receive = (event: MessageEvent) => {
|
|
267
|
+
if (event.source === frame.contentWindow && event.origin === chatOrigin)
|
|
268
|
+
link.receive(event.data);
|
|
269
|
+
};
|
|
270
|
+
window.addEventListener('message', receive);
|
|
271
|
+
// After the link is made: it hears nothing before the page's `loaded`.
|
|
272
|
+
document.body.append(frame);
|
|
273
|
+
return () => {
|
|
274
|
+
window.removeEventListener('message', receive);
|
|
275
|
+
frame.remove();
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
To load the chat again, close it and open a new one. Never reload a frame in place (`frame.src`
|
|
281
|
+
again): a page cannot see a frame's page leave, so a token the old page asked for could reach the
|
|
282
|
+
new one before it has said its version.
|
|
283
|
+
|
|
284
|
+
In a frame, the token-mode page hears only an extension's page (`chrome-extension:`,
|
|
285
|
+
`moz-extension:`, `safari-web-extension:`), never a website. A WebView or an Electron window uses
|
|
286
|
+
the page's native bridge instead; the host integration guide in this package's repository
|
|
287
|
+
describes each one.
|
|
288
|
+
|
|
289
|
+
## Where the chat comes from
|
|
290
|
+
|
|
291
|
+
| Mode | Options | Chat version | App setup |
|
|
292
|
+
| ------------------------ | --------------------- | ------------------------- | -------------------------------------------------------- |
|
|
293
|
+
| Always updated (default) | `connection.url` only | what that Copilot runs | none: Copilot serves it under `/copilot/nexus-chat/` |
|
|
294
|
+
| Pinned | `assetsUrl: '/chat/'` | the release you extracted | serve that release's chat build, extracted, at that path |
|
|
295
|
+
|
|
296
|
+
`connection.url` is IncortaCopilot's root as the page reaches it (`/incorta/copilot` on Incorta),
|
|
297
|
+
and the chat loads from under it. For a pinned copy, each GitHub Release attaches the build as
|
|
298
|
+
`nexus-chat-dist.tar.gz`. Serve its `remoteEntry.js` with revalidation (`no-cache`) and `assets/`
|
|
299
|
+
as immutable.
|
|
300
|
+
|
|
301
|
+
A pinned chat keeps talking to a Copilot that keeps moving; the default mode keeps the two in step.
|
|
302
|
+
|
|
303
|
+
## Everything in the package
|
|
304
|
+
|
|
305
|
+
React and other apps need only the first two imports. The rest serve hosts that load the chat
|
|
306
|
+
themselves, and apps that show `embed.html`.
|
|
307
|
+
|
|
308
|
+
| Import | What |
|
|
309
|
+
| --------------------------- | ------------------------------------------------------------------- |
|
|
310
|
+
| `@incorta/chat` | the connector (`mountChat`, `loadChat`, `forgetChat`) and the types |
|
|
311
|
+
| `@incorta/chat/react` | `<IncortaChat />` for React 18 and 19 |
|
|
312
|
+
| `@incorta/chat/contract` | the host contract's values (`CONTRACT_VERSION`, `isChatError`…) |
|
|
313
|
+
| `@incorta/chat/conformance` | checks a host's options against the contract |
|
|
314
|
+
| `@incorta/chat/host` | helpers (`createTokenModeLink`, `createElementRootAdapter`…) |
|
|
315
|
+
| `@incorta/chat/bridge` | the messages of `embed.html` (`parse`, `ToHost`, `ToChat`) |
|
|
316
|
+
|
|
317
|
+
## The host contract
|
|
318
|
+
|
|
319
|
+
`mountChat`, `<IncortaChat>` and a host that loads the chat itself all pass one options object,
|
|
320
|
+
`ChatOptions`: `version`, `connection` (how the chat reaches Copilot), `environment` (theme,
|
|
321
|
+
locale, user), and the optional `chat`, `actions`, `events` and `window`. It is plain data and
|
|
322
|
+
functions: no React node, store or provider crosses it, which is what lets the chat run its own
|
|
323
|
+
React. A host that loads the chat itself imports the contract from `@incorta/chat/contract` and
|
|
324
|
+
runs `@incorta/chat/conformance` against the build it loads.
|
|
325
|
+
|
|
326
|
+
## Versions: when a host must act
|
|
327
|
+
|
|
328
|
+
Two numbers, and only one asks anything of a host.
|
|
329
|
+
|
|
330
|
+
- **The package version** moves on every release. Updating it needs no change in the app.
|
|
331
|
+
- **`CONTRACT_VERSION`** (in `@incorta/chat/contract`) names the host contract and the `v` of the
|
|
332
|
+
`embed.html` messages. It moves only when a release breaks the contract: a member removed,
|
|
333
|
+
renamed, narrowed or made required, or a meaning changed. Additions keep the number. When it
|
|
334
|
+
moves, update `@incorta/chat` and adapt the app to the release notes.
|
|
335
|
+
|
|
336
|
+
When the app and the chat it loads disagree:
|
|
337
|
+
|
|
338
|
+
- `mountChat` or `<IncortaChat>` (direct): the chat refuses to mount with
|
|
339
|
+
`INCOMPATIBLE_HOST_CONTRACT`. It accepts a range, `MIN_CONTRACT_VERSION` to `CONTRACT_VERSION`,
|
|
340
|
+
so a newer chat can keep older hosts mounting.
|
|
341
|
+
- `<IncortaChat embedUrl>` or `widget.js`: the window shows "Incorta chat needs an update" instead
|
|
342
|
+
of the chat.
|
|
343
|
+
- An app in token mode: the launch fails `incompatible`, which the app shows as "Update needed".
|
|
344
|
+
|
|
345
|
+
## Errors
|
|
346
|
+
|
|
347
|
+
The chat's errors and the connector's load failures are named `ChatError` and carry a `code`. Test
|
|
348
|
+
them with `isChatError(error, code?)` from `@incorta/chat/contract`: it reads `name` and `code`, so
|
|
349
|
+
it works across bundles, where `instanceof` does not, and with an earlier chat's errors too (a
|
|
350
|
+
host that loads one still learns it is `INCOMPATIBLE_HOST_CONTRACT`).
|
|
351
|
+
|
|
352
|
+
| Code | When |
|
|
353
|
+
| ---------------------------- | ------------------------------------------------------------------ |
|
|
354
|
+
| `LOAD_TIMEOUT` | the chat did not load before `timeoutMs` |
|
|
355
|
+
| `LOAD_FAILED` | the load failed, and so did its retry (the failure is its `cause`) |
|
|
356
|
+
| `INCOMPATIBLE_HOST_CONTRACT` | the host's `version` is outside the range the chat mounts |
|
|
357
|
+
| `ALREADY_MOUNTED` | the element already holds a mounted chat |
|
|
358
|
+
| `MOUNT_FAILED` | the chat could not start |
|
|
359
|
+
| `RENDER_FAILED` | the chat failed while rendering, outside every contained region |
|
|
360
|
+
|
|
361
|
+
`events.onError(error, context)` also reports codes made from its `context.scope` (`TASK`, …), so
|
|
362
|
+
`code` is a `string`.
|
|
363
|
+
|
|
364
|
+
```ts
|
|
365
|
+
import { mountChat } from '@incorta/chat';
|
|
366
|
+
import { isChatError } from '@incorta/chat/contract';
|
|
367
|
+
|
|
368
|
+
try {
|
|
369
|
+
await mountChat(element, options);
|
|
370
|
+
} catch (error) {
|
|
371
|
+
if (isChatError(error, 'INCOMPATIBLE_HOST_CONTRACT')) showUpdateNeeded();
|
|
372
|
+
else showRetry();
|
|
373
|
+
}
|
|
374
|
+
```
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The embed page's message protocol (`embed.html`, HOST-INTEGRATION §4c): what the chat page
|
|
3
|
+
* and the page or app that shows it say to each other, in both directions. Framework-free,
|
|
4
|
+
* like the rest of the contract, so the chat and every app compile against this one file.
|
|
5
|
+
*
|
|
6
|
+
* `@incorta/chat/bridge`
|
|
7
|
+
*
|
|
8
|
+
* Every message names itself in `incortaChat`. The chat page and the cookie embed's window
|
|
9
|
+
* stamp `v: CONTRACT_VERSION` (`./index`) on every message they send, `createTokenModeLink` on
|
|
10
|
+
* its `hello`. `v` is checked at the handshake only:
|
|
11
|
+
* - the app refuses a `loaded` whose `v` is not its `CONTRACT_VERSION` (a missing one
|
|
12
|
+
* included): the launch fails `incompatible` and the page gets no `hello` and no token
|
|
13
|
+
* (nor does a page that has not said `loaded` yet: the app hears nothing before it);
|
|
14
|
+
* - the chat page refuses such a `hello`: `ready` with `mounted: false` and reason
|
|
15
|
+
* `incompatible`, and the chat never starts on that page (one already up ends).
|
|
16
|
+
* Every other message is read whatever its `v`, and an unknown one is ignored.
|
|
17
|
+
*/
|
|
18
|
+
import { type HostTheme, type ChatUserActivity, type ChatWindowMode, type ChatWindowState, type SurfaceIntent, type SurfaceName, type SurfaceState } from './index.js';
|
|
19
|
+
import { type LaunchFailure, type LaunchStep } from './launch.js';
|
|
20
|
+
/** What the chat page tells the page or app that shows it. */
|
|
21
|
+
export type ToHost = {
|
|
22
|
+
incortaChat: 'loaded';
|
|
23
|
+
v?: number;
|
|
24
|
+
} | {
|
|
25
|
+
incortaChat: 'ready';
|
|
26
|
+
mounted: boolean;
|
|
27
|
+
surfaces?: SurfaceName[];
|
|
28
|
+
reason?: LaunchFailure;
|
|
29
|
+
topInset?: true;
|
|
30
|
+
} | {
|
|
31
|
+
incortaChat: 'progress';
|
|
32
|
+
step: LaunchStep;
|
|
33
|
+
} | {
|
|
34
|
+
incortaChat: 'surfaces';
|
|
35
|
+
state: SurfaceState;
|
|
36
|
+
} | {
|
|
37
|
+
incortaChat: 'activity';
|
|
38
|
+
activity: ChatUserActivity['type'];
|
|
39
|
+
} | {
|
|
40
|
+
incortaChat: 'mode';
|
|
41
|
+
mode: ChatWindowMode;
|
|
42
|
+
} | {
|
|
43
|
+
incortaChat: 'dock';
|
|
44
|
+
docked: boolean;
|
|
45
|
+
} | {
|
|
46
|
+
incortaChat: 'close';
|
|
47
|
+
} | {
|
|
48
|
+
incortaChat: 'authRequired';
|
|
49
|
+
reason?: 'rejected';
|
|
50
|
+
} | {
|
|
51
|
+
incortaChat: 'signedOut';
|
|
52
|
+
} | {
|
|
53
|
+
incortaChat: 'file';
|
|
54
|
+
name: string;
|
|
55
|
+
type: string;
|
|
56
|
+
data: string;
|
|
57
|
+
} | {
|
|
58
|
+
incortaChat: 'background';
|
|
59
|
+
color: string;
|
|
60
|
+
} | {
|
|
61
|
+
incortaChat: 'openAccount';
|
|
62
|
+
} | {
|
|
63
|
+
incortaChat: 'microphoneBlocked';
|
|
64
|
+
};
|
|
65
|
+
/** What the page or app tells the chat page. */
|
|
66
|
+
export type ToChat = {
|
|
67
|
+
incortaChat: 'style';
|
|
68
|
+
theme?: HostTheme;
|
|
69
|
+
locale?: string;
|
|
70
|
+
/** An older `<IncortaChat>` sizes the type itself. */
|
|
71
|
+
remBase?: number;
|
|
72
|
+
window?: ChatWindowState;
|
|
73
|
+
/**
|
|
74
|
+
* An app whose page rides the keyboard (moved up, not resized): the keyboard's height in
|
|
75
|
+
* CSS px as it starts to rise, `0` as it starts to fall. The page's top that much is out
|
|
76
|
+
* of sight, so the intro centres below it.
|
|
77
|
+
*/
|
|
78
|
+
keyboardInset?: number;
|
|
79
|
+
/**
|
|
80
|
+
* An app that draws its bar over the page's top: the bar's height in CSS px. The
|
|
81
|
+
* conversation starts below it and scrolls under it; the intro, the canvas and the
|
|
82
|
+
* pinned question keep clear of it. `0` (or never sent): nothing covers the page.
|
|
83
|
+
*/
|
|
84
|
+
topInset?: number;
|
|
85
|
+
} | {
|
|
86
|
+
incortaChat: 'active';
|
|
87
|
+
} | {
|
|
88
|
+
incortaChat: 'auth';
|
|
89
|
+
token: string;
|
|
90
|
+
expiresAt?: number;
|
|
91
|
+
} | {
|
|
92
|
+
incortaChat: 'signOut';
|
|
93
|
+
} | {
|
|
94
|
+
incortaChat: 'hello';
|
|
95
|
+
/** The app's `CONTRACT_VERSION` (`createTokenModeLink` stamps it). */
|
|
96
|
+
v?: number;
|
|
97
|
+
surfaces: SurfaceName[];
|
|
98
|
+
files?: boolean;
|
|
99
|
+
conversationLoader?: boolean;
|
|
100
|
+
account?: boolean;
|
|
101
|
+
focusNewChat?: boolean;
|
|
102
|
+
systemFont?: boolean;
|
|
103
|
+
user?: {
|
|
104
|
+
name: string;
|
|
105
|
+
};
|
|
106
|
+
} | {
|
|
107
|
+
incortaChat: 'intent';
|
|
108
|
+
intent: SurfaceIntent;
|
|
109
|
+
};
|
|
110
|
+
export type BridgeMessage = ToHost | ToChat;
|
|
111
|
+
/**
|
|
112
|
+
* Reads one message off the bridge, in either direction: the message, typed and with
|
|
113
|
+
* every field checked, or `undefined` for anything that is not one (another sender's
|
|
114
|
+
* message, an unknown name, a token that is not a string). A `surfaces` state is checked
|
|
115
|
+
* surface by surface: one that is not an object, lacks its lists or lists something that is
|
|
116
|
+
* not an object is dropped; the fields of a row are not checked. Each side handles the names
|
|
117
|
+
* it knows and ignores the rest.
|
|
118
|
+
*/
|
|
119
|
+
export declare function parse(data: unknown): BridgeMessage | undefined;
|