@volkansuner/aichat-react-native 0.1.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/LICENSE +22 -0
- package/README.md +528 -0
- package/dist/AIChatSDK.d.ts +75 -0
- package/dist/AIChatSDK.d.ts.map +1 -0
- package/dist/AIChatSDK.js +357 -0
- package/dist/AIChatSDK.js.map +1 -0
- package/dist/components/AIChatProvider.d.ts +25 -0
- package/dist/components/AIChatProvider.d.ts.map +1 -0
- package/dist/components/AIChatProvider.js +93 -0
- package/dist/components/AIChatProvider.js.map +1 -0
- package/dist/components/AIChatWidget.d.ts +62 -0
- package/dist/components/AIChatWidget.d.ts.map +1 -0
- package/dist/components/AIChatWidget.handlers.d.ts +15 -0
- package/dist/components/AIChatWidget.handlers.d.ts.map +1 -0
- package/dist/components/AIChatWidget.handlers.js +180 -0
- package/dist/components/AIChatWidget.handlers.js.map +1 -0
- package/dist/components/AIChatWidget.js +403 -0
- package/dist/components/AIChatWidget.js.map +1 -0
- package/dist/components/AIChatWidget.styles.d.ts +17 -0
- package/dist/components/AIChatWidget.styles.d.ts.map +1 -0
- package/dist/components/AIChatWidget.styles.js +68 -0
- package/dist/components/AIChatWidget.styles.js.map +1 -0
- package/dist/components/AIChatWidget.types.d.ts +250 -0
- package/dist/components/AIChatWidget.types.d.ts.map +1 -0
- package/dist/components/AIChatWidget.types.js +3 -0
- package/dist/components/AIChatWidget.types.js.map +1 -0
- package/dist/components/AIChatWidget.utils.d.ts +12 -0
- package/dist/components/AIChatWidget.utils.d.ts.map +1 -0
- package/dist/components/AIChatWidget.utils.js +109 -0
- package/dist/components/AIChatWidget.utils.js.map +1 -0
- package/dist/components/ConfigError.d.ts +33 -0
- package/dist/components/ConfigError.d.ts.map +1 -0
- package/dist/components/ConfigError.js +195 -0
- package/dist/components/ConfigError.js.map +1 -0
- package/dist/components/FloatingButton.d.ts +12 -0
- package/dist/components/FloatingButton.d.ts.map +1 -0
- package/dist/components/FloatingButton.js +163 -0
- package/dist/components/FloatingButton.js.map +1 -0
- package/dist/components/InputArea.d.ts +58 -0
- package/dist/components/InputArea.d.ts.map +1 -0
- package/dist/components/InputArea.js +135 -0
- package/dist/components/InputArea.js.map +1 -0
- package/dist/components/MarkdownText.d.ts +16 -0
- package/dist/components/MarkdownText.d.ts.map +1 -0
- package/dist/components/MarkdownText.js +263 -0
- package/dist/components/MarkdownText.js.map +1 -0
- package/dist/components/MessageBubble.d.ts +101 -0
- package/dist/components/MessageBubble.d.ts.map +1 -0
- package/dist/components/MessageBubble.js +377 -0
- package/dist/components/MessageBubble.js.map +1 -0
- package/dist/components/NavigationSuggestion.d.ts +13 -0
- package/dist/components/NavigationSuggestion.d.ts.map +1 -0
- package/dist/components/NavigationSuggestion.js +117 -0
- package/dist/components/NavigationSuggestion.js.map +1 -0
- package/dist/components/QuickReplies.d.ts +15 -0
- package/dist/components/QuickReplies.d.ts.map +1 -0
- package/dist/components/QuickReplies.js +49 -0
- package/dist/components/QuickReplies.js.map +1 -0
- package/dist/components/TemplateRenderer.d.ts +17 -0
- package/dist/components/TemplateRenderer.d.ts.map +1 -0
- package/dist/components/TemplateRenderer.js +397 -0
- package/dist/components/TemplateRenderer.js.map +1 -0
- package/dist/components/TypingIndicator.d.ts +29 -0
- package/dist/components/TypingIndicator.d.ts.map +1 -0
- package/dist/components/TypingIndicator.js +110 -0
- package/dist/components/TypingIndicator.js.map +1 -0
- package/dist/components/defaults/DefaultCard.d.ts +15 -0
- package/dist/components/defaults/DefaultCard.d.ts.map +1 -0
- package/dist/components/defaults/DefaultCard.js +157 -0
- package/dist/components/defaults/DefaultCard.js.map +1 -0
- package/dist/components/defaults/DefaultDetail.d.ts +15 -0
- package/dist/components/defaults/DefaultDetail.d.ts.map +1 -0
- package/dist/components/defaults/DefaultDetail.js +139 -0
- package/dist/components/defaults/DefaultDetail.js.map +1 -0
- package/dist/components/defaults/DefaultList.d.ts +13 -0
- package/dist/components/defaults/DefaultList.d.ts.map +1 -0
- package/dist/components/defaults/DefaultList.js +99 -0
- package/dist/components/defaults/DefaultList.js.map +1 -0
- package/dist/components/defaults/DefaultTable.d.ts +15 -0
- package/dist/components/defaults/DefaultTable.d.ts.map +1 -0
- package/dist/components/defaults/DefaultTable.js +205 -0
- package/dist/components/defaults/DefaultTable.js.map +1 -0
- package/dist/components/defaults/index.d.ts +15 -0
- package/dist/components/defaults/index.d.ts.map +1 -0
- package/dist/components/defaults/index.js +26 -0
- package/dist/components/defaults/index.js.map +1 -0
- package/dist/components/index.d.ts +17 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +33 -0
- package/dist/components/index.js.map +1 -0
- package/dist/constants.d.ts +41 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +52 -0
- package/dist/constants.js.map +1 -0
- package/dist/hooks/useAIChatConfig.d.ts +61 -0
- package/dist/hooks/useAIChatConfig.d.ts.map +1 -0
- package/dist/hooks/useAIChatConfig.js +95 -0
- package/dist/hooks/useAIChatConfig.js.map +1 -0
- package/dist/hooks/useChatMessages.d.ts +27 -0
- package/dist/hooks/useChatMessages.d.ts.map +1 -0
- package/dist/hooks/useChatMessages.js +255 -0
- package/dist/hooks/useChatMessages.js.map +1 -0
- package/dist/hooks/useContextManager.d.ts +13 -0
- package/dist/hooks/useContextManager.d.ts.map +1 -0
- package/dist/hooks/useContextManager.js +42 -0
- package/dist/hooks/useContextManager.js.map +1 -0
- package/dist/hooks/useSDK.d.ts +18 -0
- package/dist/hooks/useSDK.d.ts.map +1 -0
- package/dist/hooks/useSDK.js +64 -0
- package/dist/hooks/useSDK.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +46 -0
- package/dist/index.js.map +1 -0
- package/dist/registry/TemplateRegistry.d.ts +48 -0
- package/dist/registry/TemplateRegistry.d.ts.map +1 -0
- package/dist/registry/TemplateRegistry.js +77 -0
- package/dist/registry/TemplateRegistry.js.map +1 -0
- package/dist/registry/index.d.ts +5 -0
- package/dist/registry/index.d.ts.map +1 -0
- package/dist/registry/index.js +9 -0
- package/dist/registry/index.js.map +1 -0
- package/dist/services/BackendService.d.ts +47 -0
- package/dist/services/BackendService.d.ts.map +1 -0
- package/dist/services/BackendService.js +362 -0
- package/dist/services/BackendService.js.map +1 -0
- package/dist/services/ConversationManager.d.ts +40 -0
- package/dist/services/ConversationManager.d.ts.map +1 -0
- package/dist/services/ConversationManager.js +95 -0
- package/dist/services/ConversationManager.js.map +1 -0
- package/dist/services/LocalAIClassifier.d.ts +32 -0
- package/dist/services/LocalAIClassifier.d.ts.map +1 -0
- package/dist/services/LocalAIClassifier.js +149 -0
- package/dist/services/LocalAIClassifier.js.map +1 -0
- package/dist/services/NavigationHandler.d.ts +30 -0
- package/dist/services/NavigationHandler.d.ts.map +1 -0
- package/dist/services/NavigationHandler.js +74 -0
- package/dist/services/NavigationHandler.js.map +1 -0
- package/dist/themes/dark.d.ts +6 -0
- package/dist/themes/dark.d.ts.map +1 -0
- package/dist/themes/dark.js +130 -0
- package/dist/themes/dark.js.map +1 -0
- package/dist/themes/index.d.ts +13 -0
- package/dist/themes/index.d.ts.map +1 -0
- package/dist/themes/index.js +34 -0
- package/dist/themes/index.js.map +1 -0
- package/dist/themes/light.d.ts +6 -0
- package/dist/themes/light.d.ts.map +1 -0
- package/dist/themes/light.js +130 -0
- package/dist/themes/light.js.map +1 -0
- package/dist/themes/types.d.ts +143 -0
- package/dist/themes/types.d.ts.map +1 -0
- package/dist/themes/types.js +6 -0
- package/dist/themes/types.js.map +1 -0
- package/dist/types/api.d.ts +118 -0
- package/dist/types/api.d.ts.map +1 -0
- package/dist/types/api.js +6 -0
- package/dist/types/api.js.map +1 -0
- package/dist/types/chat.d.ts +154 -0
- package/dist/types/chat.d.ts.map +1 -0
- package/dist/types/chat.js +6 -0
- package/dist/types/chat.js.map +1 -0
- package/dist/types/components.d.ts +69 -0
- package/dist/types/components.d.ts.map +1 -0
- package/dist/types/components.js +17 -0
- package/dist/types/components.js.map +1 -0
- package/dist/types/config.d.ts +122 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +18 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/document.d.ts +108 -0
- package/dist/types/document.d.ts.map +1 -0
- package/dist/types/document.js +7 -0
- package/dist/types/document.js.map +1 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +31 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/local-ai.d.ts +48 -0
- package/dist/types/local-ai.d.ts.map +1 -0
- package/dist/types/local-ai.js +17 -0
- package/dist/types/local-ai.js.map +1 -0
- package/dist/types/navigation.d.ts +89 -0
- package/dist/types/navigation.d.ts.map +1 -0
- package/dist/types/navigation.js +6 -0
- package/dist/types/navigation.js.map +1 -0
- package/dist/types/sdk.d.ts +47 -0
- package/dist/types/sdk.d.ts.map +1 -0
- package/dist/types/sdk.js +6 -0
- package/dist/types/sdk.js.map +1 -0
- package/dist/types/template.d.ts +220 -0
- package/dist/types/template.d.ts.map +1 -0
- package/dist/types/template.js +8 -0
- package/dist/types/template.js.map +1 -0
- package/dist/utils/configLoader.d.ts +69 -0
- package/dist/utils/configLoader.d.ts.map +1 -0
- package/dist/utils/configLoader.js +278 -0
- package/dist/utils/configLoader.js.map +1 -0
- package/dist/utils/deepMerge.d.ts +47 -0
- package/dist/utils/deepMerge.d.ts.map +1 -0
- package/dist/utils/deepMerge.js +71 -0
- package/dist/utils/deepMerge.js.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Volkan Suner
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
# AIChatSDK for React Native
|
|
2
|
+
|
|
3
|
+
AI-powered conversational interface for React Native applications with intelligent navigation and hybrid AI processing.
|
|
4
|
+
|
|
5
|
+
## 📦 Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @volkansuner/aichat-react-native
|
|
9
|
+
# or
|
|
10
|
+
yarn add @volkansuner/aichat-react-native
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @volkansuner/aichat-react-native
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Peer Dependencies
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# Sadece AsyncStorage gerekli (axios kaldırıldı, native fetch kullanılıyor)
|
|
19
|
+
npm install react-native @react-navigation/native @react-native-async-storage/async-storage
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 🚀 Quick Start
|
|
23
|
+
|
|
24
|
+
### 1. Initialize SDK
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { AIChatSDK } from '@volkansuner/aichat-react-native';
|
|
28
|
+
import { NavigationContainer } from '@react-navigation/native';
|
|
29
|
+
import { useNavigationContainerRef } from '@react-navigation/native';
|
|
30
|
+
|
|
31
|
+
function App() {
|
|
32
|
+
const navigationRef = useNavigationContainerRef();
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
const initSDK = async () => {
|
|
36
|
+
const sdk = AIChatSDK.getInstance();
|
|
37
|
+
|
|
38
|
+
await sdk.initialize({
|
|
39
|
+
apiKey: 'your-api-key-here',
|
|
40
|
+
navigationRef,
|
|
41
|
+
enableLocalAI: true,
|
|
42
|
+
onStatusChange: (status) => {
|
|
43
|
+
console.log('SDK Status:', status);
|
|
44
|
+
},
|
|
45
|
+
onNavigationSuggest: (suggestion) => {
|
|
46
|
+
console.log('Navigate to:', suggestion.screenName);
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
initSDK();
|
|
52
|
+
}, []);
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<NavigationContainer ref={navigationRef}>
|
|
56
|
+
{/* Your navigation */}
|
|
57
|
+
</NavigationContainer>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 2. Send Messages
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { getSDK } from '@volkansuner/aichat-react-native';
|
|
66
|
+
|
|
67
|
+
async function handleUserMessage(message: string) {
|
|
68
|
+
const sdk = getSDK();
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
const response = await sdk.sendMessage(message);
|
|
72
|
+
|
|
73
|
+
console.log('AI Response:', response.message);
|
|
74
|
+
console.log('Handled by:', response.handledBy); // 'local' or 'backend'
|
|
75
|
+
console.log('Confidence:', response.confidence);
|
|
76
|
+
|
|
77
|
+
if (response.navigationSuggestion) {
|
|
78
|
+
// AI suggested navigation
|
|
79
|
+
console.log('Suggested screen:', response.navigationSuggestion.screenName);
|
|
80
|
+
}
|
|
81
|
+
} catch (error) {
|
|
82
|
+
console.error('Error:', error);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 3. Access Conversation History
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const sdk = getSDK();
|
|
91
|
+
const history = await sdk.getConversationHistory();
|
|
92
|
+
|
|
93
|
+
console.log('Messages:', history);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## 🎯 Features
|
|
97
|
+
|
|
98
|
+
### Hybrid AI Processing
|
|
99
|
+
|
|
100
|
+
The SDK uses a **two-tier AI system**:
|
|
101
|
+
|
|
102
|
+
1. **Local AI** (DistilBERT via ONNX Runtime)
|
|
103
|
+
- Handles simple intents locally (<150ms)
|
|
104
|
+
- Confidence threshold: 0.85
|
|
105
|
+
- Intents: Navigate, Search, Action, General
|
|
106
|
+
- **Works offline**
|
|
107
|
+
|
|
108
|
+
2. **Backend AI** (GPT-4 Turbo via API)
|
|
109
|
+
- Complex conversations
|
|
110
|
+
- Context-aware responses
|
|
111
|
+
- Navigation schema integration
|
|
112
|
+
- Fallback for low-confidence local results
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
// Example: High-confidence local handling
|
|
116
|
+
User: "Go to settings"
|
|
117
|
+
→ Local AI (confidence: 0.92) → Navigate to Settings
|
|
118
|
+
|
|
119
|
+
// Example: Low-confidence → backend fallback
|
|
120
|
+
User: "Show me products under $50 in electronics category"
|
|
121
|
+
→ Local AI (confidence: 0.65) → Backend API → Complex response
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Navigation Intelligence
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
// User asks: "Show me my profile"
|
|
128
|
+
const response = await sdk.sendMessage("Show me my profile");
|
|
129
|
+
|
|
130
|
+
if (response.navigationSuggestion) {
|
|
131
|
+
// Automatically navigate
|
|
132
|
+
sdk.navigate(response.navigationSuggestion.screenName);
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Conversation Management
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
const sdk = getSDK();
|
|
140
|
+
|
|
141
|
+
// Get history
|
|
142
|
+
const history = await sdk.getConversationHistory();
|
|
143
|
+
|
|
144
|
+
// Clear conversation
|
|
145
|
+
await sdk.clearConversation();
|
|
146
|
+
|
|
147
|
+
// Get status
|
|
148
|
+
const status = sdk.getStatus(); // 'uninitialized' | 'initializing' | 'ready' | 'error'
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## 📚 API Reference
|
|
152
|
+
|
|
153
|
+
### `AIChatSDK.initialize(config)`
|
|
154
|
+
|
|
155
|
+
Initialize the SDK with configuration.
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
interface SDKConfig {
|
|
159
|
+
apiKey: string; // Required: Your API key
|
|
160
|
+
apiUrl?: string; // Optional: Custom API URL
|
|
161
|
+
enableLocalAI?: boolean; // Optional: Enable local AI (default: true)
|
|
162
|
+
navigationRef?: NavigationContainerRef; // Optional: React Navigation ref
|
|
163
|
+
localAI?: {
|
|
164
|
+
confidenceThreshold?: number; // Default: 0.85
|
|
165
|
+
};
|
|
166
|
+
onStatusChange?: (status) => void; // Optional: Status callback
|
|
167
|
+
onNavigationSuggest?: (suggestion) => void; // Optional: Navigation callback
|
|
168
|
+
onError?: (error) => void; // Optional: Error callback
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### `sdk.sendMessage(message: string)`
|
|
173
|
+
|
|
174
|
+
Send a chat message and get AI response.
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
const response = await sdk.sendMessage("Hello");
|
|
178
|
+
|
|
179
|
+
interface ChatResponse {
|
|
180
|
+
id: string;
|
|
181
|
+
message: string;
|
|
182
|
+
timestamp: Date;
|
|
183
|
+
handledBy: 'local' | 'backend';
|
|
184
|
+
confidence?: number;
|
|
185
|
+
tokensUsed?: number;
|
|
186
|
+
navigationSuggestion?: {
|
|
187
|
+
screenName: string;
|
|
188
|
+
confidence: number;
|
|
189
|
+
message: string;
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### `sdk.getConversationHistory()`
|
|
195
|
+
|
|
196
|
+
Get conversation history.
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
const history = await sdk.getConversationHistory();
|
|
200
|
+
// Returns: ChatMessage[]
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### `sdk.clearConversation()`
|
|
204
|
+
|
|
205
|
+
Clear conversation history and start new session.
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
await sdk.clearConversation();
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### `sdk.navigate(screenName: string, params?: any)`
|
|
212
|
+
|
|
213
|
+
Programmatically navigate to a screen.
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
sdk.navigate('Profile', { userId: '123' });
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### `sdk.getStatus()`
|
|
220
|
+
|
|
221
|
+
Get current SDK status.
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
const status = sdk.getStatus();
|
|
225
|
+
// Returns: 'uninitialized' | 'initializing' | 'ready' | 'error'
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## 🔧 Configuration Examples
|
|
229
|
+
|
|
230
|
+
### Basic Setup
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
await sdk.initialize({
|
|
234
|
+
apiKey: 'aichat_test_pk_...',
|
|
235
|
+
});
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### With Navigation
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
const navigationRef = useNavigationContainerRef();
|
|
242
|
+
|
|
243
|
+
await sdk.initialize({
|
|
244
|
+
apiKey: 'aichat_test_pk_...',
|
|
245
|
+
navigationRef,
|
|
246
|
+
onNavigationSuggest: (suggestion) => {
|
|
247
|
+
Alert.alert(
|
|
248
|
+
'Navigate?',
|
|
249
|
+
`Do you want to go to ${suggestion.screenName}?`,
|
|
250
|
+
[
|
|
251
|
+
{ text: 'Cancel', style: 'cancel' },
|
|
252
|
+
{ text: 'Go', onPress: () => sdk.navigate(suggestion.screenName) },
|
|
253
|
+
]
|
|
254
|
+
);
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Backend-Only Mode
|
|
260
|
+
|
|
261
|
+
```typescript
|
|
262
|
+
await sdk.initialize({
|
|
263
|
+
apiKey: 'aichat_test_pk_...',
|
|
264
|
+
enableLocalAI: false, // Disable local AI, always use backend
|
|
265
|
+
});
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Custom API URL
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
await sdk.initialize({
|
|
272
|
+
apiKey: 'aichat_test_pk_...',
|
|
273
|
+
apiUrl: 'https://your-custom-api.com/v1',
|
|
274
|
+
});
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## 💬 Usage Examples
|
|
278
|
+
|
|
279
|
+
### Simple Chat
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
import React, { useState } from 'react';
|
|
283
|
+
import { View, TextInput, Button, Text } from 'react-native';
|
|
284
|
+
import { getSDK } from '@volkansuner/aichat-react-native';
|
|
285
|
+
|
|
286
|
+
function ChatScreen() {
|
|
287
|
+
const [message, setMessage] = useState('');
|
|
288
|
+
const [response, setResponse] = useState('');
|
|
289
|
+
|
|
290
|
+
const sendMessage = async () => {
|
|
291
|
+
const sdk = getSDK();
|
|
292
|
+
const result = await sdk.sendMessage(message);
|
|
293
|
+
setResponse(result.message);
|
|
294
|
+
setMessage('');
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
return (
|
|
298
|
+
<View>
|
|
299
|
+
<TextInput
|
|
300
|
+
value={message}
|
|
301
|
+
onChangeText={setMessage}
|
|
302
|
+
placeholder="Type a message..."
|
|
303
|
+
/>
|
|
304
|
+
<Button title="Send" onPress={sendMessage} />
|
|
305
|
+
<Text>{response}</Text>
|
|
306
|
+
</View>
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### With History
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
import React, { useState, useEffect } from 'react';
|
|
315
|
+
import { FlatList, Text } from 'react-native';
|
|
316
|
+
import { getSDK } from '@volkansuner/aichat-react-native';
|
|
317
|
+
|
|
318
|
+
function ChatHistory() {
|
|
319
|
+
const [messages, setMessages] = useState([]);
|
|
320
|
+
|
|
321
|
+
useEffect(() => {
|
|
322
|
+
loadHistory();
|
|
323
|
+
}, []);
|
|
324
|
+
|
|
325
|
+
const loadHistory = async () => {
|
|
326
|
+
const sdk = getSDK();
|
|
327
|
+
const history = await sdk.getConversationHistory();
|
|
328
|
+
setMessages(history);
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
return (
|
|
332
|
+
<FlatList
|
|
333
|
+
data={messages}
|
|
334
|
+
keyExtractor={(item) => item.id}
|
|
335
|
+
renderItem={({ item }) => (
|
|
336
|
+
<Text>{item.role}: {item.text}</Text>
|
|
337
|
+
)}
|
|
338
|
+
/>
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Navigation Integration
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
const sdk = getSDK();
|
|
347
|
+
|
|
348
|
+
// User asks: "Take me to my orders"
|
|
349
|
+
const response = await sdk.sendMessage("Take me to my orders");
|
|
350
|
+
|
|
351
|
+
if (response.navigationSuggestion) {
|
|
352
|
+
// Option 1: Auto-navigate
|
|
353
|
+
sdk.navigate(response.navigationSuggestion.screenName);
|
|
354
|
+
|
|
355
|
+
// Option 2: Ask user first
|
|
356
|
+
Alert.alert(
|
|
357
|
+
'Navigation',
|
|
358
|
+
response.navigationSuggestion.message,
|
|
359
|
+
[
|
|
360
|
+
{ text: 'Cancel' },
|
|
361
|
+
{
|
|
362
|
+
text: 'Go',
|
|
363
|
+
onPress: () => sdk.navigate(response.navigationSuggestion.screenName)
|
|
364
|
+
},
|
|
365
|
+
]
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
## 🎨 Best Practices
|
|
371
|
+
|
|
372
|
+
### 1. Initialize Early
|
|
373
|
+
|
|
374
|
+
Initialize SDK in your root component:
|
|
375
|
+
|
|
376
|
+
```typescript
|
|
377
|
+
function App() {
|
|
378
|
+
useEffect(() => {
|
|
379
|
+
const init = async () => {
|
|
380
|
+
const sdk = AIChatSDK.getInstance();
|
|
381
|
+
await sdk.initialize({ apiKey: process.env.AICHAT_API_KEY });
|
|
382
|
+
};
|
|
383
|
+
init();
|
|
384
|
+
}, []);
|
|
385
|
+
}
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
### 2. Handle Errors
|
|
389
|
+
|
|
390
|
+
```typescript
|
|
391
|
+
try {
|
|
392
|
+
const response = await sdk.sendMessage(message);
|
|
393
|
+
} catch (error) {
|
|
394
|
+
if (error.message.includes('Invalid API key')) {
|
|
395
|
+
// Handle auth error
|
|
396
|
+
} else if (error.message.includes('Rate limit')) {
|
|
397
|
+
// Handle rate limit
|
|
398
|
+
} else {
|
|
399
|
+
// Handle other errors
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### 3. Provide Feedback
|
|
405
|
+
|
|
406
|
+
```typescript
|
|
407
|
+
const [loading, setLoading] = useState(false);
|
|
408
|
+
|
|
409
|
+
const send = async () => {
|
|
410
|
+
setLoading(true);
|
|
411
|
+
try {
|
|
412
|
+
const response = await sdk.sendMessage(message);
|
|
413
|
+
// Handle response
|
|
414
|
+
} finally {
|
|
415
|
+
setLoading(false);
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### 4. Navigation Confirmation
|
|
421
|
+
|
|
422
|
+
Always ask user before navigating:
|
|
423
|
+
|
|
424
|
+
```typescript
|
|
425
|
+
if (response.navigationSuggestion) {
|
|
426
|
+
// ✅ Good: Ask first
|
|
427
|
+
const confirmed = await showConfirmDialog(
|
|
428
|
+
`Go to ${response.navigationSuggestion.screenName}?`
|
|
429
|
+
);
|
|
430
|
+
if (confirmed) {
|
|
431
|
+
sdk.navigate(response.navigationSuggestion.screenName);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// ❌ Bad: Auto-navigate without confirmation
|
|
435
|
+
// sdk.navigate(response.navigationSuggestion.screenName);
|
|
436
|
+
}
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
## 🔄 Hybrid AI Flow
|
|
440
|
+
|
|
441
|
+
```
|
|
442
|
+
User Message
|
|
443
|
+
↓
|
|
444
|
+
Local AI Classifier (DistilBERT)
|
|
445
|
+
↓
|
|
446
|
+
Confidence ≥ 0.85?
|
|
447
|
+
↓
|
|
448
|
+
YES → Handle Locally (fast, offline)
|
|
449
|
+
↓
|
|
450
|
+
NO → Send to Backend (GPT-4, context-aware)
|
|
451
|
+
↓
|
|
452
|
+
Navigation Suggestion?
|
|
453
|
+
↓
|
|
454
|
+
YES → Suggest screen navigation
|
|
455
|
+
↓
|
|
456
|
+
Return Response
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
## 📊 Performance
|
|
460
|
+
|
|
461
|
+
- **Local AI**: < 150ms response time
|
|
462
|
+
- **Backend API**: < 2s response time
|
|
463
|
+
- **Model Size**: 20MB (DistilBERT quantized)
|
|
464
|
+
- **Memory Usage**: ~50MB additional
|
|
465
|
+
|
|
466
|
+
## 🛠️ Troubleshooting
|
|
467
|
+
|
|
468
|
+
### SDK not initializing
|
|
469
|
+
|
|
470
|
+
```typescript
|
|
471
|
+
// Check status
|
|
472
|
+
const status = sdk.getStatus();
|
|
473
|
+
console.log('Status:', status);
|
|
474
|
+
|
|
475
|
+
// Reinitialize if error
|
|
476
|
+
if (status === 'error') {
|
|
477
|
+
await sdk.destroy();
|
|
478
|
+
await sdk.initialize(config);
|
|
479
|
+
}
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
### Navigation not working
|
|
483
|
+
|
|
484
|
+
```typescript
|
|
485
|
+
// Make sure navigationRef is passed
|
|
486
|
+
const navigationRef = useNavigationContainerRef();
|
|
487
|
+
|
|
488
|
+
await sdk.initialize({
|
|
489
|
+
apiKey: '...',
|
|
490
|
+
navigationRef, // ← Important!
|
|
491
|
+
});
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
### Local AI not loading
|
|
495
|
+
|
|
496
|
+
```typescript
|
|
497
|
+
// Check if local AI is enabled
|
|
498
|
+
await sdk.initialize({
|
|
499
|
+
apiKey: '...',
|
|
500
|
+
enableLocalAI: true, // Make sure this is true
|
|
501
|
+
localAI: {
|
|
502
|
+
confidenceThreshold: 0.85,
|
|
503
|
+
},
|
|
504
|
+
});
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
## 📝 TypeScript Support
|
|
508
|
+
|
|
509
|
+
Fully typed with TypeScript:
|
|
510
|
+
|
|
511
|
+
```typescript
|
|
512
|
+
import type {
|
|
513
|
+
SDKConfig,
|
|
514
|
+
ChatMessage,
|
|
515
|
+
ChatResponse,
|
|
516
|
+
NavigationSuggestion,
|
|
517
|
+
} from '@volkansuner/aichat-react-native';
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
## 🔗 Related
|
|
521
|
+
|
|
522
|
+
- [Backend API](../../apps/api/README.md)
|
|
523
|
+
- [CLI Tool](../cli/README.md)
|
|
524
|
+
- [Project Documentation](../../README.md)
|
|
525
|
+
|
|
526
|
+
## 📄 License
|
|
527
|
+
|
|
528
|
+
MIT
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { SDKConfig, SDKStatusType, ChatMessage, ChatResponse } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* AIChatSDK - Main SDK class
|
|
4
|
+
* Provides AI-powered conversational interface for React Native apps
|
|
5
|
+
*/
|
|
6
|
+
export declare class AIChatSDK {
|
|
7
|
+
private static instance;
|
|
8
|
+
private config;
|
|
9
|
+
private status;
|
|
10
|
+
private localAI;
|
|
11
|
+
private backendService;
|
|
12
|
+
private conversationManager;
|
|
13
|
+
private navigationHandler;
|
|
14
|
+
private constructor();
|
|
15
|
+
/**
|
|
16
|
+
* Get singleton instance
|
|
17
|
+
*/
|
|
18
|
+
static getInstance(): AIChatSDK;
|
|
19
|
+
/**
|
|
20
|
+
* Initialize SDK
|
|
21
|
+
*/
|
|
22
|
+
initialize(config: SDKConfig): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Send a chat message
|
|
25
|
+
*/
|
|
26
|
+
sendMessage(message: string, context?: Record<string, any>, contextDescription?: string): Promise<ChatResponse>;
|
|
27
|
+
/**
|
|
28
|
+
* 🆕 Send message with streaming response
|
|
29
|
+
*/
|
|
30
|
+
sendMessageStream(message: string, onChunk: (chunk: string) => void, onComplete?: (response: ChatResponse) => void, onError?: (error: Error) => void, context?: Record<string, any>, contextDescription?: string, onToolSuggestions?: (tools: any[]) => void): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Handle local AI response
|
|
33
|
+
*/
|
|
34
|
+
private handleLocalResponse;
|
|
35
|
+
/**
|
|
36
|
+
* Get conversation history
|
|
37
|
+
*/
|
|
38
|
+
getConversationHistory(): Promise<ChatMessage[]>;
|
|
39
|
+
/**
|
|
40
|
+
* Clear conversation history
|
|
41
|
+
*/
|
|
42
|
+
clearConversation(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* 🆕 Add tool response to conversation
|
|
45
|
+
* Use this after executing a tool to display the results
|
|
46
|
+
*/
|
|
47
|
+
addToolResponse(toolKey: string, data: any, tool: any): ChatMessage;
|
|
48
|
+
/**
|
|
49
|
+
* Get SDK status
|
|
50
|
+
*/
|
|
51
|
+
getStatus(): SDKStatusType;
|
|
52
|
+
/**
|
|
53
|
+
* Get SDK configuration
|
|
54
|
+
*/
|
|
55
|
+
getConfig(): SDKConfig | null;
|
|
56
|
+
/**
|
|
57
|
+
* Navigate to screen
|
|
58
|
+
*/
|
|
59
|
+
navigate(screenName: string, params?: any): void;
|
|
60
|
+
/**
|
|
61
|
+
* Generate unique ID
|
|
62
|
+
*/
|
|
63
|
+
private generateId;
|
|
64
|
+
/**
|
|
65
|
+
* Resolve API key from config
|
|
66
|
+
* Priority: config.apiKey > config.keys[platform] > config.keys.default
|
|
67
|
+
*/
|
|
68
|
+
private resolveApiKey;
|
|
69
|
+
/**
|
|
70
|
+
* Destroy SDK instance
|
|
71
|
+
*/
|
|
72
|
+
destroy(): Promise<void>;
|
|
73
|
+
}
|
|
74
|
+
export declare const getSDK: () => AIChatSDK;
|
|
75
|
+
//# sourceMappingURL=AIChatSDK.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AIChatSDK.d.ts","sourceRoot":"","sources":["../src/AIChatSDK.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,WAAW,EAAE,YAAY,EAAiB,MAAM,SAAS,CAAC;AAM7F;;;GAGG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAA0B;IACjD,OAAO,CAAC,MAAM,CAA0B;IACxC,OAAO,CAAC,MAAM,CAAkC;IAChD,OAAO,CAAC,OAAO,CAAkC;IACjD,OAAO,CAAC,cAAc,CAA+B;IACrD,OAAO,CAAC,mBAAmB,CAAoC;IAC/D,OAAO,CAAC,iBAAiB,CAAkC;IAE3D,OAAO;IAEP;;OAEG;WACW,WAAW,IAAI,SAAS;IAOtC;;OAEG;IACU,UAAU,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;IAgDzD;;OAEG;IACU,WAAW,CACtB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC7B,kBAAkB,CAAC,EAAE,MAAM,GAC1B,OAAO,CAAC,YAAY,CAAC;IAgFxB;;OAEG;IACU,iBAAiB,CAC5B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,EAChC,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,YAAY,KAAK,IAAI,EAC7C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,EAChC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC7B,kBAAkB,CAAC,EAAE,MAAM,EAC3B,iBAAiB,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,IAAI,GACzC,OAAO,CAAC,IAAI,CAAC;IAoEhB;;OAEG;YACW,mBAAmB;IA8CjC;;OAEG;IACU,sBAAsB,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;IAO7D;;OAEG;IACU,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAO/C;;;OAGG;IACI,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,GAAG,WAAW;IAsB1E;;OAEG;IACI,SAAS,IAAI,aAAa;IAIjC;;OAEG;IACI,SAAS,IAAI,SAAS,GAAG,IAAI;IAIpC;;OAEG;IACI,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,IAAI;IAOvD;;OAEG;IACH,OAAO,CAAC,UAAU;IAIlB;;;OAGG;IACH,OAAO,CAAC,aAAa;IAgCrB;;OAEG;IACU,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAStC;AAGD,eAAO,MAAM,MAAM,iBAAgC,CAAC"}
|