vdb-ai-chat 1.0.0 → 1.0.1

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 (85) hide show
  1. package/dist/248.chat-widget.js +1 -0
  2. package/dist/538.chat-widget.js +1 -0
  3. package/dist/751.chat-widget.js +1 -0
  4. package/dist/chat-widget.js +1 -1
  5. package/dist/chat-widget.js.LICENSE.txt +2 -0
  6. package/lib/commonjs/api.js +2 -5
  7. package/lib/commonjs/api.js.map +1 -1
  8. package/lib/commonjs/components/ChatHeader.js +15 -2
  9. package/lib/commonjs/components/ChatHeader.js.map +1 -1
  10. package/lib/commonjs/components/ChatWidget.js +49 -24
  11. package/lib/commonjs/components/ChatWidget.js.map +1 -1
  12. package/lib/commonjs/components/MessageBubble.js +33 -9
  13. package/lib/commonjs/components/MessageBubble.js.map +1 -1
  14. package/lib/commonjs/components/ProductsList.js +2 -2
  15. package/lib/commonjs/components/ProductsList.js.map +1 -1
  16. package/lib/commonjs/components/utils.js +46 -1
  17. package/lib/commonjs/components/utils.js.map +1 -1
  18. package/lib/commonjs/contexts/AnalyticsClientContext.js +19 -0
  19. package/lib/commonjs/contexts/AnalyticsClientContext.js.map +1 -0
  20. package/lib/commonjs/contexts/SegmentClientContext.js +19 -0
  21. package/lib/commonjs/contexts/SegmentClientContext.js.map +1 -0
  22. package/lib/commonjs/hooks/useAnalytics.js +158 -0
  23. package/lib/commonjs/hooks/useAnalytics.js.map +1 -0
  24. package/lib/commonjs/index.web.js +29 -3
  25. package/lib/commonjs/index.web.js.map +1 -1
  26. package/lib/commonjs/storage.js +5 -5
  27. package/lib/commonjs/storage.js.map +1 -1
  28. package/lib/module/api.js +2 -5
  29. package/lib/module/api.js.map +1 -1
  30. package/lib/module/components/ChatHeader.js +15 -2
  31. package/lib/module/components/ChatHeader.js.map +1 -1
  32. package/lib/module/components/ChatWidget.js +50 -25
  33. package/lib/module/components/ChatWidget.js.map +1 -1
  34. package/lib/module/components/MessageBubble.js +34 -10
  35. package/lib/module/components/MessageBubble.js.map +1 -1
  36. package/lib/module/components/ProductsList.js +2 -2
  37. package/lib/module/components/ProductsList.js.map +1 -1
  38. package/lib/module/components/utils.js +42 -0
  39. package/lib/module/components/utils.js.map +1 -1
  40. package/lib/module/contexts/AnalyticsClientContext.js +10 -0
  41. package/lib/module/contexts/AnalyticsClientContext.js.map +1 -0
  42. package/lib/module/contexts/SegmentClientContext.js +10 -0
  43. package/lib/module/contexts/SegmentClientContext.js.map +1 -0
  44. package/lib/module/hooks/useAnalytics.js +146 -0
  45. package/lib/module/hooks/useAnalytics.js.map +1 -0
  46. package/lib/module/index.native.js +5 -5
  47. package/lib/module/index.web.js +30 -4
  48. package/lib/module/index.web.js.map +1 -1
  49. package/lib/module/storage.js +6 -6
  50. package/lib/module/storage.js.map +1 -1
  51. package/lib/typescript/api.d.ts +1 -1
  52. package/lib/typescript/api.d.ts.map +1 -1
  53. package/lib/typescript/components/ChatHeader.d.ts.map +1 -1
  54. package/lib/typescript/components/ChatWidget.d.ts.map +1 -1
  55. package/lib/typescript/components/MessageBubble.d.ts +2 -0
  56. package/lib/typescript/components/MessageBubble.d.ts.map +1 -1
  57. package/lib/typescript/components/ProductsList.d.ts.map +1 -1
  58. package/lib/typescript/components/utils.d.ts +23 -0
  59. package/lib/typescript/components/utils.d.ts.map +1 -1
  60. package/lib/typescript/contexts/AnalyticsClientContext.d.ts +9 -0
  61. package/lib/typescript/contexts/AnalyticsClientContext.d.ts.map +1 -0
  62. package/lib/typescript/contexts/SegmentClientContext.d.ts +9 -0
  63. package/lib/typescript/contexts/SegmentClientContext.d.ts.map +1 -0
  64. package/lib/typescript/hooks/useAnalytics.d.ts +36 -0
  65. package/lib/typescript/hooks/useAnalytics.d.ts.map +1 -0
  66. package/lib/typescript/index.native.d.ts +5 -5
  67. package/lib/typescript/index.web.d.ts +1 -1
  68. package/lib/typescript/index.web.d.ts.map +1 -1
  69. package/lib/typescript/storage.d.ts.map +1 -1
  70. package/lib/typescript/types.d.ts +1 -1
  71. package/lib/typescript/types.d.ts.map +1 -1
  72. package/package.json +13 -3
  73. package/src/api.ts +22 -11
  74. package/src/components/ChatHeader.tsx +13 -2
  75. package/src/components/ChatWidget.tsx +53 -28
  76. package/src/components/MessageBubble.tsx +52 -10
  77. package/src/components/ProductsList.tsx +8 -6
  78. package/src/components/utils.ts +60 -1
  79. package/src/contexts/AnalyticsClientContext.tsx +20 -0
  80. package/src/contexts/SegmentClientContext.tsx +20 -0
  81. package/src/hooks/useAnalytics.tsx +176 -0
  82. package/src/index.native.tsx +5 -5
  83. package/src/index.web.tsx +36 -3
  84. package/src/storage.ts +16 -13
  85. package/src/types.ts +1 -1
package/src/index.web.tsx CHANGED
@@ -1,18 +1,44 @@
1
1
  import * as React from "react";
2
2
  import { createRoot } from "react-dom/client";
3
- import { View, StyleSheet } from "react-native";
3
+ import { View, StyleSheet, Platform } from "react-native";
4
4
  import { ChatWidget } from "./components/ChatWidget";
5
5
  import type { ChatTheme } from "./types";
6
+ import { Analytics } from "@segment/analytics-next";
7
+ import { AnalyticsClientProvider } from "./contexts/AnalyticsClientContext";
6
8
 
7
9
  export function renderChatApp(
8
10
  domElement: HTMLElement,
9
11
  apiUrl: string,
10
12
  theme?: Partial<ChatTheme>,
11
13
  onClose?: () => void,
12
- onClearChat?: () => void
14
+ onClearChat?: () => void,
15
+ segmentWriteKey?: string,
13
16
  ) {
14
17
  const root = createRoot(domElement);
15
- root.render(
18
+ let analyticsClient: Analytics | undefined;
19
+ if (Platform.OS === "web" && segmentWriteKey) {
20
+ try {
21
+ const client = new Analytics({ writeKey: segmentWriteKey });
22
+
23
+ analyticsClient = client;
24
+ (window as any).analytics = client;
25
+ client
26
+ .track("AI Chat: Widget Loaded", {
27
+ userAgent:
28
+ typeof navigator !== "undefined" ? navigator.userAgent : "",
29
+ })
30
+ .then?.(() => {
31
+ // eslint-disable-next-line no-console
32
+ console.log("Segment: Widget Loaded event sent");
33
+ })
34
+ .catch((e: unknown) => {
35
+ console.warn("Segment track failed", e);
36
+ });
37
+ } catch (e) {
38
+ console.warn("Segment analytics load failed", e);
39
+ }
40
+ }
41
+ const content = (
16
42
  <View style={styles.root}>
17
43
  <ChatWidget
18
44
  apiUrl={apiUrl}
@@ -22,6 +48,13 @@ export function renderChatApp(
22
48
  />
23
49
  </View>
24
50
  );
51
+ const contentWithAnalytics = analyticsClient ? (
52
+ <AnalyticsClientProvider client={analyticsClient}>{content}</AnalyticsClientProvider>
53
+ ) : (
54
+ content
55
+ );
56
+
57
+ root.render(contentWithAnalytics);
25
58
  }
26
59
 
27
60
  const styles = StyleSheet.create({
package/src/storage.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Platform } from 'react-native';
1
+ import { Platform } from "react-native";
2
2
 
3
3
  // Storage interface that works across platforms
4
4
  interface StorageInterface {
@@ -9,7 +9,7 @@ interface StorageInterface {
9
9
 
10
10
  // Helper to safely get localStorage (only available on web)
11
11
  function getLocalStorage(): Storage | null {
12
- if (typeof window !== 'undefined' && window.localStorage) {
12
+ if (typeof window !== "undefined" && window.localStorage) {
13
13
  return window.localStorage;
14
14
  }
15
15
  return null;
@@ -54,7 +54,7 @@ let hasWarnedAboutStorage = false;
54
54
  /**
55
55
  * Initialize storage with AsyncStorage for React Native.
56
56
  * Call this once at app startup before using ChatWidget.
57
- *
57
+ *
58
58
  * @example
59
59
  * import AsyncStorage from '@react-native-async-storage/async-storage';
60
60
  * import { initStorage } from 'vdb-ai-chat';
@@ -69,7 +69,7 @@ export function initStorage(asyncStorage: StorageInterface): void {
69
69
  * Check if storage is properly initialized for the current platform
70
70
  */
71
71
  export function isStorageInitialized(): boolean {
72
- if (Platform.OS === 'web') {
72
+ if (Platform.OS === "web") {
73
73
  return getLocalStorage() !== null;
74
74
  }
75
75
  return nativeStorage !== null;
@@ -84,21 +84,21 @@ const noopStorage: StorageInterface = {
84
84
 
85
85
  // Get the appropriate storage based on platform
86
86
  function getStorage(): StorageInterface {
87
- if (Platform.OS === 'web') {
87
+ if (Platform.OS === "web") {
88
88
  return webStorage;
89
89
  }
90
-
90
+
91
91
  if (!nativeStorage) {
92
92
  // Only warn once to avoid console spam
93
93
  if (!hasWarnedAboutStorage) {
94
94
  console.warn(
95
- '[vdb-ai-chat] AsyncStorage not initialized. Call initStorage(AsyncStorage) at app startup.'
95
+ "[vdb-ai-chat] AsyncStorage not initialized. Call initStorage(AsyncStorage) at app startup."
96
96
  );
97
97
  hasWarnedAboutStorage = true;
98
98
  }
99
99
  return noopStorage;
100
100
  }
101
-
101
+
102
102
  return nativeStorage;
103
103
  }
104
104
 
@@ -107,17 +107,20 @@ export const Storage = {
107
107
  getItem: async (key: string): Promise<string | null> => {
108
108
  return getStorage().getItem(key);
109
109
  },
110
-
110
+
111
111
  setItem: async (key: string, value: string): Promise<void> => {
112
112
  return getStorage().setItem(key, value);
113
113
  },
114
-
114
+
115
115
  removeItem: async (key: string): Promise<void> => {
116
116
  return getStorage().removeItem(key);
117
117
  },
118
-
118
+
119
119
  // Convenience method to get and parse JSON
120
- getJSON: async <T = any>(key: string, defaultValue: T | null = null): Promise<T | null> => {
120
+ getJSON: async <T = any>(
121
+ key: string,
122
+ defaultValue: T | null = null
123
+ ): Promise<T | null> => {
121
124
  try {
122
125
  const value = await getStorage().getItem(key);
123
126
  if (value) {
@@ -128,7 +131,7 @@ export const Storage = {
128
131
  return defaultValue;
129
132
  }
130
133
  },
131
-
134
+
132
135
  // Convenience method to stringify and set JSON
133
136
  setJSON: async (key: string, value: any): Promise<void> => {
134
137
  try {
package/src/types.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  export type Role = "user" | "assistant";
2
2
 
3
3
  export interface ChatMessage {
4
- agent_response?: any;
5
4
  id: string;
6
5
  role: Role;
7
6
  text: string;
@@ -10,6 +9,7 @@ export interface ChatMessage {
10
9
  suggestions?: string[];
11
10
  // "0" unset, "1" like, "2" dislike
12
11
  reaction?: "0" | "1" | "2";
12
+ search_payload?: Record<string, any>;
13
13
  }
14
14
 
15
15
  export interface ChatTheme {