@schmitech/chatbot-api 0.1.0 → 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 +131 -0
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -58,6 +58,36 @@ To make this library available to anyone via npm:
|
|
|
58
58
|
npm publish
|
|
59
59
|
```
|
|
60
60
|
|
|
61
|
+
### Option 4: Using via CDN (for websites)
|
|
62
|
+
|
|
63
|
+
You can include the library directly in your website using jsDelivr CDN:
|
|
64
|
+
|
|
65
|
+
```html
|
|
66
|
+
<!-- For ESM (modern browsers) -->
|
|
67
|
+
<script type="module">
|
|
68
|
+
import { configureApi, streamChat } from 'https://cdn.jsdelivr.net/npm/@schmitech/chatbot-api@0.1.0/dist/api.mjs';
|
|
69
|
+
|
|
70
|
+
// Configure the API with your server URL
|
|
71
|
+
configureApi('https://your-api-server.com');
|
|
72
|
+
|
|
73
|
+
// Use the API functions
|
|
74
|
+
async function handleChat() {
|
|
75
|
+
for await (const response of streamChat('Hello', false)) {
|
|
76
|
+
console.log(response.text);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
</script>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
For production use, you can omit the version to always get the latest:
|
|
83
|
+
|
|
84
|
+
```html
|
|
85
|
+
<script type="module">
|
|
86
|
+
import { configureApi, streamChat } from 'https://cdn.jsdelivr.net/npm/@schmitech/chatbot-api/dist/api.mjs';
|
|
87
|
+
// ...
|
|
88
|
+
</script>
|
|
89
|
+
```
|
|
90
|
+
|
|
61
91
|
## Usage
|
|
62
92
|
|
|
63
93
|
### Configuration (Required)
|
|
@@ -208,6 +238,107 @@ function ChatComponent() {
|
|
|
208
238
|
export default ChatComponent;
|
|
209
239
|
```
|
|
210
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
|
+
|
|
211
342
|
## API Reference
|
|
212
343
|
|
|
213
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.1
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"description": "API client for the Chatbot server",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/api.cjs",
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
12
|
"import": "./dist/api.mjs",
|
|
13
|
-
"require": "./dist/api.cjs"
|
|
13
|
+
"require": "./dist/api.cjs",
|
|
14
|
+
"types": "./api.d.ts"
|
|
14
15
|
}
|
|
15
16
|
},
|
|
16
17
|
"files": [
|
|
@@ -24,7 +25,8 @@
|
|
|
24
25
|
"preview": "vite preview",
|
|
25
26
|
"test": "vitest",
|
|
26
27
|
"test:watch": "vitest",
|
|
27
|
-
"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"
|
|
28
30
|
},
|
|
29
31
|
"dependencies": {
|
|
30
32
|
"cors": "^2.8.5",
|