@rohan_p31/ai-chat-sdk 1.0.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.
- package/README.md +75 -0
- package/lib/components/AIChat.d.ts +4 -0
- package/lib/components/AIChat.d.ts.map +1 -0
- package/lib/components/AIChat.js +117 -0
- package/lib/components/AIChat.js.map +1 -0
- package/lib/components/ChatInput.d.ts +11 -0
- package/lib/components/ChatInput.d.ts.map +1 -0
- package/lib/components/ChatInput.js +109 -0
- package/lib/components/ChatInput.js.map +1 -0
- package/lib/components/MessageBubble.d.ts +13 -0
- package/lib/components/MessageBubble.d.ts.map +1 -0
- package/lib/components/MessageBubble.js +107 -0
- package/lib/components/MessageBubble.js.map +1 -0
- package/lib/hooks/useChat.d.ts +11 -0
- package/lib/hooks/useChat.d.ts.map +1 -0
- package/lib/hooks/useChat.js +66 -0
- package/lib/hooks/useChat.js.map +1 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +19 -0
- package/lib/index.js.map +1 -0
- package/lib/services/MockChatService.d.ts +7 -0
- package/lib/services/MockChatService.d.ts.map +1 -0
- package/lib/services/MockChatService.js +23 -0
- package/lib/services/MockChatService.js.map +1 -0
- package/lib/theme/colors.d.ts +20 -0
- package/lib/theme/colors.d.ts.map +1 -0
- package/lib/theme/colors.js +42 -0
- package/lib/theme/colors.js.map +1 -0
- package/lib/types/Message.d.ts +20 -0
- package/lib/types/Message.d.ts.map +1 -0
- package/lib/types/Message.js +3 -0
- package/lib/types/Message.js.map +1 -0
- package/package.json +37 -0
- package/src/components/AIChat.tsx +139 -0
- package/src/components/ChatInput.tsx +116 -0
- package/src/components/MessageBubble.tsx +151 -0
- package/src/hooks/useChat.ts +77 -0
- package/src/index.ts +8 -0
- package/src/services/MockChatService.ts +31 -0
- package/src/theme/colors.ts +63 -0
- package/src/types/Message.ts +17 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ChatTheme } from '../types/Message';
|
|
2
|
+
export interface ThemeColors {
|
|
3
|
+
background: string;
|
|
4
|
+
surface: string;
|
|
5
|
+
headerBackground: string;
|
|
6
|
+
headerText: string;
|
|
7
|
+
userBubble: string;
|
|
8
|
+
userText: string;
|
|
9
|
+
assistantBubble: string;
|
|
10
|
+
assistantText: string;
|
|
11
|
+
inputBackground: string;
|
|
12
|
+
inputText: string;
|
|
13
|
+
inputBorder: string;
|
|
14
|
+
placeholder: string;
|
|
15
|
+
typingText: string;
|
|
16
|
+
primary: string;
|
|
17
|
+
separator: string;
|
|
18
|
+
}
|
|
19
|
+
export declare function getThemeColors(theme?: ChatTheme, primaryColor?: string): ThemeColors;
|
|
20
|
+
//# sourceMappingURL=colors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"colors.d.ts","sourceRoot":"","sources":["../../src/theme/colors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD,MAAM,WAAW,WAAW;IAC1B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,gBAAgB,EAAE,MAAM,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,eAAe,EAAE,MAAM,CAAC;IACxB,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAkCD,wBAAgB,cAAc,CAC5B,KAAK,GAAE,SAAmB,EAC1B,YAAY,SAAY,GACvB,WAAW,CAOb"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getThemeColors = getThemeColors;
|
|
4
|
+
const lightBase = {
|
|
5
|
+
background: '#F5F5F5',
|
|
6
|
+
surface: '#FFFFFF',
|
|
7
|
+
headerBackground: '#FFFFFF',
|
|
8
|
+
headerText: '#1A1A1A',
|
|
9
|
+
userText: '#FFFFFF',
|
|
10
|
+
assistantBubble: '#E8E8E8',
|
|
11
|
+
assistantText: '#1A1A1A',
|
|
12
|
+
inputBackground: '#FFFFFF',
|
|
13
|
+
inputText: '#1A1A1A',
|
|
14
|
+
inputBorder: '#D0D0D0',
|
|
15
|
+
placeholder: '#8A8A8A',
|
|
16
|
+
typingText: '#6B6B6B',
|
|
17
|
+
separator: '#E0E0E0',
|
|
18
|
+
};
|
|
19
|
+
const darkBase = {
|
|
20
|
+
background: '#121212',
|
|
21
|
+
surface: '#1E1E1E',
|
|
22
|
+
headerBackground: '#1E1E1E',
|
|
23
|
+
headerText: '#F5F5F5',
|
|
24
|
+
userText: '#FFFFFF',
|
|
25
|
+
assistantBubble: '#2C2C2C',
|
|
26
|
+
assistantText: '#F0F0F0',
|
|
27
|
+
inputBackground: '#2A2A2A',
|
|
28
|
+
inputText: '#F5F5F5',
|
|
29
|
+
inputBorder: '#3A3A3A',
|
|
30
|
+
placeholder: '#9A9A9A',
|
|
31
|
+
typingText: '#A0A0A0',
|
|
32
|
+
separator: '#2A2A2A',
|
|
33
|
+
};
|
|
34
|
+
function getThemeColors(theme = 'light', primaryColor = '#6200EE') {
|
|
35
|
+
const base = theme === 'dark' ? darkBase : lightBase;
|
|
36
|
+
return {
|
|
37
|
+
...base,
|
|
38
|
+
primary: primaryColor,
|
|
39
|
+
userBubble: primaryColor,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=colors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"colors.js","sourceRoot":"","sources":["../../src/theme/colors.ts"],"names":[],"mappings":";;AAoDA,wCAUC;AA1CD,MAAM,SAAS,GAAgD;IAC7D,UAAU,EAAE,SAAS;IACrB,OAAO,EAAE,SAAS;IAClB,gBAAgB,EAAE,SAAS;IAC3B,UAAU,EAAE,SAAS;IACrB,QAAQ,EAAE,SAAS;IACnB,eAAe,EAAE,SAAS;IAC1B,aAAa,EAAE,SAAS;IACxB,eAAe,EAAE,SAAS;IAC1B,SAAS,EAAE,SAAS;IACpB,WAAW,EAAE,SAAS;IACtB,WAAW,EAAE,SAAS;IACtB,UAAU,EAAE,SAAS;IACrB,SAAS,EAAE,SAAS;CACrB,CAAC;AAEF,MAAM,QAAQ,GAAgD;IAC5D,UAAU,EAAE,SAAS;IACrB,OAAO,EAAE,SAAS;IAClB,gBAAgB,EAAE,SAAS;IAC3B,UAAU,EAAE,SAAS;IACrB,QAAQ,EAAE,SAAS;IACnB,eAAe,EAAE,SAAS;IAC1B,aAAa,EAAE,SAAS;IACxB,eAAe,EAAE,SAAS;IAC1B,SAAS,EAAE,SAAS;IACpB,WAAW,EAAE,SAAS;IACtB,WAAW,EAAE,SAAS;IACtB,UAAU,EAAE,SAAS;IACrB,SAAS,EAAE,SAAS;CACrB,CAAC;AAEF,SAAgB,cAAc,CAC5B,QAAmB,OAAO,EAC1B,YAAY,GAAG,SAAS;IAExB,MAAM,IAAI,GAAG,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;IACrD,OAAO;QACL,GAAG,IAAI;QACP,OAAO,EAAE,YAAY;QACrB,UAAU,EAAE,YAAY;KACzB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface Message {
|
|
2
|
+
id: string;
|
|
3
|
+
text: string;
|
|
4
|
+
isUser: boolean;
|
|
5
|
+
timestamp: number;
|
|
6
|
+
}
|
|
7
|
+
export type ChatTheme = 'light' | 'dark';
|
|
8
|
+
export interface AIChatProps {
|
|
9
|
+
assistantName?: string;
|
|
10
|
+
welcomeMessage?: string;
|
|
11
|
+
theme?: ChatTheme;
|
|
12
|
+
primaryColor?: string;
|
|
13
|
+
assistantAvatar?: number | {
|
|
14
|
+
uri: string;
|
|
15
|
+
};
|
|
16
|
+
userAvatar?: number | {
|
|
17
|
+
uri: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=Message.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Message.d.ts","sourceRoot":"","sources":["../../src/types/Message.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,MAAM,CAAC;AAEzC,MAAM,WAAW,WAAW;IAC1B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,GAAG;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC;IAC3C,UAAU,CAAC,EAAE,MAAM,GAAG;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC;CACvC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Message.js","sourceRoot":"","sources":["../../src/types/Message.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rohan_p31/ai-chat-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React Native AI chat UI SDK with mock streaming responses",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"react-native": "src/index.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"lib",
|
|
10
|
+
"src",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"prepare": "npm run build",
|
|
16
|
+
"clean": "rm -rf lib"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"react-native",
|
|
20
|
+
"ai",
|
|
21
|
+
"chat",
|
|
22
|
+
"sdk",
|
|
23
|
+
"streaming"
|
|
24
|
+
],
|
|
25
|
+
"author": "Innowrap Technologies",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"react": ">=18.0.0",
|
|
29
|
+
"react-native": ">=0.72.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/react": "^19.0.0",
|
|
33
|
+
"react": "^19.0.0",
|
|
34
|
+
"react-native": "^0.76.0",
|
|
35
|
+
"typescript": "^5.6.3"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import React, { useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
FlatList,
|
|
4
|
+
KeyboardAvoidingView,
|
|
5
|
+
Platform,
|
|
6
|
+
SafeAreaView,
|
|
7
|
+
StyleSheet,
|
|
8
|
+
Text,
|
|
9
|
+
View,
|
|
10
|
+
type ListRenderItem,
|
|
11
|
+
} from 'react-native';
|
|
12
|
+
import type { AIChatProps, Message } from '../types/Message';
|
|
13
|
+
import { getThemeColors } from '../theme/colors';
|
|
14
|
+
import { useChat } from '../hooks/useChat';
|
|
15
|
+
import { MessageBubble } from './MessageBubble';
|
|
16
|
+
import { ChatInput } from './ChatInput';
|
|
17
|
+
|
|
18
|
+
export function AIChat({
|
|
19
|
+
assistantName = 'AI Assistant',
|
|
20
|
+
welcomeMessage = 'Hi, how can I help?',
|
|
21
|
+
theme = 'light',
|
|
22
|
+
primaryColor = '#6200EE',
|
|
23
|
+
assistantAvatar,
|
|
24
|
+
userAvatar,
|
|
25
|
+
}: AIChatProps) {
|
|
26
|
+
const colors = useMemo(
|
|
27
|
+
() => getThemeColors(theme, primaryColor),
|
|
28
|
+
[theme, primaryColor],
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const { messages, isTyping, isStreaming, sendMessage } = useChat({
|
|
32
|
+
welcomeMessage,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const listRef = useRef<FlatList<Message>>(null);
|
|
36
|
+
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
const timer = setTimeout(() => {
|
|
39
|
+
listRef.current?.scrollToEnd({ animated: true });
|
|
40
|
+
}, 50);
|
|
41
|
+
return () => clearTimeout(timer);
|
|
42
|
+
}, [messages, isTyping]);
|
|
43
|
+
|
|
44
|
+
const renderItem: ListRenderItem<Message> = ({ item }) => {
|
|
45
|
+
if (!item.isUser && item.text.length === 0) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<MessageBubble
|
|
51
|
+
message={item}
|
|
52
|
+
colors={colors}
|
|
53
|
+
assistantAvatar={assistantAvatar}
|
|
54
|
+
userAvatar={userAvatar}
|
|
55
|
+
/>
|
|
56
|
+
);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]}>
|
|
61
|
+
<KeyboardAvoidingView
|
|
62
|
+
style={styles.flex}
|
|
63
|
+
behavior={Platform.OS === 'ios' ? 'padding' : undefined}
|
|
64
|
+
keyboardVerticalOffset={Platform.OS === 'ios' ? 8 : 0}
|
|
65
|
+
>
|
|
66
|
+
<View
|
|
67
|
+
style={[
|
|
68
|
+
styles.header,
|
|
69
|
+
{
|
|
70
|
+
backgroundColor: colors.headerBackground,
|
|
71
|
+
borderBottomColor: colors.separator,
|
|
72
|
+
},
|
|
73
|
+
]}
|
|
74
|
+
>
|
|
75
|
+
<Text style={[styles.headerTitle, { color: colors.headerText }]}>
|
|
76
|
+
{assistantName}
|
|
77
|
+
</Text>
|
|
78
|
+
</View>
|
|
79
|
+
|
|
80
|
+
<FlatList
|
|
81
|
+
ref={listRef}
|
|
82
|
+
data={messages}
|
|
83
|
+
keyExtractor={item => item.id}
|
|
84
|
+
renderItem={renderItem}
|
|
85
|
+
contentContainerStyle={styles.listContent}
|
|
86
|
+
style={[styles.flex, { backgroundColor: colors.background }]}
|
|
87
|
+
onContentSizeChange={() =>
|
|
88
|
+
listRef.current?.scrollToEnd({ animated: true })
|
|
89
|
+
}
|
|
90
|
+
keyboardShouldPersistTaps="handled"
|
|
91
|
+
/>
|
|
92
|
+
|
|
93
|
+
{isTyping && (
|
|
94
|
+
<View style={styles.typingRow}>
|
|
95
|
+
<Text style={[styles.typingText, { color: colors.typingText }]}>
|
|
96
|
+
AI is typing...
|
|
97
|
+
</Text>
|
|
98
|
+
</View>
|
|
99
|
+
)}
|
|
100
|
+
|
|
101
|
+
<ChatInput
|
|
102
|
+
colors={colors}
|
|
103
|
+
disabled={isStreaming}
|
|
104
|
+
onSend={sendMessage}
|
|
105
|
+
/>
|
|
106
|
+
</KeyboardAvoidingView>
|
|
107
|
+
</SafeAreaView>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const styles = StyleSheet.create({
|
|
112
|
+
safe: {
|
|
113
|
+
flex: 1,
|
|
114
|
+
},
|
|
115
|
+
flex: {
|
|
116
|
+
flex: 1,
|
|
117
|
+
},
|
|
118
|
+
header: {
|
|
119
|
+
paddingHorizontal: 16,
|
|
120
|
+
paddingVertical: 14,
|
|
121
|
+
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
122
|
+
},
|
|
123
|
+
headerTitle: {
|
|
124
|
+
fontSize: 17,
|
|
125
|
+
fontWeight: '700',
|
|
126
|
+
},
|
|
127
|
+
listContent: {
|
|
128
|
+
paddingVertical: 12,
|
|
129
|
+
flexGrow: 1,
|
|
130
|
+
},
|
|
131
|
+
typingRow: {
|
|
132
|
+
paddingHorizontal: 20,
|
|
133
|
+
paddingBottom: 6,
|
|
134
|
+
},
|
|
135
|
+
typingText: {
|
|
136
|
+
fontSize: 13,
|
|
137
|
+
fontStyle: 'italic',
|
|
138
|
+
},
|
|
139
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Pressable,
|
|
4
|
+
StyleSheet,
|
|
5
|
+
Text,
|
|
6
|
+
TextInput,
|
|
7
|
+
View,
|
|
8
|
+
type StyleProp,
|
|
9
|
+
type ViewStyle,
|
|
10
|
+
} from 'react-native';
|
|
11
|
+
import type { ThemeColors } from '../theme/colors';
|
|
12
|
+
|
|
13
|
+
export interface ChatInputProps {
|
|
14
|
+
colors: ThemeColors;
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
onSend: (text: string) => void;
|
|
17
|
+
style?: StyleProp<ViewStyle>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function ChatInput({
|
|
21
|
+
colors,
|
|
22
|
+
disabled = false,
|
|
23
|
+
onSend,
|
|
24
|
+
style,
|
|
25
|
+
}: ChatInputProps) {
|
|
26
|
+
const [text, setText] = useState('');
|
|
27
|
+
|
|
28
|
+
const handleSend = () => {
|
|
29
|
+
const trimmed = text.trim();
|
|
30
|
+
if (!trimmed || disabled) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
onSend(trimmed);
|
|
34
|
+
setText('');
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const canSend = text.trim().length > 0 && !disabled;
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<View
|
|
41
|
+
style={[
|
|
42
|
+
styles.container,
|
|
43
|
+
{
|
|
44
|
+
backgroundColor: colors.surface,
|
|
45
|
+
borderTopColor: colors.separator,
|
|
46
|
+
},
|
|
47
|
+
style,
|
|
48
|
+
]}
|
|
49
|
+
>
|
|
50
|
+
<TextInput
|
|
51
|
+
value={text}
|
|
52
|
+
onChangeText={setText}
|
|
53
|
+
placeholder="Type Message..."
|
|
54
|
+
placeholderTextColor={colors.placeholder}
|
|
55
|
+
editable={!disabled}
|
|
56
|
+
multiline
|
|
57
|
+
style={[
|
|
58
|
+
styles.input,
|
|
59
|
+
{
|
|
60
|
+
backgroundColor: colors.inputBackground,
|
|
61
|
+
borderColor: colors.inputBorder,
|
|
62
|
+
color: colors.inputText,
|
|
63
|
+
},
|
|
64
|
+
]}
|
|
65
|
+
onSubmitEditing={handleSend}
|
|
66
|
+
blurOnSubmit={false}
|
|
67
|
+
/>
|
|
68
|
+
<Pressable
|
|
69
|
+
onPress={handleSend}
|
|
70
|
+
disabled={!canSend}
|
|
71
|
+
style={({ pressed }) => [
|
|
72
|
+
styles.sendButton,
|
|
73
|
+
{
|
|
74
|
+
backgroundColor: canSend ? colors.primary : colors.inputBorder,
|
|
75
|
+
opacity: pressed && canSend ? 0.85 : 1,
|
|
76
|
+
},
|
|
77
|
+
]}
|
|
78
|
+
>
|
|
79
|
+
<Text style={styles.sendLabel}>Send</Text>
|
|
80
|
+
</Pressable>
|
|
81
|
+
</View>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const styles = StyleSheet.create({
|
|
86
|
+
container: {
|
|
87
|
+
flexDirection: 'row',
|
|
88
|
+
alignItems: 'flex-end',
|
|
89
|
+
paddingHorizontal: 12,
|
|
90
|
+
paddingVertical: 10,
|
|
91
|
+
borderTopWidth: StyleSheet.hairlineWidth,
|
|
92
|
+
gap: 8,
|
|
93
|
+
},
|
|
94
|
+
input: {
|
|
95
|
+
flex: 1,
|
|
96
|
+
minHeight: 40,
|
|
97
|
+
maxHeight: 120,
|
|
98
|
+
borderWidth: 1,
|
|
99
|
+
borderRadius: 20,
|
|
100
|
+
paddingHorizontal: 14,
|
|
101
|
+
paddingVertical: 10,
|
|
102
|
+
fontSize: 15,
|
|
103
|
+
},
|
|
104
|
+
sendButton: {
|
|
105
|
+
height: 40,
|
|
106
|
+
paddingHorizontal: 16,
|
|
107
|
+
borderRadius: 20,
|
|
108
|
+
alignItems: 'center',
|
|
109
|
+
justifyContent: 'center',
|
|
110
|
+
},
|
|
111
|
+
sendLabel: {
|
|
112
|
+
color: '#FFFFFF',
|
|
113
|
+
fontSize: 14,
|
|
114
|
+
fontWeight: '600',
|
|
115
|
+
},
|
|
116
|
+
});
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Image,
|
|
4
|
+
StyleSheet,
|
|
5
|
+
Text,
|
|
6
|
+
View,
|
|
7
|
+
type ImageSourcePropType,
|
|
8
|
+
type StyleProp,
|
|
9
|
+
type ViewStyle,
|
|
10
|
+
} from 'react-native';
|
|
11
|
+
import type { Message } from '../types/Message';
|
|
12
|
+
import type { ThemeColors } from '../theme/colors';
|
|
13
|
+
|
|
14
|
+
export interface MessageBubbleProps {
|
|
15
|
+
message: Message;
|
|
16
|
+
colors: ThemeColors;
|
|
17
|
+
assistantAvatar?: ImageSourcePropType;
|
|
18
|
+
userAvatar?: ImageSourcePropType;
|
|
19
|
+
style?: StyleProp<ViewStyle>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function MessageBubble({
|
|
23
|
+
message,
|
|
24
|
+
colors,
|
|
25
|
+
assistantAvatar,
|
|
26
|
+
userAvatar,
|
|
27
|
+
style,
|
|
28
|
+
}: MessageBubbleProps) {
|
|
29
|
+
const isUser = message.isUser;
|
|
30
|
+
const avatar = isUser ? userAvatar : assistantAvatar;
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<View
|
|
34
|
+
style={[
|
|
35
|
+
styles.row,
|
|
36
|
+
isUser ? styles.rowUser : styles.rowAssistant,
|
|
37
|
+
style,
|
|
38
|
+
]}
|
|
39
|
+
>
|
|
40
|
+
{!isUser && (
|
|
41
|
+
<View style={styles.avatarSlot}>
|
|
42
|
+
{avatar ? (
|
|
43
|
+
<Image source={avatar} style={styles.avatar} />
|
|
44
|
+
) : (
|
|
45
|
+
<View
|
|
46
|
+
style={[
|
|
47
|
+
styles.avatarPlaceholder,
|
|
48
|
+
{ backgroundColor: colors.assistantBubble },
|
|
49
|
+
]}
|
|
50
|
+
>
|
|
51
|
+
<Text style={[styles.avatarLetter, { color: colors.assistantText }]}>
|
|
52
|
+
AI
|
|
53
|
+
</Text>
|
|
54
|
+
</View>
|
|
55
|
+
)}
|
|
56
|
+
</View>
|
|
57
|
+
)}
|
|
58
|
+
|
|
59
|
+
<View
|
|
60
|
+
style={[
|
|
61
|
+
styles.bubble,
|
|
62
|
+
{
|
|
63
|
+
backgroundColor: isUser ? colors.userBubble : colors.assistantBubble,
|
|
64
|
+
},
|
|
65
|
+
isUser ? styles.bubbleUser : styles.bubbleAssistant,
|
|
66
|
+
]}
|
|
67
|
+
>
|
|
68
|
+
<Text
|
|
69
|
+
style={[
|
|
70
|
+
styles.text,
|
|
71
|
+
{ color: isUser ? colors.userText : colors.assistantText },
|
|
72
|
+
]}
|
|
73
|
+
>
|
|
74
|
+
{message.text}
|
|
75
|
+
</Text>
|
|
76
|
+
</View>
|
|
77
|
+
|
|
78
|
+
{isUser && (
|
|
79
|
+
<View style={styles.avatarSlot}>
|
|
80
|
+
{avatar ? (
|
|
81
|
+
<Image source={avatar} style={styles.avatar} />
|
|
82
|
+
) : (
|
|
83
|
+
<View
|
|
84
|
+
style={[
|
|
85
|
+
styles.avatarPlaceholder,
|
|
86
|
+
{ backgroundColor: colors.primary },
|
|
87
|
+
]}
|
|
88
|
+
>
|
|
89
|
+
<Text style={[styles.avatarLetter, { color: colors.userText }]}>
|
|
90
|
+
U
|
|
91
|
+
</Text>
|
|
92
|
+
</View>
|
|
93
|
+
)}
|
|
94
|
+
</View>
|
|
95
|
+
)}
|
|
96
|
+
</View>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const styles = StyleSheet.create({
|
|
101
|
+
row: {
|
|
102
|
+
flexDirection: 'row',
|
|
103
|
+
alignItems: 'flex-end',
|
|
104
|
+
marginVertical: 4,
|
|
105
|
+
paddingHorizontal: 12,
|
|
106
|
+
maxWidth: '100%',
|
|
107
|
+
},
|
|
108
|
+
rowUser: {
|
|
109
|
+
justifyContent: 'flex-end',
|
|
110
|
+
},
|
|
111
|
+
rowAssistant: {
|
|
112
|
+
justifyContent: 'flex-start',
|
|
113
|
+
},
|
|
114
|
+
avatarSlot: {
|
|
115
|
+
width: 32,
|
|
116
|
+
height: 32,
|
|
117
|
+
marginHorizontal: 6,
|
|
118
|
+
},
|
|
119
|
+
avatar: {
|
|
120
|
+
width: 32,
|
|
121
|
+
height: 32,
|
|
122
|
+
borderRadius: 16,
|
|
123
|
+
},
|
|
124
|
+
avatarPlaceholder: {
|
|
125
|
+
width: 32,
|
|
126
|
+
height: 32,
|
|
127
|
+
borderRadius: 16,
|
|
128
|
+
alignItems: 'center',
|
|
129
|
+
justifyContent: 'center',
|
|
130
|
+
},
|
|
131
|
+
avatarLetter: {
|
|
132
|
+
fontSize: 11,
|
|
133
|
+
fontWeight: '700',
|
|
134
|
+
},
|
|
135
|
+
bubble: {
|
|
136
|
+
maxWidth: '72%',
|
|
137
|
+
borderRadius: 16,
|
|
138
|
+
paddingHorizontal: 14,
|
|
139
|
+
paddingVertical: 10,
|
|
140
|
+
},
|
|
141
|
+
bubbleUser: {
|
|
142
|
+
borderBottomRightRadius: 4,
|
|
143
|
+
},
|
|
144
|
+
bubbleAssistant: {
|
|
145
|
+
borderBottomLeftRadius: 4,
|
|
146
|
+
},
|
|
147
|
+
text: {
|
|
148
|
+
fontSize: 15,
|
|
149
|
+
lineHeight: 21,
|
|
150
|
+
},
|
|
151
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from 'react';
|
|
2
|
+
import type { Message } from '../types/Message';
|
|
3
|
+
import MockChatService from '../services/MockChatService';
|
|
4
|
+
|
|
5
|
+
function createId(): string {
|
|
6
|
+
return `${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface UseChatOptions {
|
|
10
|
+
welcomeMessage?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function useChat(options: UseChatOptions = {}) {
|
|
14
|
+
const { welcomeMessage = 'Hi, how can I help?' } = options;
|
|
15
|
+
|
|
16
|
+
const [messages, setMessages] = useState<Message[]>(() => [
|
|
17
|
+
{
|
|
18
|
+
id: createId(),
|
|
19
|
+
text: welcomeMessage,
|
|
20
|
+
isUser: false,
|
|
21
|
+
timestamp: Date.now(),
|
|
22
|
+
},
|
|
23
|
+
]);
|
|
24
|
+
const [isTyping, setIsTyping] = useState(false);
|
|
25
|
+
const [isStreaming, setIsStreaming] = useState(false);
|
|
26
|
+
const busyRef = useRef(false);
|
|
27
|
+
|
|
28
|
+
const sendMessage = useCallback(async (text: string) => {
|
|
29
|
+
const trimmed = text.trim();
|
|
30
|
+
if (!trimmed || busyRef.current) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
busyRef.current = true;
|
|
35
|
+
|
|
36
|
+
const userMessage: Message = {
|
|
37
|
+
id: createId(),
|
|
38
|
+
text: trimmed,
|
|
39
|
+
isUser: true,
|
|
40
|
+
timestamp: Date.now(),
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const assistantId = createId();
|
|
44
|
+
const assistantMessage: Message = {
|
|
45
|
+
id: assistantId,
|
|
46
|
+
text: '',
|
|
47
|
+
isUser: false,
|
|
48
|
+
timestamp: Date.now(),
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
setMessages(prev => [...prev, userMessage, assistantMessage]);
|
|
52
|
+
setIsTyping(true);
|
|
53
|
+
setIsStreaming(true);
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
await MockChatService.streamMessage(trimmed, partialText => {
|
|
57
|
+
setIsTyping(false);
|
|
58
|
+
setMessages(prev =>
|
|
59
|
+
prev.map(msg =>
|
|
60
|
+
msg.id === assistantId ? { ...msg, text: partialText } : msg,
|
|
61
|
+
),
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
} finally {
|
|
65
|
+
setIsTyping(false);
|
|
66
|
+
setIsStreaming(false);
|
|
67
|
+
busyRef.current = false;
|
|
68
|
+
}
|
|
69
|
+
}, []);
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
messages,
|
|
73
|
+
isTyping,
|
|
74
|
+
isStreaming,
|
|
75
|
+
sendMessage,
|
|
76
|
+
};
|
|
77
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { AIChat } from './components/AIChat';
|
|
2
|
+
export { MessageBubble } from './components/MessageBubble';
|
|
3
|
+
export { ChatInput } from './components/ChatInput';
|
|
4
|
+
export { useChat } from './hooks/useChat';
|
|
5
|
+
export { default as MockChatService } from './services/MockChatService';
|
|
6
|
+
export { getThemeColors } from './theme/colors';
|
|
7
|
+
export type { Message, AIChatProps, ChatTheme } from './types/Message';
|
|
8
|
+
export type { ThemeColors } from './theme/colors';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const sleep = (ms: number) => new Promise<void>(resolve => setTimeout(resolve, ms));
|
|
2
|
+
|
|
3
|
+
class MockChatService {
|
|
4
|
+
async sendMessage(message: string): Promise<string> {
|
|
5
|
+
await sleep(1500);
|
|
6
|
+
return `Dummy response for: ${message}`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async streamMessage(
|
|
10
|
+
message: string,
|
|
11
|
+
onChunk: (partialText: string) => void,
|
|
12
|
+
): Promise<string> {
|
|
13
|
+
await sleep(600);
|
|
14
|
+
|
|
15
|
+
const dummyResponse =
|
|
16
|
+
`Hello! This is a dummy streaming response for: "${message}". ` +
|
|
17
|
+
'In a real integration, this text would come from your AI backend.';
|
|
18
|
+
|
|
19
|
+
let currentText = '';
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < dummyResponse.length; i++) {
|
|
22
|
+
currentText += dummyResponse[i];
|
|
23
|
+
onChunk(currentText);
|
|
24
|
+
await sleep(30);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return currentText;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default new MockChatService();
|