@aikaara/chat-sdk 0.1.4 → 0.3.0

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/ui.d.ts CHANGED
@@ -6,6 +6,77 @@ export declare class AikaaraChatBubble extends HTMLElement {
6
6
  setIcon(svgOrText: string): void;
7
7
  }
8
8
 
9
+ declare class AikaaraChatClient extends EventEmitter<ChatEvents> {
10
+ private connection;
11
+ private tiledesk;
12
+ private api;
13
+ private messageStore;
14
+ private conversationManager;
15
+ private subscription;
16
+ private config;
17
+ private mode;
18
+ private uploadAdapter;
19
+ private tiledeskUnsubs;
20
+ constructor(config: ChatClientConfig_2, opts?: {
21
+ uploadAdapter?: UploadAdapter_2;
22
+ });
23
+ private usesAikaara;
24
+ private usesTiledesk;
25
+ private initTiledeskTransport;
26
+ connect(): Promise<void>;
27
+ sendMessage(content: string): Promise<void>;
28
+ /**
29
+ * Upload a file via the configured UploadAdapter (client-side: file goes
30
+ * to the tenant's own backend, never through Aikaara). Then publish the
31
+ * Tiledesk file-message envelope so hooks_controller picks it up.
32
+ */
33
+ sendFile(file: File | Blob, opts?: {
34
+ caption?: string;
35
+ }): Promise<void>;
36
+ /**
37
+ * Trigger Tiledesk's CHAT_INITIATED event — required to kick off a bot flow
38
+ * for newly-created Tiledesk request groups.
39
+ */
40
+ initiateTiledeskChat(extraAttributes?: Record<string, unknown>): void;
41
+ /** Mark a Tiledesk message as read (publishes status=300 update). */
42
+ markTiledeskRead(messageId: string): void;
43
+ setUploadAdapter(adapter: UploadAdapter_2): void;
44
+ sendUserEvent(eventKey: string, value?: object, source?: string): Promise<void>;
45
+ loadHistory(): Promise<Message_2[]>;
46
+ get messages(): Message_2[];
47
+ get conversationId(): string | null;
48
+ get isConnected(): boolean;
49
+ /**
50
+ * Update the agent's context with information about the host app's current state.
51
+ * Call this on route changes so the agent knows what page/entity the user is viewing.
52
+ *
53
+ * The context is stored in conversation metadata and interpolated into the system prompt.
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * // On route change
58
+ * client.setContext({
59
+ * currentPage: '/products/42',
60
+ * entityType: 'product',
61
+ * entityId: '42',
62
+ * availableRoutes: { products: '/products', orders: '/orders' },
63
+ * });
64
+ * ```
65
+ */
66
+ setContext(context: AppContext): Promise<void>;
67
+ disconnect(): Promise<void>;
68
+ private handleTiledeskMessage;
69
+ private handleTiledeskStatusUpdate;
70
+ /**
71
+ * Parse structured action results from tool execution output.
72
+ * When the agent calls tools like `edit_current_entity`, `save_current_entity`,
73
+ * `navigate_to`, or `test_tool_by_id`, the result contains an action payload
74
+ * that the SDK emits as a typed event for the host app to handle.
75
+ */
76
+ private parseActionResult;
77
+ private handleBroadcast;
78
+ }
79
+
9
80
  export declare class AikaaraChatHeader extends HTMLElement {
10
81
  private shadow;
11
82
  static get observedAttributes(): string[];
@@ -43,8 +114,11 @@ export declare class AikaaraChatWidget extends HTMLElement {
43
114
  attributeChangedCallback(_name: string, oldVal: string, newVal: string): void;
44
115
  configure(config: Partial<WidgetConfig>): void;
45
116
  private getConfig;
117
+ setUploadAdapter(adapter: UploadAdapter): void;
46
118
  private render;
47
119
  private initController;
120
+ sendUserEvent(eventKey: string, value?: Record<string, unknown>, source?: string): void;
121
+ getClient(): AikaaraChatClient | null;
48
122
  private darkenColor;
49
123
  }
50
124
 
@@ -60,11 +134,19 @@ export declare class AikaaraErrorBanner extends HTMLElement {
60
134
 
61
135
  export declare class AikaaraMessageBubble extends HTMLElement {
62
136
  private shadow;
137
+ private templatePayload;
138
+ private attachments;
63
139
  static get observedAttributes(): string[];
64
140
  constructor();
65
141
  connectedCallback(): void;
66
142
  attributeChangedCallback(): void;
143
+ setTemplatePayload(payload: unknown): void;
144
+ setAttachments(attachments: Array<{
145
+ fileName: string;
146
+ fileUrl: string;
147
+ }>): void;
67
148
  private render;
149
+ private renderAttachments;
68
150
  }
69
151
 
70
152
  export declare class AikaaraMessageList extends HTMLElement {
@@ -83,6 +165,13 @@ export declare class AikaaraMessageList extends HTMLElement {
83
165
  showTypingIndicator(): void;
84
166
  removeTypingIndicator(): void;
85
167
  private appendMessageElement;
168
+ /**
169
+ * Replace an existing rendered message (matched by id or externalId) in place.
170
+ * Used when a Tiledesk self-echo reconciles with an optimistic bubble or when
171
+ * a status update flips delivered → read.
172
+ */
173
+ upsertMessage(message: Message): void;
174
+ private findRenderedMessage;
86
175
  private scrollToBottom;
87
176
  private formatTime;
88
177
  }
@@ -96,6 +185,27 @@ export declare class AikaaraStreamingMessage extends HTMLElement {
96
185
  finalize(): void;
97
186
  }
98
187
 
188
+ /**
189
+ * Renders Tiledesk contentType=300 template payloads.
190
+ *
191
+ * Slot-based override system — tenants ship a cloud-hosted bundle that
192
+ * registers `<aikaara-template-{templateId}>` custom elements; this component
193
+ * looks one up by `template-id` attribute and mounts it as a child. If no
194
+ * tenant override is registered, a no-op fallback renders the inner message
195
+ * (TaxBuddy-style nested envelope) so the chat at least shows readable text.
196
+ */
197
+ export declare class AikaaraTemplateRenderer extends HTMLElement {
198
+ private shadow;
199
+ private payloadData;
200
+ private innerMessage;
201
+ static get observedAttributes(): string[];
202
+ constructor();
203
+ connectedCallback(): void;
204
+ attributeChangedCallback(): void;
205
+ setPayload(payload: unknown, innerMessage?: string): void;
206
+ private render;
207
+ }
208
+
99
209
  export declare class AikaaraTypingIndicator extends HTMLElement {
100
210
  private shadow;
101
211
  constructor();
@@ -104,23 +214,140 @@ export declare class AikaaraTypingIndicator extends HTMLElement {
104
214
  hide(): void;
105
215
  }
106
216
 
217
+ declare interface AppContext {
218
+ /** Current page/route path in the host app (e.g., '/products/42') */
219
+ currentPage: string;
220
+ /** Entity type on the current page (e.g., 'product', 'agent') */
221
+ entityType?: string;
222
+ /** Entity ID on the current page */
223
+ entityId?: string | number;
224
+ /** Project/workspace ID if applicable */
225
+ projectId?: string | number;
226
+ /** Routes the agent can navigate to — map of label → path */
227
+ availableRoutes?: Record<string, string>;
228
+ /** Additional context for the agent (e.g., form field names, entity schema) */
229
+ custom?: Record<string, unknown>;
230
+ }
231
+
107
232
  declare interface ChatClientConfig extends ConnectionConfig {
108
233
  apiKey?: string;
234
+ authToken?: string;
109
235
  extUid?: string;
110
236
  conversationId?: string;
111
237
  systemPromptId?: number;
112
- channel?: 'widget' | 'api';
238
+ channel?: 'widget' | 'api' | 'sidekick';
239
+ /**
240
+ * Transport selection.
241
+ * - `aikaara` (default): ActionCable to Aikaara Rails (AI streaming).
242
+ * - `tiledesk`: MQTT direct to a self-hosted Tiledesk + chat21 stack.
243
+ * - `dual`: both — Aikaara cable for AI, Tiledesk MQTT for live-agent + file events.
244
+ */
245
+ transport?: TransportMode;
246
+ /**
247
+ * Tiledesk-side identity. Required when `transport` is `tiledesk` or `dual`.
248
+ * `userId` here is the Tiledesk user `_id` and is also used as the SDK conversation
249
+ * subject — Aikaara conversations bound to this user via ext_uid mapping.
250
+ */
251
+ tiledeskIdentity?: {
252
+ userId: string;
253
+ userName?: string;
254
+ departmentId?: string;
255
+ senderFullname?: string;
256
+ };
113
257
  onMessage?: (message: Message) => void;
114
258
  onStatusChange?: (status: string) => void;
115
259
  onError?: (error: Error) => void;
116
260
  onStreamUpdate?: (delta: string, fullContent: string) => void;
117
261
  onConnectionStateChange?: (state: ConnectionState) => void;
262
+ onTemplateMessage?: (template: TemplateMessageEvent) => void;
263
+ }
264
+
265
+ declare interface ChatClientConfig_2 extends ConnectionConfig_2 {
266
+ apiKey?: string;
267
+ authToken?: string;
268
+ extUid?: string;
269
+ conversationId?: string;
270
+ systemPromptId?: number;
271
+ channel?: 'widget' | 'api' | 'sidekick';
272
+ /**
273
+ * Transport selection.
274
+ * - `aikaara` (default): ActionCable to Aikaara Rails (AI streaming).
275
+ * - `tiledesk`: MQTT direct to a self-hosted Tiledesk + chat21 stack.
276
+ * - `dual`: both — Aikaara cable for AI, Tiledesk MQTT for live-agent + file events.
277
+ */
278
+ transport?: TransportMode_2;
279
+ /**
280
+ * Tiledesk-side identity. Required when `transport` is `tiledesk` or `dual`.
281
+ * `userId` here is the Tiledesk user `_id` and is also used as the SDK conversation
282
+ * subject — Aikaara conversations bound to this user via ext_uid mapping.
283
+ */
284
+ tiledeskIdentity?: {
285
+ userId: string;
286
+ userName?: string;
287
+ departmentId?: string;
288
+ senderFullname?: string;
289
+ };
290
+ onMessage?: (message: Message_2) => void;
291
+ onStatusChange?: (status: string) => void;
292
+ onError?: (error: Error) => void;
293
+ onStreamUpdate?: (delta: string, fullContent: string) => void;
294
+ onConnectionStateChange?: (state: ConnectionState_2) => void;
295
+ onTemplateMessage?: (template: TemplateMessageEvent_2) => void;
296
+ }
297
+
298
+ declare interface ChatEvents {
299
+ 'connection:state': ConnectionState_2;
300
+ 'message:received': Message_2;
301
+ 'message:updated': Message_2;
302
+ 'message:sent': Message_2;
303
+ 'stream:start': {
304
+ messageId: string;
305
+ };
306
+ 'stream:update': {
307
+ delta: string;
308
+ content: string;
309
+ };
310
+ 'stream:end': {
311
+ messageId: string;
312
+ usage?: {
313
+ tokensInput: number;
314
+ tokensOutput: number;
315
+ };
316
+ };
317
+ 'typing:start': void;
318
+ 'typing:stop': void;
319
+ 'error': Error;
320
+ 'status': string;
321
+ 'action:edit_entity': EditEntityAction;
322
+ 'action:save_entity': SaveEntityAction;
323
+ 'action:test_tool': TestToolAction;
324
+ 'action:navigate': NavigateAction;
325
+ 'tool:start': {
326
+ toolName: string;
327
+ args: Record<string, unknown>;
328
+ };
329
+ 'tool:end': {
330
+ toolName: string;
331
+ result: unknown;
332
+ isError: boolean;
333
+ };
118
334
  }
119
335
 
120
336
  declare interface ConnectionConfig {
121
337
  baseUrl: string;
122
338
  wsUrl?: string;
123
339
  userToken: string;
340
+ tiledesk?: TiledeskTransportConfig;
341
+ reconnect?: boolean;
342
+ maxReconnectAttempts?: number;
343
+ reconnectInterval?: number;
344
+ }
345
+
346
+ declare interface ConnectionConfig_2 {
347
+ baseUrl: string;
348
+ wsUrl?: string;
349
+ userToken: string;
350
+ tiledesk?: TiledeskTransportConfig_2;
124
351
  reconnect?: boolean;
125
352
  maxReconnectAttempts?: number;
126
353
  reconnectInterval?: number;
@@ -128,10 +355,33 @@ declare interface ConnectionConfig {
128
355
 
129
356
  declare type ConnectionState = 'disconnected' | 'connecting' | 'connected' | 'reconnecting';
130
357
 
358
+ declare type ConnectionState_2 = 'disconnected' | 'connecting' | 'connected' | 'reconnecting';
359
+
360
+ declare interface EditEntityAction {
361
+ action: 'edit_entity';
362
+ entity_type: string;
363
+ entity_id: string | number;
364
+ fields: FieldUpdate[];
365
+ }
366
+
367
+ declare class EventEmitter<Events extends Record<string, any>> {
368
+ private handlers;
369
+ on<K extends keyof Events & string>(event: K, handler: (data: Events[K]) => void): () => void;
370
+ off<K extends keyof Events & string>(event: K, handler: (data: Events[K]) => void): void;
371
+ emit<K extends keyof Events & string>(event: K, data: Events[K]): void;
372
+ removeAllListeners(): void;
373
+ }
374
+
375
+ declare interface FieldUpdate {
376
+ field: string;
377
+ value: unknown;
378
+ previousValue?: unknown;
379
+ }
380
+
131
381
  declare interface Message {
132
382
  id: string;
133
383
  conversationId: string;
134
- role: 'user' | 'assistant' | 'system' | 'tool';
384
+ role: 'user' | 'assistant' | 'system' | 'tool' | 'agent';
135
385
  content: string;
136
386
  toolCalls?: ToolCall[];
137
387
  toolCallResults?: ToolCallResult;
@@ -139,11 +389,195 @@ declare interface Message {
139
389
  tokensOutput?: number;
140
390
  metadata?: Record<string, unknown>;
141
391
  createdAt: string;
142
- status?: 'sending' | 'sent' | 'streaming' | 'complete' | 'error';
392
+ status?: 'sending' | 'sent' | 'delivered' | 'read' | 'streaming' | 'complete' | 'error';
393
+ externalId?: string;
394
+ template?: {
395
+ contentType?: string;
396
+ templateId?: string;
397
+ payload?: unknown;
398
+ };
399
+ attachments?: Array<{
400
+ fileName: string;
401
+ fileUrl: string;
402
+ cloudFileId?: string;
403
+ contentType?: string;
404
+ }>;
405
+ }
406
+
407
+ declare interface Message_2 {
408
+ id: string;
409
+ conversationId: string;
410
+ role: 'user' | 'assistant' | 'system' | 'tool' | 'agent';
411
+ content: string;
412
+ toolCalls?: ToolCall_2[];
413
+ toolCallResults?: ToolCallResult_2;
414
+ tokensInput?: number;
415
+ tokensOutput?: number;
416
+ metadata?: Record<string, unknown>;
417
+ createdAt: string;
418
+ status?: 'sending' | 'sent' | 'delivered' | 'read' | 'streaming' | 'complete' | 'error';
419
+ externalId?: string;
420
+ template?: {
421
+ contentType?: string;
422
+ templateId?: string;
423
+ payload?: unknown;
424
+ };
425
+ attachments?: Array<{
426
+ fileName: string;
427
+ fileUrl: string;
428
+ cloudFileId?: string;
429
+ contentType?: string;
430
+ }>;
431
+ }
432
+
433
+ declare interface NavigateAction {
434
+ navigate_to: string;
143
435
  }
144
436
 
145
437
  export declare function registerComponents(): void;
146
438
 
439
+ declare interface SaveEntityAction {
440
+ action: 'save_entity';
441
+ }
442
+
443
+ declare interface TemplateMessageEvent {
444
+ messageId: string;
445
+ conversationId: string;
446
+ role: 'user' | 'assistant' | 'system' | 'agent';
447
+ contentType?: string;
448
+ templateId?: string;
449
+ payload?: unknown;
450
+ innerMessage?: string;
451
+ raw: unknown;
452
+ }
453
+
454
+ declare interface TemplateMessageEvent_2 {
455
+ messageId: string;
456
+ conversationId: string;
457
+ role: 'user' | 'assistant' | 'system' | 'agent';
458
+ contentType?: string;
459
+ templateId?: string;
460
+ payload?: unknown;
461
+ innerMessage?: string;
462
+ raw: unknown;
463
+ }
464
+
465
+ declare interface TestToolAction {
466
+ action: 'test_tool';
467
+ tool_id: number;
468
+ parameters: Record<string, unknown>;
469
+ }
470
+
471
+ declare interface TiledeskFileTemplateConfig {
472
+ templateId: string;
473
+ headerImgSrc?: string;
474
+ type?: 'link' | 'image';
475
+ isDeepLink?: boolean;
476
+ }
477
+
478
+ declare interface TiledeskFileTemplateConfig_2 {
479
+ templateId: string;
480
+ headerImgSrc?: string;
481
+ type?: 'link' | 'image';
482
+ isDeepLink?: boolean;
483
+ }
484
+
485
+ declare interface TiledeskMessageDefaults {
486
+ channelType?: string;
487
+ channel?: string;
488
+ requestChannel?: string;
489
+ platform?: string;
490
+ medium?: string;
491
+ departmentId?: string;
492
+ attributes?: Record<string, unknown>;
493
+ }
494
+
495
+ declare interface TiledeskMessageDefaults_2 {
496
+ channelType?: string;
497
+ channel?: string;
498
+ requestChannel?: string;
499
+ platform?: string;
500
+ medium?: string;
501
+ departmentId?: string;
502
+ attributes?: Record<string, unknown>;
503
+ }
504
+
505
+ declare interface TiledeskTopicTemplates {
506
+ inbound?: string;
507
+ inboundUpdate?: string;
508
+ outbound?: string;
509
+ presence?: string;
510
+ wildcardSubscribe?: string;
511
+ }
512
+
513
+ declare interface TiledeskTopicTemplates_2 {
514
+ inbound?: string;
515
+ inboundUpdate?: string;
516
+ outbound?: string;
517
+ presence?: string;
518
+ wildcardSubscribe?: string;
519
+ }
520
+
521
+ declare interface TiledeskTransportConfig {
522
+ mqttEndpoint: string;
523
+ jwtToken: string;
524
+ userId: string;
525
+ userName?: string;
526
+ projectId: string;
527
+ appId?: string;
528
+ clientId?: string;
529
+ protocolVersion?: 3 | 4 | 5;
530
+ protocolId?: 'MQIsdp' | 'MQTT';
531
+ mqttUsername?: string;
532
+ connectTimeoutMs?: number;
533
+ keepAliveSec?: number;
534
+ maxReconnectAttempts?: number;
535
+ reconnectMaxDelayMs?: number;
536
+ tokenProvider?: () => Promise<string>;
537
+ wildcardSubscribe?: boolean;
538
+ subscribeQos?: 0 | 1 | 2;
539
+ publishQos?: 0 | 1 | 2;
540
+ publishRetain?: boolean;
541
+ enablePresence?: boolean;
542
+ presencePayloadConnected?: Record<string, unknown>;
543
+ presencePayloadDisconnected?: Record<string, unknown>;
544
+ topicTemplates?: TiledeskTopicTemplates;
545
+ messageDefaults?: TiledeskMessageDefaults;
546
+ fileTemplate?: TiledeskFileTemplateConfig;
547
+ recipientFullnameResolver?: (conversationId: string) => string | undefined;
548
+ senderFullname?: string;
549
+ }
550
+
551
+ declare interface TiledeskTransportConfig_2 {
552
+ mqttEndpoint: string;
553
+ jwtToken: string;
554
+ userId: string;
555
+ userName?: string;
556
+ projectId: string;
557
+ appId?: string;
558
+ clientId?: string;
559
+ protocolVersion?: 3 | 4 | 5;
560
+ protocolId?: 'MQIsdp' | 'MQTT';
561
+ mqttUsername?: string;
562
+ connectTimeoutMs?: number;
563
+ keepAliveSec?: number;
564
+ maxReconnectAttempts?: number;
565
+ reconnectMaxDelayMs?: number;
566
+ tokenProvider?: () => Promise<string>;
567
+ wildcardSubscribe?: boolean;
568
+ subscribeQos?: 0 | 1 | 2;
569
+ publishQos?: 0 | 1 | 2;
570
+ publishRetain?: boolean;
571
+ enablePresence?: boolean;
572
+ presencePayloadConnected?: Record<string, unknown>;
573
+ presencePayloadDisconnected?: Record<string, unknown>;
574
+ topicTemplates?: TiledeskTopicTemplates_2;
575
+ messageDefaults?: TiledeskMessageDefaults_2;
576
+ fileTemplate?: TiledeskFileTemplateConfig_2;
577
+ recipientFullnameResolver?: (conversationId: string) => string | undefined;
578
+ senderFullname?: string;
579
+ }
580
+
147
581
  declare interface ToolCall {
148
582
  id: string;
149
583
  type: 'function';
@@ -153,11 +587,71 @@ declare interface ToolCall {
153
587
  };
154
588
  }
155
589
 
590
+ declare interface ToolCall_2 {
591
+ id: string;
592
+ type: 'function';
593
+ function: {
594
+ name: string;
595
+ arguments: string;
596
+ };
597
+ }
598
+
156
599
  declare interface ToolCallResult {
157
600
  tool_call_id: string;
158
601
  content: string;
159
602
  }
160
603
 
604
+ declare interface ToolCallResult_2 {
605
+ tool_call_id: string;
606
+ content: string;
607
+ }
608
+
609
+ declare type TransportMode = 'aikaara' | 'tiledesk' | 'dual';
610
+
611
+ declare type TransportMode_2 = 'aikaara' | 'tiledesk' | 'dual';
612
+
613
+ declare interface UploadAdapter {
614
+ upload(file: File | Blob, ctx: UploadAdapterContext): Promise<UploadAdapterResult>;
615
+ }
616
+
617
+ declare interface UploadAdapter_2 {
618
+ upload(file: File | Blob, ctx: UploadAdapterContext_2): Promise<UploadAdapterResult_2>;
619
+ }
620
+
621
+ declare interface UploadAdapterContext {
622
+ conversationId: string;
623
+ userId: string;
624
+ projectId?: string;
625
+ appId?: string;
626
+ }
627
+
628
+ declare interface UploadAdapterContext_2 {
629
+ conversationId: string;
630
+ userId: string;
631
+ projectId?: string;
632
+ appId?: string;
633
+ }
634
+
635
+ declare interface UploadAdapterResult {
636
+ url: string;
637
+ fileName: string;
638
+ cloudFileId?: string;
639
+ relativePath?: string;
640
+ contentType?: string;
641
+ byteSize?: number;
642
+ meta?: Record<string, unknown>;
643
+ }
644
+
645
+ declare interface UploadAdapterResult_2 {
646
+ url: string;
647
+ fileName: string;
648
+ cloudFileId?: string;
649
+ relativePath?: string;
650
+ contentType?: string;
651
+ byteSize?: number;
652
+ meta?: Record<string, unknown>;
653
+ }
654
+
161
655
  declare interface WidgetConfig extends ChatClientConfig {
162
656
  position?: 'bottom-right' | 'bottom-left';
163
657
  offset?: {
@@ -180,6 +674,7 @@ declare interface WidgetConfig extends ChatClientConfig {
180
674
  showBubble?: boolean;
181
675
  bubbleText?: string;
182
676
  bubbleIcon?: string;
677
+ uploadAdapter?: UploadAdapter;
183
678
  }
184
679
 
185
680
  export { }