@aikaara/chat-sdk 0.3.0 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -49,15 +49,21 @@ export declare class AikaaraChatClient extends EventEmitter<ChatEvents> {
49
49
  private config;
50
50
  private mode;
51
51
  private uploadAdapter;
52
+ private historyAdapter;
52
53
  private tiledeskUnsubs;
53
54
  constructor(config: ChatClientConfig, opts?: {
54
55
  uploadAdapter?: UploadAdapter;
56
+ historyAdapter?: ConversationHistoryAdapter;
55
57
  });
56
58
  private usesAikaara;
57
59
  private usesTiledesk;
58
60
  private initTiledeskTransport;
59
61
  connect(): Promise<void>;
60
- sendMessage(content: string): Promise<void>;
62
+ private hydrateTiledeskHistory;
63
+ sendMessage(content: string, opts?: {
64
+ attributes?: Record<string, unknown>;
65
+ metadata?: Record<string, unknown>;
66
+ }): Promise<void>;
61
67
  /**
62
68
  * Upload a file via the configured UploadAdapter (client-side: file goes
63
69
  * to the tenant's own backend, never through Aikaara). Then publish the
@@ -121,17 +127,38 @@ export declare class AikaaraChatHeader extends HTMLElement {
121
127
  setStatus(status: string): void;
122
128
  }
123
129
 
130
+ /**
131
+ * Chat input box. Composes a textarea + send button + optional attach button.
132
+ *
133
+ * Events fired (composed:true so they cross shadow boundaries):
134
+ * - `send` `{ content: string }` — user pressed enter / clicked send
135
+ * - `file-pick` `{ file: File }` — user selected a file to upload
136
+ *
137
+ * Attributes:
138
+ * - `placeholder` placeholder text
139
+ * - `disable-attach` hides the attach button (set when host has no uploadAdapter)
140
+ * - `accept` forwarded to the hidden <input type="file">
141
+ * - `multiple` allow selecting multiple files
142
+ */
124
143
  export declare class AikaaraChatInput extends HTMLElement {
125
144
  private shadow;
126
145
  private textarea;
127
146
  private sendBtn;
147
+ private attachBtn;
148
+ private fileInput;
128
149
  private _disabled;
150
+ private _uploading;
129
151
  constructor();
152
+ static get observedAttributes(): string[];
130
153
  connectedCallback(): void;
131
154
  set disabled(val: boolean);
132
155
  get disabled(): boolean;
156
+ /** Toggle the attach button into a "uploading…" spinner state. */
157
+ set uploading(val: boolean);
158
+ get uploading(): boolean;
133
159
  focus(): void;
134
160
  clear(): void;
161
+ private refreshSendDisabled;
135
162
  private handleSend;
136
163
  private autoGrow;
137
164
  }
@@ -148,6 +175,7 @@ export declare class AikaaraChatWidget extends HTMLElement {
148
175
  configure(config: Partial<WidgetConfig>): void;
149
176
  private getConfig;
150
177
  setUploadAdapter(adapter: UploadAdapter): void;
178
+ setHistoryAdapter(adapter: ConversationHistoryAdapter): void;
151
179
  private render;
152
180
  private initController;
153
181
  sendUserEvent(eventKey: string, value?: Record<string, unknown>, source?: string): void;
@@ -300,6 +328,13 @@ export declare interface ChatClientConfig extends ConnectionConfig {
300
328
  departmentId?: string;
301
329
  senderFullname?: string;
302
330
  };
331
+ /**
332
+ * Tenant-level defaults merged into the `action` object of every template
333
+ * postback (e.g. submit-action click). Useful for app-specific keys like
334
+ * `serviceType: 'ITR'` that the bot expects on every form submission.
335
+ * Per-submit values from the form take precedence over these defaults.
336
+ */
337
+ templateActionAttributes?: Record<string, unknown>;
303
338
  onMessage?: (message: Message) => void;
304
339
  onStatusChange?: (status: string) => void;
305
340
  onError?: (error: Error) => void;
@@ -346,6 +381,8 @@ export declare interface ChatEvents {
346
381
  };
347
382
  }
348
383
 
384
+ export declare function clearPersistedConversationId(userId: string, projectId: string): void;
385
+
349
386
  export declare interface ConnectionConfig {
350
387
  baseUrl: string;
351
388
  wsUrl?: string;
@@ -377,6 +414,19 @@ export declare class ConnectionManager extends EventEmitter<ChatEvents> {
377
414
 
378
415
  export declare type ConnectionState = 'disconnected' | 'connecting' | 'connected' | 'reconnecting';
379
416
 
417
+ /**
418
+ * Pluggable adapter that hydrates prior conversation messages on connect.
419
+ * The SDK calls this once after MQTT subscribes so the user sees existing
420
+ * history before any new live messages arrive.
421
+ */
422
+ export declare interface ConversationHistoryAdapter {
423
+ fetchMessages(conversationId: string, ctx: {
424
+ userId: string;
425
+ appId?: string;
426
+ projectId?: string;
427
+ }): Promise<TiledeskMessage[]>;
428
+ }
429
+
380
430
  export declare class ConversationManager {
381
431
  private _conversationId;
382
432
  private persist;
@@ -395,6 +445,8 @@ declare interface CreateConversationResponse {
395
445
 
396
446
  export declare function createFetchUploadAdapter(config: FetchUploadAdapterConfig): UploadAdapter;
397
447
 
448
+ export declare function createTiledeskHistoryAdapter(config: TiledeskHistoryAdapterConfig): ConversationHistoryAdapter;
449
+
398
450
  export declare interface EditEntityAction {
399
451
  action: 'edit_entity';
400
452
  entity_type: string;
@@ -616,6 +668,20 @@ export declare class MessageStore {
616
668
 
617
669
  export declare function mount(config: WidgetConfig): AikaaraChatWidget;
618
670
 
671
+ export declare interface MountedTenantWidget {
672
+ widget: AikaaraChatWidget;
673
+ requestId: string;
674
+ config: WidgetConfig;
675
+ destroy: () => void;
676
+ }
677
+
678
+ /**
679
+ * Standard tenant mount. Fetches the descriptor (when `configUrl` is set),
680
+ * merges per-mount overrides, builds default adapters from the descriptor's
681
+ * endpoint URLs (when no override is supplied), and mounts the widget.
682
+ */
683
+ export declare function mountTenantWidget(opts: TenantMountOptions): Promise<MountedTenantWidget>;
684
+
619
685
  export declare interface NavigateAction {
620
686
  navigate_to: string;
621
687
  }
@@ -641,6 +707,43 @@ declare interface TemplateMessageEvent {
641
707
  raw: unknown;
642
708
  }
643
709
 
710
+ export declare interface TenantMountOptions {
711
+ container: HTMLElement;
712
+ /** Fetched from this URL when `config` is omitted. */
713
+ configUrl?: string;
714
+ /** Pre-resolved descriptor — skip the fetch. */
715
+ config?: WidgetConfigDescriptor;
716
+ /** Headers attached when fetching `configUrl` (e.g. tenant-API auth). */
717
+ configHeaders?: Record<string, string>;
718
+ identity: {
719
+ userId: string;
720
+ userName?: string;
721
+ departmentId?: string;
722
+ senderFullname?: string;
723
+ };
724
+ /** Returns a raw Tiledesk JWT (no Bearer/JWT prefix) for MQTT password. */
725
+ tokenProvider: () => Promise<string>;
726
+ /** Authorization value for the history fetch. Defaults to `tokenProvider()`. */
727
+ historyTokenProvider?: () => Promise<string>;
728
+ /**
729
+ * Optional override adapters. If omitted and the descriptor specifies the
730
+ * matching endpoint, the SDK builds default fetch-based adapters.
731
+ */
732
+ uploadAdapter?: UploadAdapter;
733
+ historyAdapter?: ConversationHistoryAdapter;
734
+ /**
735
+ * Pin a conversationId. When omitted we look up
736
+ * `aikaara_chat:requestId:{userId}:{projectId}` in localStorage and reuse
737
+ * any persisted value; only mint a fresh `requestId` when nothing is stored.
738
+ */
739
+ conversationId?: string;
740
+ forceNewConversation?: boolean;
741
+ /** Per-mount overrides that take precedence over the descriptor. */
742
+ overrides?: Partial<WidgetConfigDescriptor>;
743
+ /** Surfaced from the SDK. */
744
+ onError?: (err: Error) => void;
745
+ }
746
+
644
747
  export declare interface TestToolAction {
645
748
  action: 'test_tool';
646
749
  tool_id: number;
@@ -674,6 +777,32 @@ export declare interface TiledeskFileTemplateConfig {
674
777
  isDeepLink?: boolean;
675
778
  }
676
779
 
780
+ export declare interface TiledeskHistoryAdapterConfig {
781
+ /**
782
+ * Base URL of the Tiledesk chat API, e.g. `https://uat-tiledesk.taxbuddy.com/chatapi/api`
783
+ * (no trailing slash). Final URL is composed as
784
+ * `{apiBase}/tilechat/{userId}/conversations/{conversationId}/messages?pageSize={pageSize}`
785
+ */
786
+ apiBase: string;
787
+ pageSize?: number;
788
+ /**
789
+ * Async getter for the bearer token. Most Tiledesk-fronted gateways accept
790
+ * the same JWT used for the MQTT connection. Return `null` to omit the
791
+ * `Authorization` header.
792
+ */
793
+ getToken?: () => string | null | Promise<string | null>;
794
+ /** Override the path template. Default mirrors bandhan's chatapi route. */
795
+ pathTemplate?: string;
796
+ /** Extra static headers to attach (e.g. `partnerid`, `environment`). */
797
+ extraHeaders?: Record<string, string>;
798
+ /**
799
+ * Optional response shape mapper. Default expects
800
+ * `{ success: true, result: TiledeskMessage[] }`
801
+ * which matches the bandhan `/chatapi/api/tilechat/.../messages` response.
802
+ */
803
+ parseResponse?: (raw: unknown) => TiledeskMessage[];
804
+ }
805
+
677
806
  export declare interface TiledeskMessage {
678
807
  text?: string;
679
808
  type?: string;
@@ -760,6 +889,7 @@ export declare class TiledeskTransport {
760
889
  private inboundUpdateRegex;
761
890
  constructor(config: TiledeskTransportConfig);
762
891
  connect(): Promise<void>;
892
+ private debugLog;
763
893
  subscribeWildcard(): void;
764
894
  subscribeToConversation(conversationId: string): void;
765
895
  unsubscribeFromConversation(conversationId: string): void;
@@ -821,6 +951,20 @@ export declare interface TiledeskTransportConfig {
821
951
  fileTemplate?: TiledeskFileTemplateConfig;
822
952
  recipientFullnameResolver?: (conversationId: string) => string | undefined;
823
953
  senderFullname?: string;
954
+ /**
955
+ * If true (default), AikaaraChatClient publishes a `CHAT_INITIATED` event
956
+ * to the conversation's outbound topic on first connect — but only when
957
+ * the conversation history is empty (so reload of an in-flight chat
958
+ * doesn't replay the kickoff).
959
+ */
960
+ autoInitiateOnEmpty?: boolean;
961
+ /** Extra attributes merged into the auto-fired CHAT_INITIATED envelope. */
962
+ chatInitiatedAttributes?: Record<string, unknown>;
963
+ /**
964
+ * Log decoded MQTT frames to console as they're sent/received.
965
+ * Off by default — set true for debugging Tiledesk wire traffic.
966
+ */
967
+ debug?: boolean;
824
968
  }
825
969
 
826
970
  export declare interface ToolCall {
@@ -891,6 +1035,70 @@ export declare interface WidgetConfig extends ChatClientConfig {
891
1035
  bubbleText?: string;
892
1036
  bubbleIcon?: string;
893
1037
  uploadAdapter?: UploadAdapter;
1038
+ historyAdapter?: ConversationHistoryAdapter;
1039
+ /**
1040
+ * Display mode:
1041
+ * - `popup` (default): floating bubble in the corner, click to open panel.
1042
+ * - `embed`: render the chat panel inline at the host element's location,
1043
+ * filling its container. Bubble + open/close animation are skipped.
1044
+ */
1045
+ display?: 'popup' | 'embed';
1046
+ }
1047
+
1048
+ export declare interface WidgetConfigDescriptor {
1049
+ /** Visuals */
1050
+ display?: 'popup' | 'embed';
1051
+ position?: 'bottom-right' | 'bottom-left';
1052
+ primaryColor?: string;
1053
+ title?: string;
1054
+ subtitle?: string;
1055
+ avatarUrl?: string;
1056
+ width?: number;
1057
+ height?: number;
1058
+ borderRadius?: number;
1059
+ fontFamily?: string;
1060
+ welcomeMessage?: string;
1061
+ placeholder?: string;
1062
+ showTimestamps?: boolean;
1063
+ persistConversation?: boolean;
1064
+ /** Transport selection */
1065
+ transport?: TransportMode;
1066
+ /** Tiledesk wire config (JSON-serializable subset of TiledeskTransportConfig) */
1067
+ tiledesk?: {
1068
+ mqttEndpoint: string;
1069
+ appId?: string;
1070
+ projectId: string;
1071
+ mqttUsername?: string;
1072
+ protocolId?: 'MQIsdp' | 'MQTT';
1073
+ protocolVersion?: 3 | 4 | 5;
1074
+ keepAliveSec?: number;
1075
+ connectTimeoutMs?: number;
1076
+ maxReconnectAttempts?: number;
1077
+ reconnectMaxDelayMs?: number;
1078
+ wildcardSubscribe?: boolean;
1079
+ enablePresence?: boolean;
1080
+ autoInitiateOnEmpty?: boolean;
1081
+ chatInitiatedAttributes?: Record<string, unknown>;
1082
+ messageDefaults?: TiledeskMessageDefaults;
1083
+ fileTemplate?: TiledeskFileTemplateConfig;
1084
+ topicTemplates?: TiledeskTopicTemplates;
1085
+ debug?: boolean;
1086
+ /**
1087
+ * Pattern for `support-group-…` requestId minted on first chat. Use
1088
+ * `{projectId}` and `{uuid}` placeholders. Defaults to bandhan's
1089
+ * `support-group-{projectId}-{uuid}`.
1090
+ */
1091
+ requestIdTemplate?: string;
1092
+ };
1093
+ /** Defaults merged into every form-action postback */
1094
+ templateActionAttributes?: Record<string, unknown>;
1095
+ /** Adapter endpoints — SDK builds the actual fetch calls */
1096
+ uploadEndpoint?: string;
1097
+ uploadFieldName?: string;
1098
+ uploadExtraFields?: Record<string, string>;
1099
+ historyApiBase?: string;
1100
+ historyPageSize?: number;
1101
+ historyPathTemplate?: string;
894
1102
  }
895
1103
 
896
1104
  export { }
package/dist/index.mjs CHANGED
@@ -1,9 +1,8 @@
1
- import { A as m, a as u, b as h, C as k, c as g, d as C, E as A, M as b, T as f, e as T, i as M, f as v, p as w } from "./AikaaraChatClient-Cqbcd1jb.mjs";
2
- import { FormBridge as E, createFetchUploadAdapter as S } from "./headless.mjs";
3
- import { registerComponents as s } from "./ui.mjs";
4
- import { AikaaraChatBubble as B, AikaaraChatHeader as F, AikaaraChatInput as U, AikaaraChatWidget as I, AikaaraErrorBanner as j, AikaaraMessageBubble as q, AikaaraMessageList as H, AikaaraStreamingMessage as K, AikaaraTypingIndicator as L } from "./ui.mjs";
1
+ import { r as o } from "./ui-BMGa0gZH.mjs";
2
+ import { A as m, a as u, b as k, c as g, d as h, e as C, f as A, g as b, h as f, i as T, j as v, k as y, C as M, l as w, m as E, E as S, M as x, T as B, n as F, o as I, p as U, q as j } from "./ui-BMGa0gZH.mjs";
3
+ import { FormBridge as H, clearPersistedConversationId as W, createFetchUploadAdapter as K, createTiledeskHistoryAdapter as L, mountTenantWidget as O } from "./headless.mjs";
5
4
  function l(e) {
6
- s();
5
+ o();
7
6
  const a = document.createElement("aikaara-chat-widget"), r = {
8
7
  baseUrl: "base-url",
9
8
  userToken: "user-token",
@@ -19,42 +18,45 @@ function l(e) {
19
18
  welcomeMessage: "welcome-message",
20
19
  avatarUrl: "avatar-url"
21
20
  };
22
- for (const [i, o] of Object.entries(r)) {
21
+ for (const [i, s] of Object.entries(r)) {
23
22
  const t = e[i];
24
- t != null && a.setAttribute(o, String(t));
23
+ t != null && a.setAttribute(s, String(t));
25
24
  }
26
25
  return a.configure(e), document.body.appendChild(a), a;
27
26
  }
28
- function c() {
27
+ function d() {
29
28
  const e = document.querySelector("aikaara-chat-widget");
30
29
  e && e.remove();
31
30
  }
32
31
  export {
33
32
  m as ActionCableClient,
34
- B as AikaaraChatBubble,
35
- u as AikaaraChatClient,
36
- F as AikaaraChatHeader,
37
- U as AikaaraChatInput,
38
- I as AikaaraChatWidget,
39
- j as AikaaraErrorBanner,
40
- q as AikaaraMessageBubble,
41
- H as AikaaraMessageList,
42
- K as AikaaraStreamingMessage,
43
- L as AikaaraTypingIndicator,
44
- h as ApiClient,
45
- k as ChannelSubscription,
46
- g as ConnectionManager,
47
- C as ConversationManager,
48
- A as EventEmitter,
49
- E as FormBridge,
50
- b as MessageStore,
51
- f as TiledeskTransport,
52
- S as createFetchUploadAdapter,
53
- T as extractTiledeskFileEnvelope,
54
- M as inferTiledeskRole,
55
- v as isTiledeskSelfEcho,
33
+ u as AikaaraChatBubble,
34
+ k as AikaaraChatClient,
35
+ g as AikaaraChatHeader,
36
+ h as AikaaraChatInput,
37
+ C as AikaaraChatWidget,
38
+ A as AikaaraErrorBanner,
39
+ b as AikaaraMessageBubble,
40
+ f as AikaaraMessageList,
41
+ T as AikaaraStreamingMessage,
42
+ v as AikaaraTypingIndicator,
43
+ y as ApiClient,
44
+ M as ChannelSubscription,
45
+ w as ConnectionManager,
46
+ E as ConversationManager,
47
+ S as EventEmitter,
48
+ H as FormBridge,
49
+ x as MessageStore,
50
+ B as TiledeskTransport,
51
+ W as clearPersistedConversationId,
52
+ K as createFetchUploadAdapter,
53
+ L as createTiledeskHistoryAdapter,
54
+ F as extractTiledeskFileEnvelope,
55
+ I as inferTiledeskRole,
56
+ U as isTiledeskSelfEcho,
56
57
  l as mount,
57
- w as parseTiledeskTemplate,
58
- s as registerComponents,
59
- c as unmount
58
+ O as mountTenantWidget,
59
+ j as parseTiledeskTemplate,
60
+ o as registerComponents,
61
+ d as unmount
60
62
  };