@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.
@@ -0,0 +1,922 @@
1
+ /** The host contract version: a host passes it as `version`, the embed bridge stamps it as `v`. */
2
+ export declare const CONTRACT_VERSION: 1;
3
+ /**
4
+ * The oldest host contract version the chat still mounts: a host's `version` from this one to
5
+ * `CONTRACT_VERSION` is accepted, anything else is `INCOMPATIBLE_HOST_CONTRACT`.
6
+ */
7
+ export declare const MIN_CONTRACT_VERSION: 1;
8
+ export type ContractVersion = typeof CONTRACT_VERSION;
9
+ export type HostTheme = 'light' | 'dark';
10
+ export type HostDirection = 'ltr' | 'rtl';
11
+ export type ArtifactPlacement = 'inline' | 'canvas';
12
+ export type ArtifactView = 'chart' | 'table';
13
+ export interface ChatConnection {
14
+ /**
15
+ * The IncortaCopilot client this host is (`general` for Incorta, `hikari`
16
+ * for Hikari). Copilot picks the welcome text, agents and contexts from it.
17
+ */
18
+ client: string;
19
+ /** IncortaCopilot's root, for example `/incorta/copilot` (Incorta) or `/copilot` (Hikari). */
20
+ url: string;
21
+ /** Agent used when the Copilot handshake names no default agent for this client. */
22
+ agent?: string;
23
+ /** Headers every request carries (auth, CSRF). Re-read before each attempt. */
24
+ headers?(): Readonly<Record<string, string>> | Promise<Readonly<Record<string, string>>>;
25
+ credentials?: RequestCredentials;
26
+ /**
27
+ * Called once after a 401/403; resolve `true` when a retry can succeed.
28
+ * Absent: the failure surfaces to the user.
29
+ */
30
+ refreshAuth?(): Promise<boolean>;
31
+ /**
32
+ * How the chat reaches the Incorta API for built-in kinds other than `'file'` in
33
+ * `chat.contexts`, under the headers, credentials and `refreshAuth` above.
34
+ * - `'bff'`: Incorta's internal BFF (web-react: `baseUrl: '/incorta/bff/v1'`;
35
+ * the session headers carry the user).
36
+ * - `'public'`: the public API v2, rooted at `/api/v2/{tenant}`.
37
+ */
38
+ incortaApi?: {
39
+ type: 'bff' | 'public';
40
+ baseUrl: string;
41
+ };
42
+ /** Poll tuning; hosts normally omit both (defaults 1500 ms / 1000 ms). */
43
+ pollIntervalMs?: number;
44
+ initialPollDelayMs?: number;
45
+ }
46
+ export interface HostUser {
47
+ id?: string | number;
48
+ name?: string;
49
+ }
50
+ /**
51
+ * The languages the chat speaks, each named in itself, with the tag a host sends as `locale`
52
+ * to choose it (any tag of the same language works too; others get English). For a host that
53
+ * offers the choice, as the app's account page does.
54
+ */
55
+ export declare const CHAT_LANGUAGES: readonly {
56
+ locale: string;
57
+ label: string;
58
+ }[];
59
+ /**
60
+ * Plain host state. `get` may return a fresh object on every call: the chat
61
+ * compares the fields itself.
62
+ */
63
+ export interface ChatEnvironmentSnapshot {
64
+ theme: HostTheme;
65
+ locale: string;
66
+ /** Absent: follows `locale` (Arabic, Hebrew, Persian and Urdu are right-to-left). */
67
+ direction?: HostDirection;
68
+ tenantId?: string;
69
+ user?: HostUser;
70
+ }
71
+ export interface ChatEnvironment {
72
+ get(): ChatEnvironmentSnapshot;
73
+ /** Call `onChange` whenever `get` may return something different. */
74
+ subscribe(onChange: () => void): () => void;
75
+ }
76
+ export type ContextKind = 'business-view' | 'physical-table' | 'insight' | 'file' | 'knowledge-store' | 'ai-container';
77
+ /** One row in a context browser. */
78
+ export interface ContextItem {
79
+ id: string;
80
+ label: string;
81
+ description?: string;
82
+ /** A folder the user can open; its children load with `parentId: id`. */
83
+ hasChildren?: boolean;
84
+ /** Shown greyed out with this reason. */
85
+ disabledReason?: string;
86
+ /**
87
+ * What Copilot receives when the item is attached, sent as
88
+ * `{ type, data: dataset }` (`type` follows the kind: `business_view`,
89
+ * `insight`, `space`, `ai_container`; a physical table travels as a
90
+ * `business_view`, as Copilot reads it). An item without one cannot be picked.
91
+ */
92
+ dataset?: Readonly<Record<string, unknown>>;
93
+ }
94
+ /** A host-owned context browser: the host loads the rows, the chat does the rest. */
95
+ export interface ContextSource {
96
+ kind: Exclude<ContextKind, 'file'>;
97
+ load(request: {
98
+ parentId?: string;
99
+ query: string;
100
+ }, signal: AbortSignal): Promise<readonly ContextItem[]>;
101
+ /**
102
+ * Re-reads an attached item's `dataset` before every send (a retry asks
103
+ * again), so the question carries what the host shows now: a dashboard
104
+ * insight re-applies the current runtime filters. A rejection fails the
105
+ * send. Absent: the dataset goes as picked.
106
+ */
107
+ resolve?(dataset: Readonly<Record<string, unknown>>, signal: AbortSignal): Promise<Readonly<Record<string, unknown>>>;
108
+ }
109
+ /**
110
+ * The built-in kinds the chat handles itself: its upload browser and contexts
111
+ * loaded through `connection.incortaApi`. `'insight'` is not one: insights come
112
+ * from the dashboard the host has open, so only a host source can list them.
113
+ */
114
+ export declare const BUILT_IN_CONTEXT_KINDS: readonly ["file", "business-view", "physical-table", "ai-container", "knowledge-store"];
115
+ export type BuiltInContextKind = (typeof BUILT_IN_CONTEXT_KINDS)[number];
116
+ /**
117
+ * One entry of the add-context menu; `chat.contexts` lists them in menu order.
118
+ *
119
+ * - `'file'`: the chat's own upload browser.
120
+ * - Another built-in kind (`'business-view'`, …): the chat loads it from
121
+ * `connection.incortaApi`. Without `incortaApi`, or for a kind its API does not
122
+ * offer (`'knowledge-store'` has no public API), the kind is left out of
123
+ * the menu with a console warning.
124
+ * - A `ContextSource`: the host loads the rows. A source of a built-in kind
125
+ * replaces the chat's loader for that kind.
126
+ *
127
+ * Name each kind once. A host that lists a bare kind and a source of the same
128
+ * kind fails conformance; the chat still shows the kind once, as the host's
129
+ * source, in the source's place. Any other string is dropped.
130
+ *
131
+ * Migrating: code that told a source apart with `spec === 'file'` no longer
132
+ * compiles, since a bare string may now be a kind; narrow with
133
+ * `typeof spec === 'string'`. Pass bare kinds only to a chat that has them
134
+ * (this release or later): an older chat cannot render them.
135
+ */
136
+ export type ContextSpec = BuiltInContextKind | ContextSource;
137
+ export interface ColumnSummary {
138
+ id: string;
139
+ name: string;
140
+ label?: string;
141
+ type?: string;
142
+ }
143
+ export interface ValueMatch {
144
+ value: string | number | boolean | null;
145
+ label: string;
146
+ fieldId?: string;
147
+ fieldLabel?: string;
148
+ dataType?: string;
149
+ complete?: boolean;
150
+ }
151
+ /** The composer trigger characters the chat can serve itself. */
152
+ export declare const BUILT_IN_TRIGGERS: readonly ["@", "#"];
153
+ export type BuiltInTrigger = (typeof BUILT_IN_TRIGGERS)[number];
154
+ /**
155
+ * A composer trigger the host serves. Both search inside the one attached
156
+ * business view (`viewId` is that context's `id`); with none or several
157
+ * attached the character stays plain text.
158
+ *
159
+ * - `@` mentions a column. Asked once per view; the chat filters as you type.
160
+ * - `#` searches the view's values for the typed text.
161
+ */
162
+ export type TriggerSource = {
163
+ char: '@';
164
+ search(request: {
165
+ viewId: string;
166
+ }, signal: AbortSignal): Promise<readonly ColumnSummary[]>;
167
+ } | {
168
+ char: '#';
169
+ search(request: {
170
+ viewId: string;
171
+ query: string;
172
+ limit: number;
173
+ }, signal: AbortSignal): Promise<readonly ValueMatch[]>;
174
+ };
175
+ /**
176
+ * One composer trigger; `chat.triggers` lists them.
177
+ *
178
+ * - A bare `'@'` or `'#'`: the chat serves it itself through a BFF
179
+ * `connection.incortaApi`, across every attached business view and physical
180
+ * table. The user can narrow to one view, then one column, and `#` searches
181
+ * text columns only. Without a BFF `incortaApi` the character is left out
182
+ * with a console warning.
183
+ * - A `TriggerSource`: the host serves it, for one attached view.
184
+ *
185
+ * List each character once. Migrating: code that read `trigger.char` must
186
+ * narrow with `typeof trigger === 'string'` first. Pass bare characters only
187
+ * to a chat that has them (this release or later): an older chat ignores them.
188
+ */
189
+ export type TriggerSpec = BuiltInTrigger | TriggerSource;
190
+ /** Switches for things the chat builds itself. Each one defaults to `true`. */
191
+ export interface ChatFeatures {
192
+ /**
193
+ * Follow running answers over one SSE stream instead of polling. A backend
194
+ * that refuses the stream falls back to polling.
195
+ */
196
+ streaming?: boolean;
197
+ /**
198
+ * `false` when the host page's CSP blocks the `blob:` frame a PDF preview
199
+ * needs (Hikari serves `frame-src 'none'`); PDF cards then offer Download.
200
+ */
201
+ inlinePdfPreview?: boolean;
202
+ /**
203
+ * While a conversation opens (a switch, a restore), the Incorta Intelligence mark and a gradient
204
+ * bar show in the middle of the transcript. For a host that draws the header itself, so the topbar's
205
+ * loading line is not on screen. Absent or `false`: the transcript stays empty ground under
206
+ * that line.
207
+ */
208
+ conversationLoader?: boolean;
209
+ /**
210
+ * A new chat's field takes the focus as it opens on a touch screen too, its keyboard with it.
211
+ * For a host whose page is the chat (the iOS app). Absent or `false`: on a touch screen the
212
+ * keyboard waits for the user's tap.
213
+ */
214
+ focusNewChat?: boolean;
215
+ /**
216
+ * The chat's text in the device's own font (`system-ui`: San Francisco on an iPhone), beside the
217
+ * system's own chrome. For a native app's page. Absent or `false`: Incorta's font (Roboto).
218
+ */
219
+ systemFont?: boolean;
220
+ }
221
+ export interface ChatArtifact {
222
+ id: string;
223
+ /**
224
+ * Artifact type. New kinds SHOULD use the namespaced `vendor.name` form
225
+ * (e.g. `incorta.insight`, `hikari.dashboard`) so two hosts can never
226
+ * collide on a bare name; `parseArtifactKind` reads that convention.
227
+ * Existing bare kinds (`insight`, …) remain valid.
228
+ */
229
+ kind: string;
230
+ /**
231
+ * Version of the payload shape under `data`, independent of the contract
232
+ * version. A renderer that does not recognise a kind's schemaVersion must
233
+ * decline it synchronously (`canRender` → false) so the portable fallback
234
+ * renders — never guess at a payload. Absent means "unversioned legacy
235
+ * payload", which hosts may continue to accept.
236
+ */
237
+ schemaVersion?: number;
238
+ data: unknown;
239
+ metadata?: Readonly<Record<string, unknown>>;
240
+ }
241
+ /**
242
+ * Reads the namespaced-kind convention. Returns null for bare legacy kinds.
243
+ * Framework-free on purpose: hosts use this inside `canRender` to route by
244
+ * vendor without re-implementing the convention.
245
+ */
246
+ export declare function parseArtifactKind(kind: string): {
247
+ namespace: string;
248
+ name: string;
249
+ } | null;
250
+ /**
251
+ * True when the artifact carries the given payload schema version (or is an
252
+ * unversioned legacy payload and `acceptUnversioned` allows it). The guard a
253
+ * host renderer should call first in `canRender`.
254
+ */
255
+ export declare function artifactMatchesSchema(artifact: Pick<ChatArtifact, 'schemaVersion'>, supportedVersion: number, options?: {
256
+ acceptUnversioned?: boolean;
257
+ }): boolean;
258
+ /** The appearance the remote is rendering under, so a host renderer can match it without reading the DOM. */
259
+ export interface HostArtifactRendererAppearance {
260
+ theme: HostTheme;
261
+ direction: HostDirection;
262
+ locale: string;
263
+ }
264
+ export interface HostArtifactRenderRequest {
265
+ artifact: ChatArtifact;
266
+ messageId: string;
267
+ sessionId?: string;
268
+ userQuestion?: string;
269
+ taskId?: string;
270
+ placement: ArtifactPlacement;
271
+ /** Optional canvas presentation. Unsupported views fall back to the host's default. */
272
+ view?: ArtifactView;
273
+ /**
274
+ * Optional, per-mount callback: report the views this artifact actually has,
275
+ * including after asynchronous preparation. Never report after signal abort.
276
+ * Omit calls to retain the default canvas without a view-switching menu.
277
+ */
278
+ onSupportedViewsChange?(views: readonly ArtifactView[]): void;
279
+ isHistorical: boolean;
280
+ cachedData?: unknown;
281
+ metadata?: Readonly<Record<string, unknown>>;
282
+ /** Re-sent on every `update`: a theme switch arrives as an update with a new appearance. */
283
+ appearance?: HostArtifactRendererAppearance;
284
+ /** Aborted when the remote unmounts this renderer; stable across updates. */
285
+ signal?: AbortSignal;
286
+ /**
287
+ * Live answers only (absent when `isHistorical`): fields to merge into the
288
+ * artifact's `data` on the backend once its task finishes, so a reopened
289
+ * conversation replays the artifact without re-running it (an Incorta
290
+ * insight reports its `cached_data`). The first report per artifact wins.
291
+ */
292
+ onDataReady?(patch: Readonly<Record<string, unknown>>): void;
293
+ }
294
+ export interface HostArtifactRendererMount {
295
+ update(request: HostArtifactRenderRequest): void;
296
+ unmount(): void;
297
+ }
298
+ /** Host-native charts, painted into an element the chat owns. */
299
+ export interface HostArtifactRendererCapability {
300
+ /** Optional synchronous, side-effect-free title from a host-owned payload. No I/O or rendering. */
301
+ getTitle?(artifact: ChatArtifact): string | undefined;
302
+ canRender(request: HostArtifactRenderRequest): boolean;
303
+ mount(element: HTMLElement, request: HostArtifactRenderRequest): HostArtifactRendererMount;
304
+ }
305
+ /** What a host adds to one question on its way to Copilot. */
306
+ export interface SendAddition {
307
+ /** Replaces the text sent to Copilot; the user still sees what they typed. */
308
+ prompt?: string;
309
+ /** Extra `client_context` fields (a notebook's code `language`); `datasets` and `params` stay the chat's. */
310
+ clientContext?: Readonly<Record<string, unknown>>;
311
+ }
312
+ export interface ChatSetup {
313
+ /** What the user can attach. Absent or empty: no contexts. */
314
+ contexts?: readonly ContextSpec[];
315
+ /** Absent or empty: `@` and `#` stay plain text. `/` (agents) always works. */
316
+ triggers?: readonly TriggerSpec[];
317
+ /** Absent: tables use the chat's own renderer, other host kinds show as unsupported. */
318
+ renderers?: HostArtifactRendererCapability;
319
+ features?: ChatFeatures;
320
+ /**
321
+ * Called before each question is sent (a retry calls it again). A notebook
322
+ * wraps the active paragraph's code into the prompt and adds its language.
323
+ * A rejection fails the send. Absent: questions go as typed.
324
+ */
325
+ beforeSend?(request: {
326
+ prompt: string;
327
+ }): SendAddition | Promise<SendAddition>;
328
+ /**
329
+ * The absolute URL that reopens a saved conversation in this host (the
330
+ * instance root plus the conversation id, for example
331
+ * `https://host/incorta/?chat-session-id=<id>`, in the page query before any hash route, which in-app redirects never drop). The host reads that URL
332
+ * back and calls `handle.openSession`. Absent: no Copy link.
333
+ */
334
+ sessionLink?(sessionId: string): string;
335
+ /**
336
+ * The chat's controls this host draws itself (a native app's nav bar and attach
337
+ * sheet): the chat hides its own, publishes their state through `handle.surfaces` and
338
+ * takes the user's actions back as intents. Absent or empty: the chat draws everything.
339
+ */
340
+ surfaces?: {
341
+ owned: readonly SurfaceName[];
342
+ };
343
+ }
344
+ /** The picture a picker row shows; the host draws its own glyph for each. */
345
+ export type PickerIcon = 'businessView' | 'knowledgeStore' | 'aiContainer' | 'insight' | 'file' | 'folder' | 'recentFiles' | 'upload';
346
+ /** One row of the attach picker, as the chat publishes it. */
347
+ export interface PickerRow {
348
+ /** Opaque: the chat maps it back to its own item. */
349
+ id: string;
350
+ label: string;
351
+ /** A second line: a description, a schema. */
352
+ detail?: string;
353
+ icon?: PickerIcon;
354
+ /** Attached now: picking it again removes it. */
355
+ selected?: boolean;
356
+ /** Shown, not pickable (the agent does not take the kind, or the item cannot be attached). */
357
+ disabled?: boolean;
358
+ /** Opens a level of its own (`drill`) instead of being picked. */
359
+ hasChildren?: boolean;
360
+ }
361
+ /**
362
+ * The attach picker (the composer's `+`): its level, search text and rows. The chat
363
+ * owns it; the web popover and a host that draws the picker show the same state.
364
+ */
365
+ export interface PickerState {
366
+ /** Absent: closed. A new number each time it opens: an intent for an older one is dropped. */
367
+ pickerId?: number;
368
+ /** The level on screen. */
369
+ title?: string;
370
+ /** The level takes a search (`query`): a context source's level does, a file list does not. */
371
+ searchable: boolean;
372
+ query: string;
373
+ /** The levels drilled into, outermost first. */
374
+ path: readonly {
375
+ id: string;
376
+ label: string;
377
+ }[];
378
+ rows: readonly PickerRow[];
379
+ loading: boolean;
380
+ hasMore: boolean;
381
+ /** A line in place of rows: loading, could not load, nothing found. In the chat's locale. */
382
+ notice?: string;
383
+ /**
384
+ * What Upload file takes, while the level offers it: for a host that picks the files with its
385
+ * own chooser and sends them as `upload` (the chat still checks them as its file input does).
386
+ * `formats`: file extensions, lower case, no dot. `maxFiles`: how many more files it takes (the
387
+ * files already attached count), 0 when no more.
388
+ */
389
+ upload?: {
390
+ formats: readonly string[];
391
+ maxBytes: number;
392
+ maxFiles: number;
393
+ };
394
+ }
395
+ /** A file the host picked for Upload file, its bytes as base64. */
396
+ export interface PickedFile {
397
+ name: string;
398
+ /** Its media type; empty when unknown. */
399
+ type: string;
400
+ data: string;
401
+ }
402
+ type InPicker = {
403
+ surface: 'picker';
404
+ pickerId: number;
405
+ };
406
+ /** What the user did in a host-drawn attach picker. Rows are named by the ids the chat published. */
407
+ export type PickerIntent = {
408
+ surface: 'picker';
409
+ type: 'open';
410
+ } | (InPicker & {
411
+ type: 'query';
412
+ query: string;
413
+ }) | (InPicker & {
414
+ type: 'drill' | 'pick';
415
+ id: string;
416
+ }) | (InPicker & {
417
+ type: 'back' | 'more' | 'dismiss';
418
+ }) | (InPicker & {
419
+ type: 'upload';
420
+ id: string;
421
+ files: readonly PickedFile[];
422
+ more?: boolean;
423
+ });
424
+ /** One conversation of the history list, as the chat publishes it. */
425
+ export interface HistoryRow {
426
+ /** Opaque: the chat maps it back to its own conversation. */
427
+ id: string;
428
+ /** In the chat's locale ("New chat" for one not named yet). */
429
+ title: string;
430
+ /** The conversation on screen. */
431
+ active: boolean;
432
+ /** An answer is being produced in it. */
433
+ running: boolean;
434
+ /** Saved by the backend, so it can be renamed. */
435
+ canRename: boolean;
436
+ /**
437
+ * It finished an answer the user has not opened yet (the chat's own rail shows a dot); the
438
+ * chat clears it once the conversation opens, and never sets it while `running`. Absent: read
439
+ * (and from a chat before it).
440
+ */
441
+ unread?: boolean;
442
+ }
443
+ /**
444
+ * The chat history (the drawer): one flat list, newest first, paged with `more`. The
445
+ * chat owns it; the host opens it with `open` and draws the rows only while `open`.
446
+ */
447
+ export interface HistoryState {
448
+ open: boolean;
449
+ /** The list takes a search (`query`): absent when the deployment turned search off. */
450
+ searchable: boolean;
451
+ /** The search text: its results replace the history in the list. */
452
+ query: string;
453
+ rows: readonly HistoryRow[];
454
+ loading: boolean;
455
+ hasMore: boolean;
456
+ /** A line in place of rows: nothing yet, no match, could not load. In the chat's locale. */
457
+ notice?: string;
458
+ }
459
+ /**
460
+ * What the user did in a host-drawn history list. Rows are named by the ids the chat
461
+ * published; `delete` is already confirmed by the host. `select` closes the list.
462
+ */
463
+ export type HistoryIntent = {
464
+ surface: 'history';
465
+ type: 'open';
466
+ } | {
467
+ surface: 'history';
468
+ type: 'dismiss';
469
+ } | {
470
+ surface: 'history';
471
+ type: 'more';
472
+ } | {
473
+ surface: 'history';
474
+ type: 'query';
475
+ query: string;
476
+ } | {
477
+ surface: 'history';
478
+ type: 'select';
479
+ id: string;
480
+ } | {
481
+ surface: 'history';
482
+ type: 'delete';
483
+ id: string;
484
+ } | {
485
+ surface: 'history';
486
+ type: 'rename';
487
+ id: string;
488
+ title: string;
489
+ };
490
+ /**
491
+ * The composer's model choice (its agent's one model option with values), for a host that draws
492
+ * the chooser itself: the chip stays in the composer and opens it.
493
+ */
494
+ export interface ModelState {
495
+ /** The user tapped the chip: show the chooser until a pick or a dismiss closes it. */
496
+ open: boolean;
497
+ /**
498
+ * While open: a new number each time the chip opens it. A host that closed its chooser itself
499
+ * shows it again for a new one, though `open` never changed. Absent from a chat before it.
500
+ */
501
+ openId?: number;
502
+ /**
503
+ * The models in the composer's order. `provider` names the mark the chat draws beside one
504
+ * (`openai`, `anthropic`, `gemini`, `mistral`, `deepseek`, `glm`, `kimi`); absent: the
505
+ * generic one. `disabled`: offered but not selectable now.
506
+ */
507
+ options: readonly {
508
+ value: string;
509
+ provider?: string;
510
+ disabled?: boolean;
511
+ }[];
512
+ /** The model the next question goes to. */
513
+ selected?: string;
514
+ }
515
+ /** What the user did in a host-drawn model chooser. `select` closes it, as `dismiss` does. */
516
+ export type ModelIntent = {
517
+ surface: 'model';
518
+ type: 'select';
519
+ value: string;
520
+ } | {
521
+ surface: 'model';
522
+ type: 'dismiss';
523
+ };
524
+ /**
525
+ * The chat's controls a host may draw itself instead (a native app's nav bar,
526
+ * history drawer, attach sheet and model chooser). For each one the host owns, the chat hides its own, publishes the
527
+ * control's state through `handle.surfaces` and takes the user's actions back as
528
+ * intents. Absent or empty: the chat draws everything, as before.
529
+ */
530
+ export type SurfaceName = (typeof SURFACE_NAMES)[number];
531
+ export declare const SURFACE_NAMES: readonly ["header", "picker", "history", "model", "canvas"];
532
+ /** The conversation header: its title and what its controls may do now. */
533
+ export interface HeaderState {
534
+ /** The backend conversation; absent while it is unsent. */
535
+ sessionId?: string;
536
+ /** Absent: nothing to name yet (a new chat). */
537
+ title?: string;
538
+ /** A conversation is opening: its transcript is not shown yet. */
539
+ switching: boolean;
540
+ /** An answer is being produced. */
541
+ answering: boolean;
542
+ canStartNewChat: boolean;
543
+ canRename: boolean;
544
+ canDelete: boolean;
545
+ /**
546
+ * The conversation's logs can be fetched: Session logs (`openLogs`) and Report issue
547
+ * (`reportIssue`) are offered, as the chat's own title menu offers them. Absent: neither
548
+ * (and from a chat before it).
549
+ */
550
+ canOpenLogs?: boolean;
551
+ /**
552
+ * The canvas covers the whole chat, the chat's own topbar with it (full screen, as a small
553
+ * screen always opens it): a host bar gives way too. Absent: nothing covers it (and from a
554
+ * chat before it).
555
+ */
556
+ covered?: boolean;
557
+ /**
558
+ * The covering canvas is a sheet over the chat dimmed by a scrim (a small screen): what the
559
+ * host draws above the page (the status bar's strip) dims with it. Absent: no scrim (and from
560
+ * a chat before it).
561
+ */
562
+ dimmed?: boolean;
563
+ }
564
+ /**
565
+ * What the user did in a host-drawn header. `rename`, `delete`, `openLogs` and `reportIssue`
566
+ * name the conversation (`HeaderState.sessionId`) they were asked for: one for another
567
+ * conversation is dropped. `delete` is already confirmed by the host.
568
+ */
569
+ export type HeaderIntent = {
570
+ surface: 'header';
571
+ type: 'newChat';
572
+ } | {
573
+ surface: 'header';
574
+ type: 'openHistory';
575
+ } | {
576
+ surface: 'header';
577
+ type: 'delete';
578
+ sessionId: string;
579
+ } | {
580
+ surface: 'header';
581
+ type: 'rename';
582
+ sessionId: string;
583
+ title: string;
584
+ } | {
585
+ surface: 'header';
586
+ type: 'openLogs';
587
+ sessionId: string;
588
+ } | {
589
+ surface: 'header';
590
+ type: 'reportIssue';
591
+ sessionId: string;
592
+ };
593
+ /** What a canvas control does, so a host can pick its own symbol for it. */
594
+ export type CanvasActionIcon = 'download' | 'share' | 'save' | 'open' | 'deploy' | 'table' | 'chart';
595
+ /** One of the canvas header's controls, as the chat's own header offers it now. */
596
+ export interface CanvasAction {
597
+ /** Sent back in `{ type: 'action', id }`. */
598
+ id: string;
599
+ label: string;
600
+ icon: CanvasActionIcon;
601
+ /** Shown but not usable yet (the body still loading, a save under way). */
602
+ disabled?: boolean;
603
+ }
604
+ /**
605
+ * The canvas header, for a host that draws it (`canvas`, D-200): present while the canvas covers
606
+ * the chat on a screen it fills (a phone, the app), absent otherwise. The chat then hides its own
607
+ * canvas header and leaves the host's bar in place (`HeaderState.covered` stays unset): the host
608
+ * shows this title, Close and these actions in that bar instead of the conversation's.
609
+ */
610
+ export interface CanvasState {
611
+ /**
612
+ * A new number each time a canvas opens or shows another artifact (one count for the page):
613
+ * every intent sends back the one it was drawn from, so a tap that arrives after the canvas
614
+ * changed acts on nothing.
615
+ */
616
+ canvasId: number;
617
+ title: string;
618
+ /**
619
+ * A tag beside the title, as the chat's own header shows it: `label` (e.g. "Snapshot", a saved
620
+ * dashboard or report pictured as it was when the answer was made) and `hint`, what it means.
621
+ * Absent: none.
622
+ */
623
+ badge?: {
624
+ label: string;
625
+ hint: string;
626
+ };
627
+ /** In the chat header's order. */
628
+ actions: CanvasAction[];
629
+ }
630
+ /**
631
+ * What the user did in a host-drawn canvas header, for the canvas `canvasId` names. One for
632
+ * another canvas, or an action not published now (or disabled), is dropped. A `close` without
633
+ * `canvasId` (an app from before it) closes the canvas shown, so no app is ever left unable to
634
+ * leave it.
635
+ */
636
+ export type CanvasIntent = {
637
+ surface: 'canvas';
638
+ canvasId?: number;
639
+ type: 'close';
640
+ } | {
641
+ surface: 'canvas';
642
+ canvasId: number;
643
+ type: 'action';
644
+ id: string;
645
+ };
646
+ export type SurfaceIntent = PickerIntent | HeaderIntent | HistoryIntent | ModelIntent | CanvasIntent;
647
+ /** The state of each surface the host owns. */
648
+ export interface SurfaceState {
649
+ header?: HeaderState;
650
+ picker?: PickerState;
651
+ history?: HistoryState;
652
+ model?: ModelState;
653
+ canvas?: CanvasState;
654
+ }
655
+ /** The surfaces a host draws, on a mount's handle. */
656
+ export interface ChatSurfaces {
657
+ get(): SurfaceState;
658
+ /** Call `onChange` whenever `get` may return something different. */
659
+ subscribe(onChange: () => void): () => void;
660
+ /** An intent for a surface the host does not own, or a stale one, is dropped. */
661
+ send(intent: SurfaceIntent): void;
662
+ }
663
+ export type OpenIntent = {
664
+ type: 'open-dashboard';
665
+ dashboardId: string;
666
+ tabId?: string;
667
+ } | {
668
+ type: 'open-insight';
669
+ insightId: string;
670
+ dashboardId?: string;
671
+ tabId?: string;
672
+ } | {
673
+ type: 'open-external';
674
+ url: string;
675
+ } | {
676
+ type: 'open-host-artifact';
677
+ artifact: ChatArtifact;
678
+ target: 'view' | 'edit';
679
+ };
680
+ /** `can` decides, synchronously, whether the control shows; `run` handles the click. */
681
+ export interface HostAction<T, R = void> {
682
+ can(target: T): boolean;
683
+ run(target: T): R | Promise<R>;
684
+ }
685
+ /**
686
+ * What `actions.save` is handed: the artifact. A report (`html`, `report`)
687
+ * also carries where Copilot stores it and Copilot's own task and artifact
688
+ * ids: the chat asks about a report only once it can place it in the stored
689
+ * conversation. Every other kind is saved from `data` and never carries them.
690
+ */
691
+ export interface SaveTarget extends ChatArtifact {
692
+ /**
693
+ * The report's file in Copilot's artifact repository (`artifact://{tenant}/{id}`):
694
+ * what a host saves the report by and later opens it by. Absent when Copilot
695
+ * did not name one; a host that needs it declines the report then.
696
+ */
697
+ artifactUri?: string;
698
+ /** Copilot's task and artifact ids for the message part: email share and lineage. */
699
+ taskId?: string;
700
+ artifactId?: string;
701
+ }
702
+ /** An artifact to share, with Copilot's own task and artifact ids (what `POST /copilot/email` takes). */
703
+ export interface ShareTarget {
704
+ artifact: ChatArtifact;
705
+ taskId: string;
706
+ artifactId: string;
707
+ }
708
+ export interface CodeSnippet {
709
+ code: string;
710
+ language?: string;
711
+ }
712
+ export interface ChatActions {
713
+ /** Absent: source links and Open/Edit buttons are hidden. */
714
+ open?: HostAction<OpenIntent>;
715
+ /**
716
+ * Save a generated draft or a report in the host's catalog. `run` resolves
717
+ * to the fields the save changed; the chat records them on the stored part
718
+ * so the conversation replays the artifact as saved: in `data` when the
719
+ * payload is an object, in `metadata` when it is not (a report's HTML), which
720
+ * is where `can` then reads them. In `metadata` a field is only ever added:
721
+ * one the part already carries is kept. `{}` means nothing was saved (the
722
+ * user dismissed the host's dialog): nothing is recorded and Save stays.
723
+ * Absent: no Save.
724
+ */
725
+ save?: HostAction<SaveTarget, Record<string, unknown>>;
726
+ /**
727
+ * Share an artifact through the host's own dialog; `run` resolves `true`
728
+ * once shared. Shown in the canvas and the card menu wherever `can` accepts
729
+ * the target. Absent: no Share.
730
+ */
731
+ share?: HostAction<ShareTarget, boolean>;
732
+ /** Insert an answer's code block into the host (a notebook paragraph). Absent: no Insert button. */
733
+ insertCode?: HostAction<CodeSnippet>;
734
+ /**
735
+ * Keep a file the chat made for the user (Download, a session's logs): a native app shares or
736
+ * saves it. Absent, or `can` false: the browser downloads it.
737
+ */
738
+ download?: HostAction<FileDownload>;
739
+ /**
740
+ * Show the host's own account page (a native app's): the history's account footer becomes a
741
+ * button that opens it. Absent, or `can` false: the footer only shows who is signed in.
742
+ */
743
+ openAccount?: HostAction<void>;
744
+ }
745
+ /** A file the chat made for the user to keep. */
746
+ export interface FileDownload {
747
+ name: string;
748
+ blob: Blob;
749
+ }
750
+ export interface ChatUserActivity {
751
+ type: 'prompt' | 'human-input' | 'cancel' | 'upload' | 'history-mutation';
752
+ occurredAt: number;
753
+ }
754
+ /**
755
+ * Where a contained failure happened. `scope` is a stable slug the remote
756
+ * owns (`task`, `upload`, `poll-budget`, `render-region/thread`,
757
+ * `host-renderer`, …); the ids are present when the failure belongs to one
758
+ * conversation or task.
759
+ */
760
+ export interface ChatErrorContext {
761
+ scope: string;
762
+ sessionId?: string;
763
+ taskId?: string;
764
+ remoteTaskId?: string;
765
+ }
766
+ export interface ChatEvents {
767
+ /** A contained failure; the user already sees it inline. Absent: logged to the console. */
768
+ onError?(error: ChatError, context: ChatErrorContext): void;
769
+ /** The mount cannot continue; the host should tear it down and show its fallback. */
770
+ onFatal?(error: ChatError): void;
771
+ onActivity?(activity: ChatUserActivity): void;
772
+ /**
773
+ * Dictation was refused the microphone (the browser's or the system's permission): a host
774
+ * that can fix it says how, or asks for it again (a page of its own, the system settings).
775
+ */
776
+ onMicrophoneBlocked?(): void;
777
+ /**
778
+ * An answer is being produced: keep the user's host session alive until the
779
+ * returned release runs (the answer settled, or the mount ended). A long
780
+ * answer the user only watches must not sign them out. Absent: nothing held.
781
+ */
782
+ keepSessionAlive?(): () => void;
783
+ /**
784
+ * The chat entered or left fullscreen; the host hides or restores its own
785
+ * chrome. Always called with `false` before the mount's cleanup completes.
786
+ */
787
+ onFullscreen?(active: boolean): void;
788
+ }
789
+ /**
790
+ * Everything a host passes to one chat mount. Only plain data and
791
+ * functions cross this boundary — no React node, store, router or provider —
792
+ * which is what lets the chat run its own React. `connection` and
793
+ * `environment` are required; everything else is optional, and the chat hides
794
+ * what it drives when absent.
795
+ */
796
+ export interface ChatOptions {
797
+ version: ContractVersion;
798
+ connection: ChatConnection;
799
+ environment: ChatEnvironment;
800
+ chat?: ChatSetup;
801
+ actions?: ChatActions;
802
+ events?: ChatEvents;
803
+ /**
804
+ * Stable identity for this mount location, keying route-return recovery so
805
+ * two simultaneous mounts never share a checkpoint. Hosts that render one
806
+ * chat at a time omit it (docs/architecture/adr/checkpoint-lifecycle.md).
807
+ */
808
+ instanceId?: string;
809
+ /**
810
+ * The host sizes the mount and owns its window (a page-covering overlay, a
811
+ * docked side panel) and draws its frame. Present: the chat hides its own
812
+ * fullscreen toggle and its edge border, and shows a dock toggle while
813
+ * `canDock` (or, with `mode`, `modes` and `setMode`, one button per other
814
+ * mode), and a close button while it has `close`. Absent: the chat's own
815
+ * fullscreen toggle, and no close button.
816
+ */
817
+ window?: ChatWindow;
818
+ }
819
+ /** The windows a host can show the chat in. */
820
+ export type ChatWindowMode = 'floating' | 'docked' | 'fullscreen';
821
+ export interface ChatWindowState {
822
+ /** The mount is the docked side panel (the toggle shows pressed). */
823
+ docked: boolean;
824
+ /** The host page allows docking now; `false` hides the dock toggle. */
825
+ canDock: boolean;
826
+ /**
827
+ * The window shown now. With `modes` and `setMode`, the header offers every
828
+ * mode in `modes` but this one (float, dock, full screen, in that order)
829
+ * instead of the dock toggle.
830
+ */
831
+ mode?: ChatWindowMode;
832
+ /** The modes the host can switch to now, the current one included. */
833
+ modes?: readonly ChatWindowMode[];
834
+ /**
835
+ * The window is out of sight but the chat stays mounted (minimized, so the
836
+ * conversation stays). Anything that should not go on unseen stops: a
837
+ * dictation in progress ends and releases the microphone. Omitted means shown.
838
+ */
839
+ hidden?: boolean;
840
+ }
841
+ export interface ChatWindow {
842
+ /** May return a fresh object on every call: the chat compares the fields. */
843
+ get(): ChatWindowState;
844
+ /** Call `onChange` whenever `get` may return something different. */
845
+ subscribe(onChange: () => void): () => void;
846
+ setDocked(docked: boolean): void;
847
+ /** Shows the chat in another of `modes`; the conversation and draft stay. */
848
+ setMode?(mode: ChatWindowMode): void;
849
+ /** Hides the chat. Absent: no close button (an app that owns its window, a desktop window or a side panel). */
850
+ close?(): void;
851
+ }
852
+ /** What `mount` returns: the host's handle on one running chat. */
853
+ export interface ChatHandle {
854
+ /** Tears the chat down and restores the element. Idempotent. */
855
+ unmount(): void;
856
+ /**
857
+ * Sends a question as if the user typed it and pressed Send (same gates: it
858
+ * is ignored while an answer is running). `prompt` replaces the text sent to
859
+ * Copilot, so a host can send a prepared request under a readable question.
860
+ */
861
+ ask(question: string, options?: {
862
+ prompt?: string;
863
+ }): void;
864
+ /**
865
+ * Attaches a host context to the composer as if the user picked it (same
866
+ * gates: the agent must support the kind; `false` when refused). Called
867
+ * before the chat has connected, the item is checked again by the agent's
868
+ * rules once it connects, after the deployment's default contexts.
869
+ *
870
+ * `replace: true` (a deep link that names the contexts) first removes every
871
+ * context the draft holds, files included, and keeps the deployment's
872
+ * defaults out; the removal stands even when the item is then refused. For
873
+ * several items, pass it on the first only. A chat older than `replace`
874
+ * ignores it and adds the item to the draft.
875
+ */
876
+ attach(kind: Exclude<ContextKind, 'file'>, item: ContextItem, options?: {
877
+ replace?: boolean;
878
+ }): boolean;
879
+ /**
880
+ * Opens a saved conversation by its id (a `chat.sessionLink` the host read
881
+ * back from its URL). A conversation with no messages, or one the backend
882
+ * refuses (deleted, someone else's), stays closed and the chat says so.
883
+ * While an unsent question is still being recovered nothing opens.
884
+ */
885
+ openSession(sessionId: string): void;
886
+ /**
887
+ * The state of the surfaces the host owns (`chat.surfaces`), and the user's actions on
888
+ * them. Absent on a chat older than surfaces.
889
+ */
890
+ surfaces?: ChatSurfaces;
891
+ }
892
+ /** The federated entry point `nexusChat/mount`. */
893
+ export type ChatMount = (element: HTMLElement, options: ChatOptions) => ChatHandle;
894
+ /**
895
+ * The fixed codes a `ChatError` carries today. The set is open: `onError` also
896
+ * reports codes made from its `context.scope` (`TASK`, `RENDER_REGION_THREAD`, …), so
897
+ * `ChatError.code` stays a `string`.
898
+ * - `INCOMPATIBLE_HOST_CONTRACT`: the host's `version` is outside the range the chat mounts.
899
+ * - `ALREADY_MOUNTED`: the element already holds a mounted chat.
900
+ * - `MOUNT_FAILED`: the chat could not start.
901
+ * - `RENDER_FAILED`: the chat failed while rendering, outside every contained region.
902
+ * - `LOAD_TIMEOUT`, `LOAD_FAILED`: `@incorta/chat` could not load the chat (the
903
+ * deadline passed, or its retry failed too).
904
+ */
905
+ export type ChatErrorCode = 'INCOMPATIBLE_HOST_CONTRACT' | 'ALREADY_MOUNTED' | 'MOUNT_FAILED' | 'RENDER_FAILED' | 'LOAD_TIMEOUT' | 'LOAD_FAILED';
906
+ export declare class ChatError extends Error {
907
+ readonly code: string;
908
+ constructor(code: string, message: string, options?: ErrorOptions);
909
+ }
910
+ /**
911
+ * A `ChatError`, with `code` when given, from this copy of the contract or any
912
+ * other (another bundle's class is a different class, so this reads `name` and `code`). An
913
+ * earlier chat's error counts too: a host that loads one still reads its
914
+ * `INCOMPATIBLE_HOST_CONTRACT`, and says an update is needed instead of offering a retry.
915
+ */
916
+ export declare function isChatError(error: unknown, code?: string): error is Error & {
917
+ code: string;
918
+ };
919
+ export declare function assertCompatibleHost(options: Pick<ChatOptions, 'version'>): asserts options is Pick<ChatOptions, 'version'> & {
920
+ version: ContractVersion;
921
+ };
922
+ export {};