@opencx/widget 2.4.3 → 2.4.4-rn.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.
Files changed (49) hide show
  1. package/README.md +30 -30
  2. package/dist/basic.cjs +51 -51
  3. package/dist/basic.cjs.map +1 -1
  4. package/dist/basic.js +1983 -1981
  5. package/dist/basic.js.map +1 -1
  6. package/dist/core/client/api.d.ts +36 -6
  7. package/dist/core/client/chat.d.ts +10 -20
  8. package/dist/core/client/config.d.ts +2 -3
  9. package/dist/core/client/contact.d.ts +13 -12
  10. package/dist/core/client/index.d.ts +1 -0
  11. package/dist/core/index.d.ts +0 -1
  12. package/dist/core/platform/index.d.ts +5 -5
  13. package/dist/core/platform/logger.d.ts +14 -0
  14. package/dist/core/platform/storage.d.ts +46 -0
  15. package/dist/core/types/helpers.d.ts +12 -0
  16. package/dist/core/types/index.d.ts +1 -1
  17. package/dist/core/types/messages.d.ts +2 -2
  18. package/dist/core/types/pub-sub.d.ts +24 -8
  19. package/dist/core/types/schemas-v2.d.ts +22 -25
  20. package/dist/{index-Bb4FAD9K.cjs → index-BVoLVQCZ.cjs} +3 -3
  21. package/dist/index-BVoLVQCZ.cjs.map +1 -0
  22. package/dist/{index-D2ByqkLZ.js → index-Cjs7ckCU.js} +92 -87
  23. package/dist/index-Cjs7ckCU.js.map +1 -0
  24. package/dist/index.cjs +1 -1
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.js +705 -343
  27. package/dist/index.js.map +1 -1
  28. package/dist/package.json.d.ts +156 -0
  29. package/dist/react.cjs +1 -1
  30. package/dist/react.cjs.map +1 -1
  31. package/dist/react.js +1 -1
  32. package/dist/react.js.map +1 -1
  33. package/dist/schemas-BBAV6Sd_.js.map +1 -1
  34. package/dist/schemas-wky4fpbc.cjs.map +1 -1
  35. package/dist/style.css +1 -1
  36. package/dist-embed/script.js +5 -5
  37. package/dist-embed/script.js.map +1 -1
  38. package/package.json +3 -1
  39. package/dist/core/client/chat.test.d.ts +0 -1
  40. package/dist/core/client/client.d.ts +0 -9
  41. package/dist/core/client/config.test.d.ts +0 -1
  42. package/dist/core/client/contact.test.d.ts +0 -1
  43. package/dist/index-Bb4FAD9K.cjs.map +0 -1
  44. package/dist/index-D2ByqkLZ.js.map +0 -1
  45. package/dist/react-bindings/context/ChatContext.d.ts +0 -127
  46. package/dist/react-bindings/hooks/useContact.d.ts +0 -9
  47. package/dist/react-bindings/hooks/usePubsub.d.ts +0 -3
  48. package/dist/react-bindings/hooks/useSendMessage.d.ts +0 -6
  49. package/dist/react-bindings/index.d.ts +0 -3
@@ -1,9 +1,8 @@
1
- import { HandleContactMessageOutputSchema, WidgetHistorySchema, WidgetPreludeSchema, WidgetSessionSchema } from '../types/schemas-v2';
2
- import { CoreOptions, SendMessageInput, ConsumerType } from '../types';
1
+ import { HttpChatInputSchema, WidgetHistorySchema, WidgetPreludeSchema, WidgetSessionSchema } from '../types/schemas-v2';
2
+ import { ConsumerType } from '../types';
3
+ import { NormalizedConfig } from './config';
3
4
  export interface ApiCallerOptions {
4
- apiUrl: string;
5
- token: string;
6
- coreOptions: CoreOptions;
5
+ config: NormalizedConfig;
7
6
  }
8
7
  export declare class ApiCaller {
9
8
  #private;
@@ -14,7 +13,38 @@ export declare class ApiCaller {
14
13
  contactName: string;
15
14
  }>;
16
15
  widgetPrelude(): Promise<WidgetPreludeSchema>;
17
- handleMessage(message: SendMessageInput): Promise<HandleContactMessageOutputSchema>;
16
+ handleMessage(message: HttpChatInputSchema): Promise<{
17
+ success: true;
18
+ code?: string | undefined;
19
+ options?: {
20
+ value: string[];
21
+ type: "options";
22
+ } | undefined;
23
+ autopilotResponse?: {
24
+ value: {
25
+ content: string;
26
+ error: boolean;
27
+ };
28
+ type: "text";
29
+ id?: string | undefined;
30
+ } | undefined;
31
+ uiResponse?: {
32
+ value: {
33
+ type: "ui_component";
34
+ name: string;
35
+ request_response?: unknown;
36
+ content?: string | undefined;
37
+ };
38
+ type: "ui";
39
+ } | undefined;
40
+ sessionIsHandedOff?: boolean | undefined;
41
+ } | {
42
+ error: {
43
+ message?: string | undefined;
44
+ code?: string | undefined;
45
+ };
46
+ success: false;
47
+ }>;
18
48
  getSessionHistory(sessionId: string, lastMessageTimestamp?: string): Promise<WidgetHistorySchema[]>;
19
49
  createSession(): Promise<WidgetSessionSchema>;
20
50
  getSession(sessionId: string): Promise<WidgetSessionSchema>;
@@ -1,18 +1,23 @@
1
1
  import { PubSub } from '../types/pub-sub';
2
2
  import { MessageType } from '../types';
3
3
  import { ApiCaller } from './api';
4
+ import { HttpChatInputSchema } from '../types/schemas-v2';
5
+ import { LoadingState, ErrorState, SomeOptional } from '../types/helpers';
6
+ import { ConfigInstance } from './config';
7
+ import { Platform } from '../platform';
4
8
  type ChatState = {
5
- lastUpdated: number | null;
6
9
  messages: MessageType[];
7
10
  keyboard: {
8
11
  options: string[];
9
12
  } | null;
13
+ loading: LoadingState;
14
+ error: ErrorState;
10
15
  };
11
16
  type ChatOptions = {
12
17
  api: ApiCaller;
13
- persistSession?: boolean;
14
- useSoundEffects?: boolean;
18
+ config: ConfigInstance;
15
19
  onSessionDestroy?: () => void;
20
+ platform: Platform;
16
21
  };
17
22
  export declare function createChat(options: ChatOptions): {
18
23
  chatState: PubSub<ChatState>;
@@ -28,22 +33,7 @@ export declare function createChat(options: ChatOptions): {
28
33
  isHandedOff: boolean;
29
34
  isOpened: boolean;
30
35
  } | null>;
31
- sendMessage: (input: {
32
- content: {
33
- text: string;
34
- };
35
- attachments?: any[];
36
- id?: string;
37
- language?: string;
38
- user?: {
39
- external_id?: string;
40
- name?: string;
41
- email?: string;
42
- phone?: string;
43
- customData?: Record<string, string>;
44
- avatarUrl?: string;
45
- };
46
- }) => Promise<void>;
36
+ sendMessage: (input: SomeOptional<Omit<HttpChatInputSchema, "bot_token">, "session_id" | "user">) => Promise<boolean>;
47
37
  createSession: () => Promise<{
48
38
  id: string;
49
39
  channel: string;
@@ -55,7 +45,7 @@ export declare function createChat(options: ChatOptions): {
55
45
  updatedAt: Date;
56
46
  isHandedOff: boolean;
57
47
  isOpened: boolean;
58
- }>;
48
+ } | null>;
59
49
  clearSession: () => Promise<void>;
60
50
  cleanup: () => void;
61
51
  };
@@ -1,5 +1,5 @@
1
1
  import { CoreOptions } from '../types';
2
- type NormalizedConfig = Required<Omit<CoreOptions, 'contactToken'>> & {
2
+ export type NormalizedConfig = Required<Omit<CoreOptions, 'contactToken'>> & {
3
3
  contactToken: string | undefined | null;
4
4
  soundEffectFiles: {
5
5
  messageArrived: string;
@@ -13,7 +13,7 @@ type NormalizedConfig = Required<Omit<CoreOptions, 'contactToken'>> & {
13
13
  useSoundEffects: boolean;
14
14
  };
15
15
  };
16
- type ConfigInstance = {
16
+ export type ConfigInstance = {
17
17
  getConfig: () => NormalizedConfig;
18
18
  getApiConfig: () => {
19
19
  apiUrl: string;
@@ -31,4 +31,3 @@ type ConfigInstance = {
31
31
  getDebugMode: () => boolean;
32
32
  };
33
33
  export declare function createConfig(options: CoreOptions): ConfigInstance;
34
- export {};
@@ -1,32 +1,33 @@
1
1
  import { PubSub } from '../types/pub-sub';
2
+ import { ConsumerType } from '../types';
2
3
  import { ApiCaller } from './api';
3
4
  import { Platform } from '../platform';
5
+ import { LoadingState, ErrorState } from '../types/helpers';
6
+ import { ConfigInstance } from './config';
7
+ type ContactState = {
8
+ contact: ConsumerType | null;
9
+ loading: LoadingState;
10
+ error: ErrorState;
11
+ };
4
12
  type ContactOptions = {
5
13
  api: ApiCaller;
6
- botToken: string;
7
14
  platform: Platform;
8
- collectUserData?: boolean;
9
- user?: {
10
- external_id?: string;
11
- name?: string;
12
- email?: string;
13
- phone?: string;
14
- customData?: Record<string, string>;
15
- avatarUrl?: string;
16
- };
15
+ config: ConfigInstance;
17
16
  };
18
17
  export declare function createContact(options: ContactOptions): {
18
+ contactState: PubSub<ContactState>;
19
19
  shouldCollectData: () => {
20
20
  should: boolean;
21
21
  reason?: string;
22
22
  };
23
- cleanup: () => void;
24
- contactState: PubSub<{
23
+ loadContact: () => Promise<any>;
24
+ saveContact: (contactData: Partial<ConsumerType>) => Promise<{
25
25
  email: string | null;
26
26
  id: string;
27
27
  name: string | null;
28
28
  avatar_url: string | null;
29
29
  created_at: string;
30
30
  } | null>;
31
+ cleanup: () => Promise<void>;
31
32
  };
32
33
  export {};
@@ -1,3 +1,4 @@
1
1
  export { createChat } from './chat';
2
2
  export { createContact } from './contact';
3
3
  export { ApiCaller, type ApiCallerOptions } from './api';
4
+ export { createConfig } from './config';
@@ -1,4 +1,3 @@
1
- export * from './client';
2
1
  export * from './errors';
3
2
  export * from './types';
4
3
  export * from './platform';
@@ -1,11 +1,11 @@
1
+ import { Logger } from './logger';
2
+ import { Storage } from './storage';
3
+ export * from './storage';
4
+ export type { Storage };
1
5
  export interface Platform {
2
6
  storage?: Storage;
7
+ logger?: Logger;
3
8
  env: {
4
9
  platform: string;
5
10
  };
6
- date: {
7
- now(): number;
8
- toISOString(date: number): string;
9
- };
10
11
  }
11
- export declare function createDefaultPlatform(): Platform;
@@ -0,0 +1,14 @@
1
+ export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
2
+ export interface Logger {
3
+ debug(message: string, ...args: any[]): void;
4
+ info(message: string, ...args: any[]): void;
5
+ warn(message: string, ...args: any[]): void;
6
+ error(message: string, ...args: any[]): void;
7
+ setLevel(level: LogLevel): void;
8
+ }
9
+ export type LoggerOptions = {
10
+ level?: LogLevel;
11
+ prefix?: string;
12
+ enabled?: boolean;
13
+ };
14
+ export declare function createLogger(options?: LoggerOptions): Logger;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Platform-agnostic storage interface that can be implemented
3
+ * for different environments (web, mobile, desktop, etc.)
4
+ */
5
+ export interface Storage {
6
+ /**
7
+ * Retrieves the value associated with the given key
8
+ * @param key The key to look up
9
+ * @returns The stored value or null if not found
10
+ */
11
+ getItem(key: string): Promise<string | null>;
12
+ /**
13
+ * Stores a value with the given key
14
+ * @param key The key to store under
15
+ * @param value The value to store
16
+ */
17
+ setItem(key: string, value: string): Promise<void>;
18
+ /**
19
+ * Removes the value associated with the given key
20
+ * @param key The key to remove
21
+ */
22
+ removeItem(key: string): Promise<void>;
23
+ /**
24
+ * Checks if the storage is available and working
25
+ * @returns true if storage is available and working
26
+ */
27
+ isAvailable?(): boolean;
28
+ }
29
+ /**
30
+ * Helper function to check if storage is available and working
31
+ */
32
+ export declare function isStorageAvailable(storage: Storage | undefined): storage is Storage;
33
+ /**
34
+ * Type for the result of a safe storage operation
35
+ */
36
+ export type StorageOperationResult<T> = {
37
+ success: true;
38
+ result: T;
39
+ } | {
40
+ success: false;
41
+ error: Error;
42
+ };
43
+ /**
44
+ * Helper function to safely perform storage operations
45
+ */
46
+ export declare function safeStorageOperation<T>(operation: () => Promise<T>, errorMessage: string): Promise<StorageOperationResult<T>>;
@@ -3,3 +3,15 @@ export type FunctionReturningPromise = (...args: any[]) => Promise<any>;
3
3
  export type MakeKeysNotNullable<T, K extends keyof T> = Omit<T, K> & {
4
4
  [P in K]-?: NonNullable<T[P]>;
5
5
  };
6
+ export type LoadingReason = 'sending_message' | 'creating_session' | 'polling' | 'loading_contact' | 'saving_contact' | 'cleaning_up' | null;
7
+ export type LoadingState = {
8
+ isLoading: boolean;
9
+ reason?: LoadingReason;
10
+ };
11
+ export type ErrorCode = 'SESSION_CREATION_FAILED' | 'SESSION_CLEAR_FAILED' | 'SESSION_PERSISTENCE_FAILED' | 'MESSAGE_SEND_FAILED' | 'NO_ACTIVE_SESSION' | 'CONTACT_PERSISTENCE_FAILED' | 'CONTACT_LOAD_FAILED' | 'CONTACT_SAVE_FAILED' | 'CONTACT_CLEANUP_FAILED';
12
+ export type ErrorState = {
13
+ hasError: boolean;
14
+ message?: string;
15
+ code?: ErrorCode;
16
+ };
17
+ export type SomeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
@@ -23,11 +23,11 @@ export interface SendMessageInput extends Record<string, unknown> {
23
23
  export interface CoreOptions {
24
24
  token: string;
25
25
  apiUrl?: string;
26
- transport?: 'socket' | 'http';
27
26
  socketUrl?: string;
28
27
  headers?: Record<string, string>;
29
28
  queryParams?: Record<string, string>;
30
29
  pathParams?: Record<string, string>;
30
+ collectUserData?: boolean;
31
31
  debug?: boolean;
32
32
  language?: string;
33
33
  user?: User;
@@ -5,7 +5,7 @@ export type UserMessageType = {
5
5
  content: string;
6
6
  deliveredAt: string | null;
7
7
  attachments?: ChatAttachmentType[];
8
- timestamp?: string;
8
+ timestamp: string;
9
9
  user?: {
10
10
  name?: string;
11
11
  email?: string;
@@ -19,7 +19,7 @@ export type BotMessageType<TData = unknown> = {
19
19
  type: "FROM_BOT";
20
20
  component: string;
21
21
  data: TData;
22
- timestamp?: string;
22
+ timestamp: string;
23
23
  original?: ChatHistoryMessageType;
24
24
  agent?: AgentType;
25
25
  attachments?: ChatAttachmentType[];
@@ -1,31 +1,47 @@
1
1
  export type Subscriber<T> = (data: T) => void;
2
+ export declare enum LifecycleEvent {
3
+ INIT = "init",
4
+ STATE_CHANGE = "stateChange",
5
+ BEFORE_UPDATE = "beforeUpdate",
6
+ AFTER_UPDATE = "afterUpdate",
7
+ DESTROY = "destroy",
8
+ ERROR = "error"
9
+ }
10
+ type LifecycleListener = (event: {
11
+ type: LifecycleEvent;
12
+ timestamp: number;
13
+ data?: any;
14
+ }) => void;
2
15
  export declare class PubSub<S> {
3
16
  #private;
4
17
  private subscribers;
5
18
  private initialState;
19
+ private lifecycleListeners;
6
20
  constructor(state: S);
21
+ private emitLifecycle;
7
22
  /**
8
23
  * Subscribe to state changes
9
24
  * @param callback Function to call when state changes
10
25
  * @returns Unsubscribe function
11
26
  */
12
- subscribe(callback: Subscriber<S>): () => void;
27
+ subscribe: (callback: Subscriber<S>) => () => void;
28
+ onLifecycle: (event: LifecycleEvent, listener: LifecycleListener) => () => void;
13
29
  /**
14
30
  * Get the current state
15
31
  */
16
- getState(): S;
32
+ getState: () => S;
17
33
  /**
18
34
  * Set the state and notify subscribers if the state changes
19
35
  * @param newState The new state to set
20
36
  */
21
- setState(newState: S): void;
22
- setStatePartial(_s: Partial<S>): void;
37
+ setState: (newState: S) => void;
38
+ setStatePartial: (_s: Partial<S>) => void;
23
39
  /**
24
40
  * Clear all subscriptions
25
41
  */
26
- clear(): void;
27
- reset(): void;
28
- getSnapshot(): S;
29
- get state(): S;
42
+ clear: () => void;
43
+ reset: () => void;
44
+ lastUpdated: () => number | null;
30
45
  }
31
46
  export declare function createPubSub<S>(state: S): PubSub<S>;
47
+ export {};
@@ -314,7 +314,7 @@ declare const widgetHistorySchema: z.ZodObject<{
314
314
  }, {
315
315
  text?: string | null | undefined;
316
316
  }>;
317
- sentAt: z.ZodNullable<z.ZodOptional<z.ZodDate>>;
317
+ sentAt: z.ZodNullable<z.ZodOptional<z.ZodString>>;
318
318
  actionCalls: z.ZodNullable<z.ZodOptional<z.ZodArray<z.ZodObject<{
319
319
  actionName: z.ZodString;
320
320
  args: z.ZodRecord<z.ZodString, z.ZodUnknown>;
@@ -365,7 +365,7 @@ declare const widgetHistorySchema: z.ZodObject<{
365
365
  size: number;
366
366
  url: string;
367
367
  }[] | null | undefined;
368
- sentAt?: Date | null | undefined;
368
+ sentAt?: string | null | undefined;
369
369
  actionCalls?: {
370
370
  actionName: string;
371
371
  args: Record<string, unknown>;
@@ -389,7 +389,7 @@ declare const widgetHistorySchema: z.ZodObject<{
389
389
  size: number;
390
390
  url: string;
391
391
  }[] | null | undefined;
392
- sentAt?: Date | null | undefined;
392
+ sentAt?: string | null | undefined;
393
393
  actionCalls?: {
394
394
  actionName: string;
395
395
  args: Record<string, unknown>;
@@ -437,13 +437,12 @@ declare const widgetSessionSchema: z.ZodObject<{
437
437
  isOpened: boolean;
438
438
  }>;
439
439
  declare const httpChatInputDto: z.ZodObject<{
440
- id: z.ZodOptional<z.ZodString>;
441
440
  content: z.ZodString;
442
441
  session_id: z.ZodString;
443
- headers: z.ZodRecord<z.ZodString, z.ZodString>;
442
+ headers: z.ZodNullable<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>>;
444
443
  bot_token: z.ZodString;
445
444
  query_params: z.ZodNullable<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>>;
446
- user: z.ZodObject<{
445
+ user: z.ZodNullable<z.ZodOptional<z.ZodObject<{
447
446
  email: z.ZodOptional<z.ZodString>;
448
447
  name: z.ZodOptional<z.ZodString>;
449
448
  phone: z.ZodOptional<z.ZodString>;
@@ -461,7 +460,7 @@ declare const httpChatInputDto: z.ZodObject<{
461
460
  avatar?: string | undefined;
462
461
  phone?: string | undefined;
463
462
  customData?: Record<string, string> | undefined;
464
- }>;
463
+ }>>>;
465
464
  language: z.ZodNullable<z.ZodOptional<z.ZodString>>;
466
465
  attachments: z.ZodNullable<z.ZodOptional<z.ZodArray<z.ZodObject<{
467
466
  id: z.ZodString;
@@ -485,16 +484,7 @@ declare const httpChatInputDto: z.ZodObject<{
485
484
  }, "strip", z.ZodTypeAny, {
486
485
  session_id: string;
487
486
  content: string;
488
- user: {
489
- email?: string | undefined;
490
- name?: string | undefined;
491
- avatar?: string | undefined;
492
- phone?: string | undefined;
493
- customData?: Record<string, string> | undefined;
494
- };
495
- headers: Record<string, string>;
496
487
  bot_token: string;
497
- id?: string | undefined;
498
488
  language?: string | null | undefined;
499
489
  attachments?: {
500
490
  id: string;
@@ -503,20 +493,19 @@ declare const httpChatInputDto: z.ZodObject<{
503
493
  size: number;
504
494
  url: string;
505
495
  }[] | null | undefined;
506
- query_params?: Record<string, string> | null | undefined;
507
- }, {
508
- session_id: string;
509
- content: string;
510
- user: {
496
+ user?: {
511
497
  email?: string | undefined;
512
498
  name?: string | undefined;
513
499
  avatar?: string | undefined;
514
500
  phone?: string | undefined;
515
501
  customData?: Record<string, string> | undefined;
516
- };
517
- headers: Record<string, string>;
502
+ } | null | undefined;
503
+ headers?: Record<string, string> | null | undefined;
504
+ query_params?: Record<string, string> | null | undefined;
505
+ }, {
506
+ session_id: string;
507
+ content: string;
518
508
  bot_token: string;
519
- id?: string | undefined;
520
509
  language?: string | null | undefined;
521
510
  attachments?: {
522
511
  id: string;
@@ -525,11 +514,19 @@ declare const httpChatInputDto: z.ZodObject<{
525
514
  size: number;
526
515
  url: string;
527
516
  }[] | null | undefined;
517
+ user?: {
518
+ email?: string | undefined;
519
+ name?: string | undefined;
520
+ avatar?: string | undefined;
521
+ phone?: string | undefined;
522
+ customData?: Record<string, string> | undefined;
523
+ } | null | undefined;
524
+ headers?: Record<string, string> | null | undefined;
528
525
  query_params?: Record<string, string> | null | undefined;
529
526
  }>;
530
527
  declare const handleContactMessageOutputSchema: z.ZodDiscriminatedUnion<"success", [z.ZodObject<{
531
528
  success: z.ZodLiteral<true>;
532
- code: z.ZodOptional<z.ZodString>;
529
+ code: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodLiteral<"session_assigned_to_human_agent">]>>;
533
530
  options: z.ZodOptional<z.ZodObject<{
534
531
  type: z.ZodLiteral<"options">;
535
532
  value: z.ZodArray<z.ZodString, "many">;