@iai-lib/any-chat-widget 0.1.0-alpha.57 → 0.1.0-alpha.59

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,8 @@
1
+ import { ChatWindowApi } from '@iai-lib/chat-widget/dist/types/types/widget';
2
+ import { TwilioManager } from '../../twilio/twilioManager';
3
+
4
+ /**
5
+ * Hijacks the chat window to use Twilio for live agent communication
6
+ * Maps Twilio events to the chat window API
7
+ */
8
+ export declare function hijackChatForTwilio(chatWindow: ChatWindowApi, twilioManager: TwilioManager): void;
@@ -0,0 +1,326 @@
1
+ /**
2
+ * Type definitions for Twilio Conversations integration
3
+ *
4
+ * These types define the structure of data used throughout the Twilio chat implementation.
5
+ * They mirror the Five9 types structure for API compatibility while adapting to Twilio's model.
6
+ */
7
+ /**
8
+ * Twilio-specific enumerations
9
+ */
10
+ export declare namespace TwilioTypes {
11
+ /**
12
+ * Message content types supported by Twilio Conversations
13
+ */
14
+ enum MessageType {
15
+ TEXT = "TEXT",
16
+ MEDIA = "MEDIA"
17
+ }
18
+ /**
19
+ * Message acknowledgment/delivery status types
20
+ */
21
+ enum AcknowledgeType {
22
+ DELIVERED = "DELIVERED",
23
+ READ = "READ",
24
+ FAILED = "FAILED"
25
+ }
26
+ /**
27
+ * Connection states for the Twilio client
28
+ */
29
+ enum ConnectionState {
30
+ CONNECTING = "connecting",
31
+ CONNECTED = "connected",
32
+ DISCONNECTING = "disconnecting",
33
+ DISCONNECTED = "disconnected",
34
+ DENIED = "denied",
35
+ ERROR = "error"
36
+ }
37
+ /**
38
+ * Conversation lifecycle states
39
+ */
40
+ enum ConversationStatus {
41
+ PENDING = "PENDING",
42
+ ACTIVE = "ACTIVE",
43
+ INACTIVE = "INACTIVE",
44
+ CLOSED = "CLOSED"
45
+ }
46
+ /**
47
+ * Participant types in a conversation
48
+ */
49
+ enum ParticipantType {
50
+ CHAT = "chat",
51
+ SMS = "sms",
52
+ WHATSAPP = "whatsapp"
53
+ }
54
+ /**
55
+ * Message delivery receipt types
56
+ */
57
+ enum DeliveryStatus {
58
+ QUEUED = "queued",
59
+ SENT = "sent",
60
+ DELIVERED = "delivered",
61
+ READ = "read",
62
+ FAILED = "failed",
63
+ UNDELIVERED = "undelivered"
64
+ }
65
+ /**
66
+ * Configuration for initializing Twilio chat
67
+ */
68
+ type TwilioConfig = {
69
+ /** Twilio Account SID (optional - can be managed on backend) */
70
+ accountSid?: string;
71
+ /** Access token for authenticating the client */
72
+ accessToken: string;
73
+ /** Identity/username for the chat participant */
74
+ identity: string;
75
+ /** Conversation SID to join (optional - can be created dynamically) */
76
+ conversationSid?: string;
77
+ /** Token refresh endpoint URL */
78
+ tokenRefreshUrl?: string;
79
+ /** Enable automatic reconnection on connection loss */
80
+ autoReconnect?: boolean;
81
+ /** Maximum reconnection attempts */
82
+ maxReconnectAttempts?: number;
83
+ /** Log level for debugging */
84
+ logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'silent';
85
+ };
86
+ /**
87
+ * Contact information for conversation participants
88
+ */
89
+ type Contact = {
90
+ email?: string;
91
+ firstName?: string;
92
+ lastName?: string;
93
+ phoneNumber?: string;
94
+ attributes?: Record<string, string>;
95
+ };
96
+ /**
97
+ * Parameters for creating a new conversation
98
+ */
99
+ type ConversationParams = {
100
+ /** Friendly name for the conversation */
101
+ friendlyName?: string;
102
+ /** Unique name/identifier for the conversation */
103
+ uniqueName?: string;
104
+ /** Custom attributes for the conversation */
105
+ attributes?: Record<string, any>;
106
+ /** Contact information */
107
+ contact?: Contact;
108
+ /** Participant identity to add */
109
+ participantIdentity?: string;
110
+ };
111
+ /**
112
+ * Message content structure
113
+ */
114
+ type Message = {
115
+ /** Type of message */
116
+ messageType: MessageType;
117
+ /** Text content of the message */
118
+ message: string;
119
+ /** Optional attributes */
120
+ attributes?: Record<string, any>;
121
+ /** Media URLs (for media messages) */
122
+ media?: string[];
123
+ };
124
+ /**
125
+ * Message acknowledgment data
126
+ */
127
+ type AcknowledgeMessage = {
128
+ /** Acknowledgment type */
129
+ type: AcknowledgeType;
130
+ /** Message index or SID to acknowledge */
131
+ messageId: string;
132
+ };
133
+ /**
134
+ * Batch message acknowledgments
135
+ */
136
+ type AcknowledgeMessages = {
137
+ messages: AcknowledgeMessage[];
138
+ };
139
+ /**
140
+ * Event emitted when a message is received
141
+ */
142
+ type MessageAddedEvent = {
143
+ /** Message index in the conversation */
144
+ index: number;
145
+ /** Message SID */
146
+ sid: string;
147
+ /** Message body/text */
148
+ body: string | null;
149
+ /** Author identity */
150
+ author: string;
151
+ /** Author friendly name (if available from participant attributes) */
152
+ authorFriendlyName?: string;
153
+ /** Participant SID */
154
+ participantSid: string;
155
+ /** Timestamp */
156
+ timestamp: Date;
157
+ /** Message attributes */
158
+ attributes: Record<string, any>;
159
+ /** Media attachments */
160
+ media?: any[];
161
+ };
162
+ /**
163
+ * Event emitted when a participant joins
164
+ */
165
+ type ParticipantJoinedEvent = {
166
+ /** Participant SID */
167
+ sid: string;
168
+ /** Participant identity */
169
+ identity: string;
170
+ /** Participant friendly name (if available from attributes) */
171
+ friendlyName?: string;
172
+ /** Participant attributes */
173
+ attributes: Record<string, any>;
174
+ /** Timestamp */
175
+ timestamp: Date;
176
+ };
177
+ /**
178
+ * Event emitted when a participant leaves
179
+ */
180
+ type ParticipantLeftEvent = {
181
+ /** Participant SID */
182
+ sid: string;
183
+ /** Participant identity */
184
+ identity: string;
185
+ /** Participant friendly name (if available from attributes) */
186
+ friendlyName?: string;
187
+ /** Timestamp */
188
+ timestamp: Date;
189
+ };
190
+ /**
191
+ * Event emitted when typing is detected
192
+ */
193
+ type TypingEvent = {
194
+ /** Participant identity who is typing */
195
+ identity: string;
196
+ /** Participant SID */
197
+ participantSid: string;
198
+ /** Is currently typing */
199
+ isTyping: boolean;
200
+ };
201
+ /**
202
+ * Event emitted on connection state changes
203
+ */
204
+ type ConnectionStateChangeEvent = {
205
+ /** Previous connection state */
206
+ previous: ConnectionState;
207
+ /** Current connection state */
208
+ current: ConnectionState;
209
+ };
210
+ /**
211
+ * Event emitted on conversation state changes
212
+ */
213
+ type ConversationStateChangeEvent = {
214
+ /** Conversation SID */
215
+ conversationSid: string;
216
+ /** Previous status */
217
+ previous: ConversationStatus;
218
+ /** Current status */
219
+ current: ConversationStatus;
220
+ };
221
+ /**
222
+ * Error event data
223
+ */
224
+ type ErrorEvent = {
225
+ /** Error message */
226
+ message: string;
227
+ /** Error code */
228
+ code?: number;
229
+ /** Error status */
230
+ status?: number;
231
+ /** Additional error details */
232
+ details?: any;
233
+ /** Timestamp */
234
+ timestamp: Date;
235
+ };
236
+ /**
237
+ * Token refresh data
238
+ */
239
+ type TokenData = {
240
+ /** New access token */
241
+ token: string;
242
+ /** Token expiration time */
243
+ expiresAt?: Date;
244
+ };
245
+ /**
246
+ * Conversation metadata
247
+ */
248
+ type ConversationInfo = {
249
+ /** Conversation SID */
250
+ sid: string;
251
+ /** Friendly name */
252
+ friendlyName: string | null;
253
+ /** Unique name */
254
+ uniqueName: string | null;
255
+ /** Conversation attributes */
256
+ attributes: Record<string, any>;
257
+ /** Creation date */
258
+ dateCreated: Date;
259
+ /** Last update date */
260
+ dateUpdated: Date;
261
+ /** Conversation state */
262
+ state: string;
263
+ /** Current status */
264
+ status: ConversationStatus;
265
+ };
266
+ /**
267
+ * Participant information
268
+ */
269
+ type Participant = {
270
+ /** Participant SID */
271
+ sid: string;
272
+ /** Identity */
273
+ identity: string;
274
+ /** Participant type */
275
+ type: ParticipantType;
276
+ /** Attributes */
277
+ attributes: Record<string, any>;
278
+ /** Last read message index */
279
+ lastReadMessageIndex: number | null;
280
+ /** Last read timestamp */
281
+ lastReadTimestamp: Date | null;
282
+ };
283
+ /**
284
+ * Message delivery receipt
285
+ */
286
+ type DeliveryReceipt = {
287
+ /** Message SID */
288
+ messageSid: string;
289
+ /** Delivery status */
290
+ status: DeliveryStatus;
291
+ /** Participant SID who acknowledged */
292
+ participantSid: string;
293
+ /** Timestamp */
294
+ timestamp: Date;
295
+ };
296
+ /**
297
+ * Session state for serialization/deserialization
298
+ */
299
+ type SessionState = {
300
+ /** Access token */
301
+ accessToken: string | null;
302
+ /** Identity */
303
+ identity: string | null;
304
+ /** Conversation SID */
305
+ conversationSid: string | null;
306
+ /** Conversation started flag */
307
+ conversationStarted: boolean;
308
+ /** Connection state */
309
+ connectionState: ConnectionState;
310
+ /** Conversation status */
311
+ conversationStatus: ConversationStatus;
312
+ };
313
+ /**
314
+ * Reconnection options
315
+ */
316
+ type ReconnectOptions = {
317
+ /** Maximum number of retry attempts */
318
+ maxAttempts?: number;
319
+ /** Initial delay in milliseconds */
320
+ initialDelay?: number;
321
+ /** Maximum delay in milliseconds */
322
+ maxDelay?: number;
323
+ /** Backoff multiplier */
324
+ backoffMultiplier?: number;
325
+ };
326
+ }
@@ -0,0 +1,247 @@
1
+ import { TwilioTypes } from './twilio.types';
2
+ import { Client as TwilioClient, Conversation as TwilioConversation } from '@twilio/conversations';
3
+
4
+ /**
5
+ * Main manager class for Twilio Conversations
6
+ *
7
+ * Provides a high-level API for chat functionality that mirrors Five9Manager
8
+ */
9
+ export declare class TwilioManager {
10
+ /**
11
+ * Event emitter for manager events
12
+ */
13
+ private readonly _emitter;
14
+ /**
15
+ * Public event listener registration
16
+ */
17
+ readonly on: {
18
+ <Key extends string | number | symbol>(type: Key, handler: import('mitt').Handler<any>): void;
19
+ (type: "*", handler: import('mitt').WildcardHandler<any>): void;
20
+ };
21
+ /**
22
+ * Public event listener removal
23
+ */
24
+ readonly off: {
25
+ <Key extends string | number | symbol>(type: Key, handler?: import('mitt').Handler<any> | undefined): void;
26
+ (type: "*", handler: import('mitt').WildcardHandler<any>): void;
27
+ };
28
+ /**
29
+ * Twilio Conversations client instance
30
+ */
31
+ private client;
32
+ /**
33
+ * Current active conversation
34
+ */
35
+ private conversation;
36
+ /**
37
+ * Current access token
38
+ */
39
+ private accessToken;
40
+ /**
41
+ * User identity
42
+ */
43
+ private identity;
44
+ /**
45
+ * Current conversation SID
46
+ */
47
+ private conversationSid;
48
+ /**
49
+ * Flag indicating if conversation has started
50
+ */
51
+ conversationStarted: boolean;
52
+ /**
53
+ * Current connection state
54
+ */
55
+ private connectionState;
56
+ /**
57
+ * Current conversation status
58
+ */
59
+ private conversationStatus;
60
+ /**
61
+ * Configuration
62
+ */
63
+ private config;
64
+ /**
65
+ * Token refresh URL
66
+ */
67
+ private tokenRefreshUrl?;
68
+ /**
69
+ * Token refresh timeout handle
70
+ */
71
+ private tokenRefreshTimeout?;
72
+ /**
73
+ * Message queue for offline scenarios
74
+ */
75
+ private messageQueue;
76
+ /**
77
+ * Typing timeout handle
78
+ */
79
+ private typingTimeout?;
80
+ /**
81
+ * Serializes the manager state for persistence
82
+ *
83
+ * @returns Serialized state as JSON string
84
+ */
85
+ serialize(): string;
86
+ /**
87
+ * Deserializes and restores manager state
88
+ *
89
+ * @param rawData - Serialized state JSON string
90
+ * @returns Restored TwilioManager instance
91
+ */
92
+ static deserialize(rawData: string): TwilioManager;
93
+ /**
94
+ * Sets up event listeners for the Twilio client
95
+ */
96
+ private setupClientListeners;
97
+ /**
98
+ * Initializes the Twilio Conversations client
99
+ *
100
+ * @param config - Twilio configuration
101
+ * @throws Error if initialization fails
102
+ */
103
+ initialize(config: TwilioTypes.TwilioConfig): Promise<void>;
104
+ /**
105
+ * Waits for the Twilio client to be ready
106
+ */
107
+ private waitForClientReady;
108
+ /**
109
+ * Maps Twilio SDK connection state to our ConnectionState enum
110
+ *
111
+ * @param state - Twilio SDK state string
112
+ * @returns Mapped ConnectionState
113
+ */
114
+ private mapConnectionState;
115
+ /**
116
+ * Refreshes the access token
117
+ *
118
+ * @throws Error if token refresh fails
119
+ */
120
+ private refreshToken;
121
+ /**
122
+ * Creates a new conversation
123
+ *
124
+ * @param params - Conversation parameters
125
+ * @returns Conversation information
126
+ * @throws Error if conversation creation fails
127
+ */
128
+ createConversation(params: TwilioTypes.ConversationParams): Promise<TwilioTypes.ConversationInfo>;
129
+ /**
130
+ * Joins an existing conversation
131
+ *
132
+ * @param conversationSid - Conversation SID to join
133
+ */
134
+ private joinConversation;
135
+ /**
136
+ * Initializes a conversation (mirrors Five9Manager API)
137
+ *
138
+ * This method provides compatibility with Five9Manager's API
139
+ *
140
+ * @param params - Conversation parameters
141
+ * @returns Conversation information
142
+ */
143
+ initializeConversation(params: TwilioTypes.ConversationParams): Promise<TwilioTypes.ConversationInfo>;
144
+ /**
145
+ * Sets up event listeners for a conversation
146
+ *
147
+ * @param conversation - Twilio Conversation instance
148
+ */
149
+ private setupConversationListeners;
150
+ /**
151
+ * Sends a message to the current conversation
152
+ *
153
+ * @param content - Message text content or options object
154
+ * @param attributes - Optional message attributes for Twilio Flex/Studio Flow context
155
+ * @throws Error if conversation is not started or message sending fails
156
+ */
157
+ sendMessage(content: string | {
158
+ body: string;
159
+ attributes?: Record<string, any>;
160
+ }, attributes?: Record<string, any>): Promise<void>;
161
+ /**
162
+ * Sends typing indicator to the conversation
163
+ *
164
+ * @throws Error if conversation is not started
165
+ */
166
+ sendTyping(): Promise<void>;
167
+ /**
168
+ * Acknowledges/marks messages as read
169
+ *
170
+ * @param messageId - Message index or SID to acknowledge
171
+ * @throws Error if conversation is not started
172
+ */
173
+ acknowledgeMessage(messageId: string): Promise<void>;
174
+ /**
175
+ * Processes queued messages after reconnection
176
+ */
177
+ private processMessageQueue;
178
+ /**
179
+ * Terminates the current conversation
180
+ *
181
+ * @throws Error if conversation is not started
182
+ */
183
+ terminateConversation(): Promise<void>;
184
+ /**
185
+ * Connects to Twilio (mirrors Five9Manager API)
186
+ */
187
+ connect(): Promise<void>;
188
+ /**
189
+ * Reconnects the client after disconnection
190
+ */
191
+ private reconnectClient;
192
+ /**
193
+ * Disconnects from Twilio
194
+ */
195
+ disconnect(): Promise<void>;
196
+ /**
197
+ * Manually triggers reconnection
198
+ */
199
+ reconnect(): Promise<void>;
200
+ /**
201
+ * Destroys the manager and cleans up all resources
202
+ */
203
+ destroy(): void;
204
+ /**
205
+ * Handles errors and emits error events
206
+ *
207
+ * @param message - Error message
208
+ * @param error - Error object
209
+ */
210
+ private handleError;
211
+ /**
212
+ * Gets the current access token
213
+ */
214
+ get token(): string | null;
215
+ /**
216
+ * Gets the user identity
217
+ */
218
+ get userIdentity(): string | null;
219
+ /**
220
+ * Gets the current conversation SID
221
+ */
222
+ get currentConversationSid(): string | null;
223
+ /**
224
+ * Gets the current connection status
225
+ */
226
+ getConnectionStatus(): TwilioTypes.ConnectionState;
227
+ /**
228
+ * Gets the current conversation state
229
+ */
230
+ getConversationState(): TwilioTypes.ConversationStatus;
231
+ /**
232
+ * Checks if currently connected
233
+ */
234
+ isConnected(): boolean;
235
+ /**
236
+ * Gets the Twilio client instance (for advanced usage)
237
+ */
238
+ getClient(): TwilioClient | null;
239
+ /**
240
+ * Gets the current conversation instance (for advanced usage)
241
+ */
242
+ getConversation(): TwilioConversation | null;
243
+ /**
244
+ * Gets the current state for serialization/persistence
245
+ */
246
+ getState(): Record<string, any>;
247
+ }
@@ -1,3 +1,13 @@
1
1
  interface ImportMeta {
2
- env: Record<string, string>;
2
+ env: {
3
+ // Chat system selection
4
+ VITE_ENABLE_TWILIO_CHAT?: string;
5
+
6
+ // Twilio configuration
7
+ VITE_FIVE9_BACKEND_URL?: string;
8
+ VITE_TWILIO_STUDIO_FLOW_SID?: string;
9
+
10
+ // Generic catch-all for other env vars
11
+ [key: string]: string | undefined;
12
+ };
3
13
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@iai-lib/any-chat-widget",
3
3
  "license": "SEE LICENSE IN LICENSE.md",
4
- "version": "0.1.0-alpha.57",
4
+ "version": "0.1.0-alpha.59",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -40,6 +40,7 @@
40
40
  "dependencies": {
41
41
  "@iai-lib/chat-widget": "^1.0.0-alpha.51",
42
42
  "@iai-lib/sdk": "^1.0.0-alpha.32",
43
+ "@twilio/conversations": "^2.6.5",
43
44
  "cleave.js": "^1.6.0",
44
45
  "dayjs": "^1.11.11",
45
46
  "lit": "^3.1.3",
@@ -47,5 +48,5 @@
47
48
  "mitt": "^3.0.1",
48
49
  "zod": "^3.23.8"
49
50
  },
50
- "gitHead": "4744603e2d8e45116e19196681a2727c668c4453"
51
+ "gitHead": "9e9c86e11069785d5c6fd91510ca303020743055"
51
52
  }