@opencx/widget 2.2.0 → 2.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.
Files changed (68) hide show
  1. package/README.md +30 -105
  2. package/dist/basic.cjs +3 -3
  3. package/dist/basic.cjs.map +1 -1
  4. package/dist/basic.js +10 -9
  5. package/dist/basic.js.map +1 -1
  6. package/dist/core/client/api.d.ts +21 -7
  7. package/dist/core/client/chat.d.ts +62 -0
  8. package/dist/core/client/chat.test.d.ts +1 -0
  9. package/dist/core/client/client.d.ts +6 -36
  10. package/dist/core/client/config.d.ts +35 -0
  11. package/dist/core/client/config.test.d.ts +1 -0
  12. package/dist/core/client/contact.d.ts +32 -0
  13. package/dist/core/client/contact.test.d.ts +1 -0
  14. package/dist/core/client/index.d.ts +3 -1
  15. package/dist/core/index.d.ts +6 -1
  16. package/dist/core/platform/index.d.ts +1 -10
  17. package/dist/core/types/index.d.ts +24 -2
  18. package/dist/core/types/pub-sub.d.ts +19 -82
  19. package/dist/core/types/schemas-v2.d.ts +687 -0
  20. package/dist/core/utils/create-fetch.d.ts +24 -0
  21. package/dist/index-B7LNpE9i.cjs +18 -0
  22. package/dist/index-B7LNpE9i.cjs.map +1 -0
  23. package/dist/{index-XkXeMM77.js → index-BQfnx4Er.js} +1182 -1310
  24. package/dist/index-BQfnx4Er.js.map +1 -0
  25. package/dist/index.cjs +1 -1
  26. package/dist/index.cjs.map +1 -1
  27. package/dist/index.js +376 -370
  28. package/dist/index.js.map +1 -1
  29. package/dist/react-bindings/context/ChatContext.d.ts +130 -0
  30. package/dist/react-bindings/hooks/useContact.d.ts +9 -0
  31. package/dist/react-bindings/hooks/usePubsub.d.ts +3 -0
  32. package/dist/react-bindings/hooks/useSendMessage.d.ts +6 -0
  33. package/dist/react-bindings/index.d.ts +3 -0
  34. package/dist/react-lib/hooks/useAbstractChat.d.ts +42 -45
  35. package/dist/react-lib/hooks/useAxiosInstance.d.ts +37 -0
  36. package/dist/react-lib/hooks/useUploadFiles.d.ts +1 -1
  37. package/dist/react-lib/providers/ChatProvider.d.ts +54 -46
  38. package/dist/react-lib/types/index.d.ts +1 -0
  39. package/dist/react.cjs +1 -1
  40. package/dist/react.cjs.map +1 -1
  41. package/dist/react.js +2 -2
  42. package/dist/react.js.map +1 -1
  43. package/dist/schemas-BBAV6Sd_.js +122 -0
  44. package/dist/schemas-BBAV6Sd_.js.map +1 -0
  45. package/dist/schemas-wky4fpbc.cjs +2 -0
  46. package/dist/schemas-wky4fpbc.cjs.map +1 -0
  47. package/dist/src/components/dialog.d.ts +2 -2
  48. package/dist/style.css +1 -1
  49. package/dist-embed/script.js +106 -106
  50. package/dist-embed/script.js.map +1 -1
  51. package/package.json +1 -3
  52. package/dist/core/managers/chathistory-manager.d.ts +0 -61
  53. package/dist/core/managers/session-manager.d.ts +0 -35
  54. package/dist/core/transport/http.transport.d.ts +0 -17
  55. package/dist/core/transport/transport.d.ts +0 -25
  56. package/dist/core/types/transport.d.ts +0 -31
  57. package/dist/history-to-widget-messages-CP0hKjhv.js +0 -195
  58. package/dist/history-to-widget-messages-CP0hKjhv.js.map +0 -1
  59. package/dist/history-to-widget-messages-CdbZolRN.cjs +0 -2
  60. package/dist/history-to-widget-messages-CdbZolRN.cjs.map +0 -1
  61. package/dist/index-XkXeMM77.js.map +0 -1
  62. package/dist/index-xb7Pg6AD.cjs +0 -18
  63. package/dist/index-xb7Pg6AD.cjs.map +0 -1
  64. package/dist/react-lib/hooks/handle-socket-messages.d.ts +0 -27
  65. package/dist/react-lib/hooks/socket.d.ts +0 -11
  66. package/dist/react-lib/hooks/socketState.d.ts +0 -2
  67. package/dist/react-lib/hooks/useBaseClient.d.ts +0 -2
  68. package/dist/react-lib/providers/ClientProvider.d.ts +0 -7
@@ -1,15 +1,29 @@
1
- import { ChatSessionType } from '../types/schemas';
2
- import { MessageData } from '../types/transport';
1
+ import { HandleContactMessageOutputSchema, WidgetHistorySchema, WidgetPreludeSchema, WidgetSessionSchema } from '../types/schemas-v2';
2
+ import { CoreOptions, SendMessageInput, ConsumerType } from '../types';
3
3
  export interface ApiCallerOptions {
4
4
  apiUrl: string;
5
5
  token: string;
6
+ coreOptions: CoreOptions;
6
7
  }
7
8
  export declare class ApiCaller {
9
+ #private;
8
10
  private readonly options;
9
11
  constructor(options: ApiCallerOptions);
10
- createSession(): Promise<ChatSessionType>;
11
- fetchSession(sessionId: string): Promise<ChatSessionType>;
12
- ping(): Promise<void>;
13
- getMessages(sessionId?: string): Promise<MessageData[]>;
14
- sendMessage(message: MessageData): Promise<void>;
12
+ me(): Promise<{
13
+ contactId: string;
14
+ contactName: string;
15
+ }>;
16
+ widgetPrelude(): Promise<WidgetPreludeSchema>;
17
+ handleMessage(message: SendMessageInput): Promise<HandleContactMessageOutputSchema>;
18
+ getSessionHistory(sessionId: string, lastMessageTimestamp?: string): Promise<WidgetHistorySchema[]>;
19
+ createSession(): Promise<WidgetSessionSchema>;
20
+ getSession(sessionId: string): Promise<WidgetSessionSchema>;
21
+ createContact(user: {
22
+ external_id?: string;
23
+ name?: string;
24
+ email?: string;
25
+ phone?: string;
26
+ customData?: Record<string, string>;
27
+ avatarUrl?: string;
28
+ }): Promise<ConsumerType>;
15
29
  }
@@ -0,0 +1,62 @@
1
+ import { PubSub } from '../types/pub-sub';
2
+ import { MessageType } from '../types';
3
+ import { ApiCaller } from './api';
4
+ type ChatState = {
5
+ lastUpdated: number | null;
6
+ messages: MessageType[];
7
+ keyboard: {
8
+ options: string[];
9
+ } | null;
10
+ };
11
+ type ChatOptions = {
12
+ api: ApiCaller;
13
+ persistSession?: boolean;
14
+ useSoundEffects?: boolean;
15
+ onSessionDestroy?: () => void;
16
+ };
17
+ export declare function createChat(options: ChatOptions): {
18
+ chatState: PubSub<ChatState>;
19
+ sessionState: PubSub<{
20
+ id: string;
21
+ channel: string;
22
+ assignee: {
23
+ name: string | null;
24
+ kind: "unknown" | "ai" | "none" | "human";
25
+ };
26
+ createdAt: Date;
27
+ updatedAt: Date;
28
+ isHandedOff: boolean;
29
+ isOpened: boolean;
30
+ } | 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>;
47
+ createSession: () => Promise<{
48
+ id: string;
49
+ channel: string;
50
+ assignee: {
51
+ name: string | null;
52
+ kind: "unknown" | "ai" | "none" | "human";
53
+ };
54
+ createdAt: Date;
55
+ updatedAt: Date;
56
+ isHandedOff: boolean;
57
+ isOpened: boolean;
58
+ }>;
59
+ clearSession: () => Promise<void>;
60
+ cleanup: () => void;
61
+ };
62
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -1,39 +1,9 @@
1
1
  import { CoreOptions } from '../types';
2
- import { Subscribable } from '../types/pub-sub';
3
- import { Platform } from '../platform';
4
- import { MessagingTransport } from '../transport/transport';
5
- import { MessageType, SendMessageInput } from '../types/messages';
6
- export declare class ApiClient extends Subscribable {
7
- private readonly platform;
8
- private messagingTransport;
9
- private readonly options;
2
+ export type RequiredOptions = Required<Omit<CoreOptions, 'contactToken'>> & {
3
+ contactToken: string | undefined | null;
4
+ };
5
+ export declare class ApiClient {
6
+ private readonly _options;
10
7
  private readonly api;
11
- private readonly session;
12
- private readonly events;
13
- private messages;
14
- private keyboard;
15
- private pollingInterval?;
16
- private heartbeatInterval?;
17
- constructor(options: CoreOptions, platform?: Platform);
18
- private startMessagePolling;
19
- private startHeartbeat;
20
- private appendMessages;
21
- private handleIncomingMessage;
22
- private refreshSession;
23
- private initializeTransport;
24
- sendMessage({ content, user, ...data }: SendMessageInput): Promise<void>;
25
- setKeyboard(options: string[] | null): void;
26
- handleKeyboardOption(option: string): void;
27
- getMessages(): readonly MessageType[];
28
- clearChat(): void;
29
- connect(): Promise<void>;
30
- disconnect(): Promise<void>;
31
- setTransport(transport: MessagingTransport): void;
32
- get state(): {
33
- connected: boolean;
34
- session: import('../managers/session-manager').ChatSessionWithStatus | null;
35
- messages: MessageType[];
36
- keyboard: string[] | null;
37
- };
38
- protected cleanup(): void;
8
+ constructor(options: CoreOptions);
39
9
  }
@@ -0,0 +1,35 @@
1
+ import { CoreOptions } from '../types';
2
+ type NormalizedConfig = Required<Omit<CoreOptions, 'contactToken'>> & {
3
+ contactToken: string | undefined | null;
4
+ soundEffectFiles: {
5
+ messageArrived: string;
6
+ };
7
+ theme: {
8
+ headerStyle: "compact" | "basic";
9
+ primaryColor: string;
10
+ triggerOffset: string;
11
+ };
12
+ settings: {
13
+ persistSession: boolean;
14
+ useSoundEffects: boolean;
15
+ };
16
+ };
17
+ type ConfigInstance = {
18
+ getConfig: () => NormalizedConfig;
19
+ getApiConfig: () => {
20
+ apiUrl: string;
21
+ token: string;
22
+ headers: Record<string, string>;
23
+ queryParams: Record<string, string>;
24
+ pathParams: Record<string, string>;
25
+ };
26
+ getBotConfig: () => NormalizedConfig['bot'];
27
+ getThemeConfig: () => NormalizedConfig['theme'];
28
+ getSettings: () => NormalizedConfig['settings'];
29
+ getSoundEffects: () => NormalizedConfig['soundEffectFiles'];
30
+ getUser: () => NormalizedConfig['user'];
31
+ getLanguage: () => string;
32
+ getDebugMode: () => boolean;
33
+ };
34
+ export declare function createConfig(options: CoreOptions): ConfigInstance;
35
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ import { PubSub } from '../types/pub-sub';
2
+ import { ApiCaller } from './api';
3
+ import { Platform } from '../platform';
4
+ type ContactOptions = {
5
+ api: ApiCaller;
6
+ botToken: string;
7
+ 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
+ };
17
+ };
18
+ export declare function createContact(options: ContactOptions): {
19
+ shouldCollectData: () => {
20
+ should: boolean;
21
+ reason?: string;
22
+ };
23
+ cleanup: () => void;
24
+ contactState: PubSub<{
25
+ email: string | null;
26
+ id: string;
27
+ name: string | null;
28
+ avatar_url: string | null;
29
+ created_at: string;
30
+ } | null>;
31
+ };
32
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -1 +1,3 @@
1
- export * from './client';
1
+ export { createChat } from './chat';
2
+ export { createContact } from './contact';
3
+ export { ApiCaller, type ApiCallerOptions } from './api';
@@ -1,3 +1,8 @@
1
1
  export * from './client';
2
2
  export * from './errors';
3
- export type { CoreOptions } from './types';
3
+ export * from './types';
4
+ export * from './platform';
5
+ export { createChat } from './client/chat';
6
+ export { createContact } from './client/contact';
7
+ export { createConfig } from './client/config';
8
+ export { ApiCaller } from './client/api';
@@ -8,13 +8,4 @@ export interface Platform {
8
8
  toISOString(date: number): string;
9
9
  };
10
10
  }
11
- export declare class DefaultPlatform implements Platform {
12
- env: {
13
- platform: string;
14
- };
15
- date: {
16
- now: () => number;
17
- toISOString: (date: number) => string;
18
- };
19
- private detectPlatform;
20
- }
11
+ export declare function createDefaultPlatform(): Platform;
@@ -1,7 +1,7 @@
1
+ import { ChatAttachmentType } from './schemas';
1
2
  export * from './messages';
2
3
  export * from './schemas';
3
4
  export * from './pub-sub';
4
- export * from './transport';
5
5
  export * from './helpers';
6
6
  export interface User {
7
7
  external_id?: string;
@@ -11,10 +11,19 @@ export interface User {
11
11
  customData?: Record<string, string>;
12
12
  avatarUrl?: string;
13
13
  }
14
+ export interface SendMessageInput extends Record<string, unknown> {
15
+ content: {
16
+ text: string;
17
+ };
18
+ attachments?: Array<ChatAttachmentType>;
19
+ id?: string;
20
+ language?: string;
21
+ user?: User;
22
+ }
14
23
  export interface CoreOptions {
15
24
  token: string;
16
25
  apiUrl?: string;
17
- transport?: "socket" | "http";
26
+ transport?: 'socket' | 'http';
18
27
  socketUrl?: string;
19
28
  headers?: Record<string, string>;
20
29
  queryParams?: Record<string, string>;
@@ -22,6 +31,7 @@ export interface CoreOptions {
22
31
  debug?: boolean;
23
32
  language?: string;
24
33
  user?: User;
34
+ contactToken?: string;
25
35
  bot?: {
26
36
  name?: string;
27
37
  avatarUrl?: string;
@@ -29,4 +39,16 @@ export interface CoreOptions {
29
39
  is_ai?: boolean;
30
40
  };
31
41
  pollingInterval?: number;
42
+ soundEffectFiles?: {
43
+ messageArrived?: string;
44
+ };
45
+ theme?: {
46
+ headerStyle?: "compact" | "basic";
47
+ primaryColor?: string;
48
+ triggerOffset?: string;
49
+ };
50
+ settings?: {
51
+ persistSession?: boolean;
52
+ useSoundEffects?: boolean;
53
+ };
32
54
  }
@@ -1,94 +1,31 @@
1
- /**
2
- * Base interface for event maps. All event maps must extend this.
3
- * @example
4
- * ```typescript
5
- * interface MyEvents extends EventMap {
6
- * "my:event": string
7
- * "my:other:event": { data: number }
8
- * }
9
- * ```
10
- */
11
- export interface EventMap {
12
- [key: string]: any;
13
- }
14
- /**
15
- * Event subscriber function type
16
- */
17
1
  export type Subscriber<T> = (data: T) => void;
18
- /**
19
- * Generic PubSub class that can be typed with specific event maps.
20
- * Each component should create its own instance with its specific events.
21
- *
22
- * @example
23
- * ```typescript
24
- * interface MyEvents extends EventMap {
25
- * "my:event": string
26
- * }
27
- *
28
- * const pubSub = new PubSub<MyEvents>();
29
- *
30
- * pubSub.subscribe("my:event", (data) => {
31
- * console.log(data.toUpperCase()); // Type-safe: data is string
32
- * });
33
- * ```
34
- */
35
- export declare class PubSub<T extends EventMap> {
2
+ export declare class PubSub<S> {
3
+ #private;
36
4
  private subscribers;
5
+ private initialState;
6
+ constructor(state: S);
37
7
  /**
38
- * Subscribe to an event
39
- * @param event Event name to subscribe to
40
- * @param callback Function to call when event is published
8
+ * Subscribe to state changes
9
+ * @param callback Function to call when state changes
41
10
  * @returns Unsubscribe function
42
11
  */
43
- subscribe<K extends keyof T>(event: K, callback: Subscriber<T[K]>): () => void;
44
- /**
45
- * Publish an event with data
46
- * @param event Event name to publish
47
- * @param data Data to send with the event
48
- */
49
- publish<K extends keyof T>(event: K, data: T[K]): void;
50
- /**
51
- * Clear all subscriptions
52
- */
53
- clear(): void;
54
- }
55
- /**
56
- * Abstract base class for components that need subscription management.
57
- * Provides common functionality for managing subscriptions and cleanup.
58
- *
59
- * @example
60
- * ```typescript
61
- * class MyComponent extends Subscribable {
62
- * constructor() {
63
- * super();
64
- * // Add unsubscribers
65
- * this.addUnsubscriber(() => {
66
- * console.log('Cleanup');
67
- * });
68
- * }
69
- *
70
- * protected cleanup(): void {
71
- * // Component-specific cleanup
72
- * }
73
- * }
74
- * ```
75
- */
76
- export declare abstract class Subscribable {
77
- protected unsubscribers: (() => void)[];
12
+ subscribe(callback: Subscriber<S>): () => void;
78
13
  /**
79
- * Add an unsubscribe function to be called during cleanup
14
+ * Get the current state
80
15
  */
81
- protected addUnsubscriber(unsubscribe: () => void): void;
16
+ getState(): S;
82
17
  /**
83
- * Dispose of all subscriptions and perform cleanup
18
+ * Set the state and notify subscribers if the state changes
19
+ * @param newState The new state to set
84
20
  */
85
- dispose(): void;
21
+ setState(newState: S): void;
22
+ setStatePartial(_s: Partial<S>): void;
86
23
  /**
87
- * Component-specific cleanup logic
24
+ * Clear all subscriptions
88
25
  */
89
- protected abstract cleanup(): void;
26
+ clear(): void;
27
+ reset(): void;
28
+ getSnapshot(): S;
29
+ get state(): S;
90
30
  }
91
- /**
92
- * Helper function to create a strongly typed PubSub instance
93
- */
94
- export declare function createPubSub<T extends EventMap>(): PubSub<T>;
31
+ export declare function createPubSub<S>(state: S): PubSub<S>;