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.
- package/dist/248.chat-widget.js +1 -0
- package/dist/538.chat-widget.js +1 -0
- package/dist/751.chat-widget.js +1 -0
- package/dist/chat-widget.js +1 -1
- package/dist/chat-widget.js.LICENSE.txt +2 -0
- package/lib/commonjs/api.js +2 -5
- package/lib/commonjs/api.js.map +1 -1
- package/lib/commonjs/components/ChatHeader.js +15 -2
- package/lib/commonjs/components/ChatHeader.js.map +1 -1
- package/lib/commonjs/components/ChatWidget.js +49 -24
- package/lib/commonjs/components/ChatWidget.js.map +1 -1
- package/lib/commonjs/components/MessageBubble.js +33 -9
- package/lib/commonjs/components/MessageBubble.js.map +1 -1
- package/lib/commonjs/components/ProductsList.js +2 -2
- package/lib/commonjs/components/ProductsList.js.map +1 -1
- package/lib/commonjs/components/utils.js +46 -1
- package/lib/commonjs/components/utils.js.map +1 -1
- package/lib/commonjs/contexts/AnalyticsClientContext.js +19 -0
- package/lib/commonjs/contexts/AnalyticsClientContext.js.map +1 -0
- package/lib/commonjs/contexts/SegmentClientContext.js +19 -0
- package/lib/commonjs/contexts/SegmentClientContext.js.map +1 -0
- package/lib/commonjs/hooks/useAnalytics.js +158 -0
- package/lib/commonjs/hooks/useAnalytics.js.map +1 -0
- package/lib/commonjs/index.web.js +29 -3
- package/lib/commonjs/index.web.js.map +1 -1
- package/lib/commonjs/storage.js +5 -5
- package/lib/commonjs/storage.js.map +1 -1
- package/lib/module/api.js +2 -5
- package/lib/module/api.js.map +1 -1
- package/lib/module/components/ChatHeader.js +15 -2
- package/lib/module/components/ChatHeader.js.map +1 -1
- package/lib/module/components/ChatWidget.js +50 -25
- package/lib/module/components/ChatWidget.js.map +1 -1
- package/lib/module/components/MessageBubble.js +34 -10
- package/lib/module/components/MessageBubble.js.map +1 -1
- package/lib/module/components/ProductsList.js +2 -2
- package/lib/module/components/ProductsList.js.map +1 -1
- package/lib/module/components/utils.js +42 -0
- package/lib/module/components/utils.js.map +1 -1
- package/lib/module/contexts/AnalyticsClientContext.js +10 -0
- package/lib/module/contexts/AnalyticsClientContext.js.map +1 -0
- package/lib/module/contexts/SegmentClientContext.js +10 -0
- package/lib/module/contexts/SegmentClientContext.js.map +1 -0
- package/lib/module/hooks/useAnalytics.js +146 -0
- package/lib/module/hooks/useAnalytics.js.map +1 -0
- package/lib/module/index.native.js +5 -5
- package/lib/module/index.web.js +30 -4
- package/lib/module/index.web.js.map +1 -1
- package/lib/module/storage.js +6 -6
- package/lib/module/storage.js.map +1 -1
- package/lib/typescript/api.d.ts +1 -1
- package/lib/typescript/api.d.ts.map +1 -1
- package/lib/typescript/components/ChatHeader.d.ts.map +1 -1
- package/lib/typescript/components/ChatWidget.d.ts.map +1 -1
- package/lib/typescript/components/MessageBubble.d.ts +2 -0
- package/lib/typescript/components/MessageBubble.d.ts.map +1 -1
- package/lib/typescript/components/ProductsList.d.ts.map +1 -1
- package/lib/typescript/components/utils.d.ts +23 -0
- package/lib/typescript/components/utils.d.ts.map +1 -1
- package/lib/typescript/contexts/AnalyticsClientContext.d.ts +9 -0
- package/lib/typescript/contexts/AnalyticsClientContext.d.ts.map +1 -0
- package/lib/typescript/contexts/SegmentClientContext.d.ts +9 -0
- package/lib/typescript/contexts/SegmentClientContext.d.ts.map +1 -0
- package/lib/typescript/hooks/useAnalytics.d.ts +36 -0
- package/lib/typescript/hooks/useAnalytics.d.ts.map +1 -0
- package/lib/typescript/index.native.d.ts +5 -5
- package/lib/typescript/index.web.d.ts +1 -1
- package/lib/typescript/index.web.d.ts.map +1 -1
- package/lib/typescript/storage.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +1 -1
- package/lib/typescript/types.d.ts.map +1 -1
- package/package.json +13 -3
- package/src/api.ts +22 -11
- package/src/components/ChatHeader.tsx +13 -2
- package/src/components/ChatWidget.tsx +53 -28
- package/src/components/MessageBubble.tsx +52 -10
- package/src/components/ProductsList.tsx +8 -6
- package/src/components/utils.ts +60 -1
- package/src/contexts/AnalyticsClientContext.tsx +20 -0
- package/src/contexts/SegmentClientContext.tsx +20 -0
- package/src/hooks/useAnalytics.tsx +176 -0
- package/src/index.native.tsx +5 -5
- package/src/index.web.tsx +36 -3
- package/src/storage.ts +16 -13
- 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
|
-
|
|
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
|
|
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 !==
|
|
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 ===
|
|
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 ===
|
|
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
|
-
|
|
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>(
|
|
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 {
|