@schmitech/chatbot-api 0.1.1 → 0.2.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/README.md +101 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -238,6 +238,107 @@ function ChatComponent() {
|
|
|
238
238
|
export default ChatComponent;
|
|
239
239
|
```
|
|
240
240
|
|
|
241
|
+
## Usage with Native Mobile Platforms
|
|
242
|
+
|
|
243
|
+
### React Native
|
|
244
|
+
|
|
245
|
+
If using React Native, you can use the npm package directly:
|
|
246
|
+
|
|
247
|
+
```javascript
|
|
248
|
+
import { configureApi, streamChat } from '@schmitech/chatbot-api';
|
|
249
|
+
|
|
250
|
+
// Configure API
|
|
251
|
+
configureApi('https://your-api-server.com');
|
|
252
|
+
|
|
253
|
+
// Usage in React Native component
|
|
254
|
+
async function handleChat(message) {
|
|
255
|
+
try {
|
|
256
|
+
for await (const response of streamChat(message, false)) {
|
|
257
|
+
// Update UI with response.text
|
|
258
|
+
}
|
|
259
|
+
} catch (error) {
|
|
260
|
+
console.error('Chat error:', error);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### Native iOS (Swift)
|
|
266
|
+
|
|
267
|
+
For pure native iOS, you'll need to create a Swift wrapper:
|
|
268
|
+
|
|
269
|
+
```swift
|
|
270
|
+
// ChatService.swift
|
|
271
|
+
import Foundation
|
|
272
|
+
|
|
273
|
+
class ChatService {
|
|
274
|
+
private let apiUrl: String
|
|
275
|
+
|
|
276
|
+
init(apiUrl: String) {
|
|
277
|
+
self.apiUrl = apiUrl
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
func sendMessage(_ message: String, voiceEnabled: Bool,
|
|
281
|
+
onResponse: @escaping (String, Bool) -> Void,
|
|
282
|
+
onError: @escaping (Error) -> Void) {
|
|
283
|
+
|
|
284
|
+
guard let url = URL(string: "\(apiUrl)/chat") else {
|
|
285
|
+
onError(NSError(domain: "Invalid URL", code: 0))
|
|
286
|
+
return
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
var request = URLRequest(url: url)
|
|
290
|
+
request.httpMethod = "POST"
|
|
291
|
+
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
292
|
+
|
|
293
|
+
let body: [String: Any] = ["message": message, "voiceEnabled": voiceEnabled]
|
|
294
|
+
request.httpBody = try? JSONSerialization.data(withJSONObject: body)
|
|
295
|
+
|
|
296
|
+
let task = URLSession.shared.dataTask(with: request) { data, response, error in
|
|
297
|
+
// Handle streaming responses
|
|
298
|
+
// ...
|
|
299
|
+
}
|
|
300
|
+
task.resume()
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### Native Android (Kotlin)
|
|
306
|
+
|
|
307
|
+
For pure native Android, you'd implement:
|
|
308
|
+
|
|
309
|
+
```kotlin
|
|
310
|
+
// ChatService.kt
|
|
311
|
+
class ChatService(private val apiUrl: String) {
|
|
312
|
+
fun sendMessage(
|
|
313
|
+
message: String,
|
|
314
|
+
voiceEnabled: Boolean,
|
|
315
|
+
onResponse: (text: String, isDone: Boolean) -> Unit,
|
|
316
|
+
onError: (error: Throwable) -> Unit
|
|
317
|
+
) {
|
|
318
|
+
val client = OkHttpClient()
|
|
319
|
+
|
|
320
|
+
val requestBody = JSONObject().apply {
|
|
321
|
+
put("message", message)
|
|
322
|
+
put("voiceEnabled", voiceEnabled)
|
|
323
|
+
}.toString().toRequestBody("application/json".toMediaType())
|
|
324
|
+
|
|
325
|
+
val request = Request.Builder()
|
|
326
|
+
.url("$apiUrl/chat")
|
|
327
|
+
.post(requestBody)
|
|
328
|
+
.build()
|
|
329
|
+
|
|
330
|
+
// Set up streaming response handling
|
|
331
|
+
// ...
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
### Alternative Approaches
|
|
337
|
+
|
|
338
|
+
1. **Flutter**: Create Dart implementation using http package
|
|
339
|
+
2. **WebView**: Embed a web component in your app that uses the JS client
|
|
340
|
+
3. **Capacitor/Cordova**: Create a hybrid app, use the npm package directly
|
|
341
|
+
|
|
241
342
|
## API Reference
|
|
242
343
|
|
|
243
344
|
### configureApi(apiUrl)
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schmitech/chatbot-api",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"description": "API client for the Chatbot server",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/api.cjs",
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"preview": "vite preview",
|
|
26
26
|
"test": "vitest",
|
|
27
27
|
"test:watch": "vitest",
|
|
28
|
-
"test-query": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' ./test/run-query.js"
|
|
28
|
+
"test-query": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' ./test/run-query.js",
|
|
29
|
+
"test-query-from-pairs": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"ts-node/esm\", pathToFileURL(\"./\"));' ./test/run-query-from-pairs.js"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
31
32
|
"cors": "^2.8.5",
|