kooby 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 +429 -0
- package/dist/cjs/index.js +883 -0
- package/dist/esm/index.js +881 -0
- package/package.json +71 -0
package/README.md
ADDED
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
# Kooby
|
|
2
|
+
|
|
3
|
+
A simple Chatbot component that uses WebSockets to interact with a backend server
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```jsx
|
|
8
|
+
// Import the kooby component
|
|
9
|
+
import { Kooby } from "kooby";
|
|
10
|
+
|
|
11
|
+
const MyComponent = () => {
|
|
12
|
+
return (
|
|
13
|
+
<Kooby url="ws://localhost:8000">
|
|
14
|
+
<Kooby.Toolbar />
|
|
15
|
+
<Kooby.Conversation />
|
|
16
|
+
<Kooby.ConnectionStatus />
|
|
17
|
+
<Kooby.TextBox />
|
|
18
|
+
</Kooby>
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## With Auth Token ( JWT )
|
|
24
|
+
|
|
25
|
+
```jsx
|
|
26
|
+
// Import the kooby component
|
|
27
|
+
import { Kooby } from "kooby";
|
|
28
|
+
|
|
29
|
+
const MyComponent = () => {
|
|
30
|
+
return (
|
|
31
|
+
<Kooby url="ws://localhost:8000" token="asdf...">
|
|
32
|
+
<Kooby.Toolbar />
|
|
33
|
+
<Kooby.Conversation />
|
|
34
|
+
<Kooby.ConnectionStatus />
|
|
35
|
+
<Kooby.TextBox />
|
|
36
|
+
</Kooby>
|
|
37
|
+
);
|
|
38
|
+
};
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## With Only URL
|
|
42
|
+
|
|
43
|
+
```jsx
|
|
44
|
+
// Import the kooby component
|
|
45
|
+
import { Kooby } from "kooby";
|
|
46
|
+
|
|
47
|
+
const MyComponent = () => {
|
|
48
|
+
return (
|
|
49
|
+
<Kooby url="ws://localhost:8000">
|
|
50
|
+
<Kooby.Toolbar />
|
|
51
|
+
<Kooby.Conversation />
|
|
52
|
+
<Kooby.ConnectionStatus />
|
|
53
|
+
<Kooby.TextBox />
|
|
54
|
+
</Kooby>
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## With metadata
|
|
60
|
+
|
|
61
|
+
```jsx
|
|
62
|
+
import { Kooby } from "kooby";
|
|
63
|
+
|
|
64
|
+
const MyComponent = () => {
|
|
65
|
+
return (
|
|
66
|
+
<Kooby
|
|
67
|
+
url="ws://localhost:8000"
|
|
68
|
+
metadata={{
|
|
69
|
+
user: "Joe",
|
|
70
|
+
topic: "Chargers",
|
|
71
|
+
project: "digital-experience/ocw-barker",
|
|
72
|
+
}}
|
|
73
|
+
>
|
|
74
|
+
<Kooby.Toolbar />
|
|
75
|
+
<Kooby.Conversation />
|
|
76
|
+
<Kooby.ConnectionStatus />
|
|
77
|
+
<Kooby.TextBox />
|
|
78
|
+
</Kooby>
|
|
79
|
+
);
|
|
80
|
+
};
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## When not expandable
|
|
84
|
+
|
|
85
|
+
```jsx
|
|
86
|
+
// Import the kooby component
|
|
87
|
+
import { Kooby } from "kooby";
|
|
88
|
+
|
|
89
|
+
const MyComponent = () => {
|
|
90
|
+
return (
|
|
91
|
+
<Kooby url="ws://localhost:8000">
|
|
92
|
+
<Kooby.Toolbar expandable={false} />
|
|
93
|
+
<Kooby.Conversation />
|
|
94
|
+
<Kooby.ConnectionStatus />
|
|
95
|
+
<Kooby.TextBox />
|
|
96
|
+
</Kooby>
|
|
97
|
+
);
|
|
98
|
+
};
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Always Expanded
|
|
102
|
+
|
|
103
|
+
Will always take up fill width of parent container 100%
|
|
104
|
+
|
|
105
|
+
```jsx
|
|
106
|
+
// Import the kooby component
|
|
107
|
+
import { Kooby } from "kooby";
|
|
108
|
+
|
|
109
|
+
const MyComponent = () => {
|
|
110
|
+
return (
|
|
111
|
+
<Kooby url="ws://localhost:8000" expanded>
|
|
112
|
+
<Kooby.Toolbar />
|
|
113
|
+
<Kooby.Conversation />
|
|
114
|
+
<Kooby.ConnectionStatus />
|
|
115
|
+
<Kooby.TextBox />
|
|
116
|
+
</Kooby>
|
|
117
|
+
);
|
|
118
|
+
};
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## When you want to hide the reset button
|
|
122
|
+
|
|
123
|
+
```jsx
|
|
124
|
+
// Import the kooby component
|
|
125
|
+
import { Kooby } from "kooby";
|
|
126
|
+
|
|
127
|
+
const MyComponent = () => {
|
|
128
|
+
return (
|
|
129
|
+
<Kooby url="ws://localhost:8000">
|
|
130
|
+
<Kooby.Toolbar reset={false} />
|
|
131
|
+
<Kooby.Conversation />
|
|
132
|
+
<Kooby.ConnectionStatus />
|
|
133
|
+
<Kooby.TextBox />
|
|
134
|
+
</Kooby>
|
|
135
|
+
);
|
|
136
|
+
};
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## With History ( seeding the conversation )
|
|
140
|
+
|
|
141
|
+
```jsx
|
|
142
|
+
// Import the kooby component
|
|
143
|
+
import { Kooby } from "kooby";
|
|
144
|
+
|
|
145
|
+
const MyComponent = () => {
|
|
146
|
+
return (
|
|
147
|
+
<Kooby
|
|
148
|
+
url="ws://localhost:8000"
|
|
149
|
+
conversation={[
|
|
150
|
+
{
|
|
151
|
+
role: "user",
|
|
152
|
+
content: "Hi, I would like to know about the barker project",
|
|
153
|
+
},
|
|
154
|
+
]}
|
|
155
|
+
>
|
|
156
|
+
<Kooby.Toolbar />
|
|
157
|
+
<Kooby.Conversation />
|
|
158
|
+
<Kooby.ConnectionStatus />
|
|
159
|
+
<Kooby.TextBox />
|
|
160
|
+
</Kooby>
|
|
161
|
+
);
|
|
162
|
+
};
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Custom Chat Components
|
|
166
|
+
|
|
167
|
+
Kooby allows you to customize what gets output by your agent. You can build custom React components that will get rendered when your agent returns specific keywords.
|
|
168
|
+
|
|
169
|
+
### Example: ( Mermaid Diagrams )
|
|
170
|
+
|
|
171
|
+
When the agent returns text like this you may want Kooby to render that as a diagram:
|
|
172
|
+
|
|
173
|
+
```
|
|
174
|
+
<mermaid>
|
|
175
|
+
graph TD
|
|
176
|
+
A[Start] -->|Step 1| B[Process]
|
|
177
|
+
B -->|Step 2| C[End]
|
|
178
|
+
</mermaid>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
In order to get its contents to render inside of your custom `Mermaid.jsx` component, you simply need to register your component like so:
|
|
182
|
+
|
|
183
|
+
```jsx
|
|
184
|
+
import { Kooby } from "kooby";
|
|
185
|
+
import { Mermaid } from "./Mermaid.jsx";
|
|
186
|
+
import { QR } from "./QR.jsx";
|
|
187
|
+
|
|
188
|
+
const mdjsx = {
|
|
189
|
+
overrides: {
|
|
190
|
+
mermaid: {
|
|
191
|
+
component: Mermaid,
|
|
192
|
+
},
|
|
193
|
+
qr: {
|
|
194
|
+
component: QR,
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const MyComponent = () => {
|
|
200
|
+
return (
|
|
201
|
+
<Kooby url="ws://localhost:8000">
|
|
202
|
+
<Kooby.Toolbar />
|
|
203
|
+
<Kooby.Conversation mdjsx={mdjsx} />
|
|
204
|
+
<Kooby.ConnectionStatus />
|
|
205
|
+
<Kooby.TextBox />
|
|
206
|
+
</Kooby>
|
|
207
|
+
);
|
|
208
|
+
};
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## Custom Toolbar
|
|
214
|
+
|
|
215
|
+
Kooby allows you to customize what gets rendered in the toolbar (at the top of the Kooby window).
|
|
216
|
+
|
|
217
|
+
```jsx
|
|
218
|
+
import { Kooby } from "kooby";
|
|
219
|
+
|
|
220
|
+
const CustomToolbar = ({ agent, conversation }) => {
|
|
221
|
+
const copyConversation = () => {
|
|
222
|
+
const conversationJson = JSON.stringify(conversation, null, 2);
|
|
223
|
+
navigator.clipboard.writeText(conversationJson);
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<button
|
|
228
|
+
type="button"
|
|
229
|
+
onClick={copyConversation}
|
|
230
|
+
aria-label="Copy conversation"
|
|
231
|
+
>
|
|
232
|
+
Copy
|
|
233
|
+
</button>
|
|
234
|
+
);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const MyComponent = () => {
|
|
238
|
+
return (
|
|
239
|
+
<Kooby url="ws://localhost:8000">
|
|
240
|
+
<Kooby.Toolbar>
|
|
241
|
+
{({ conversation }) => <CustomToolbar conversation={conversation} />}
|
|
242
|
+
</Kooby.Toolbar>
|
|
243
|
+
<Kooby.Conversation />
|
|
244
|
+
<Kooby.ConnectionStatus />
|
|
245
|
+
<Kooby.TextBox />
|
|
246
|
+
</Kooby>
|
|
247
|
+
);
|
|
248
|
+
};
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
## Kooby Api
|
|
254
|
+
|
|
255
|
+
You can get access to the internal kooby api via a ref
|
|
256
|
+
|
|
257
|
+
```jsx
|
|
258
|
+
// Import useRef
|
|
259
|
+
import React, { useRef } from "react";
|
|
260
|
+
|
|
261
|
+
// Import the kooby component
|
|
262
|
+
import { Kooby } from "kooby";
|
|
263
|
+
|
|
264
|
+
const MyComponent = () => {
|
|
265
|
+
const koobyApiRef = useRef();
|
|
266
|
+
|
|
267
|
+
return (
|
|
268
|
+
<>
|
|
269
|
+
<Button
|
|
270
|
+
onClick={() => {
|
|
271
|
+
koobyApiRef.current?.resetConversation();
|
|
272
|
+
}}
|
|
273
|
+
variant="secondary"
|
|
274
|
+
>
|
|
275
|
+
Reset
|
|
276
|
+
</Button>
|
|
277
|
+
<Kooby url="ws://localhost:8000" apiRef={koobyApiRef}>
|
|
278
|
+
<Kooby.Toolbar />
|
|
279
|
+
<Kooby.Conversation />
|
|
280
|
+
<Kooby.ConnectionStatus />
|
|
281
|
+
<Kooby.TextBox />
|
|
282
|
+
</Kooby>
|
|
283
|
+
</>
|
|
284
|
+
);
|
|
285
|
+
};
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
## Sending Feedback
|
|
291
|
+
|
|
292
|
+
```jsx
|
|
293
|
+
// Import the kooby component
|
|
294
|
+
import { Kooby } from "kooby";
|
|
295
|
+
|
|
296
|
+
const MyComponent = () => {
|
|
297
|
+
return (
|
|
298
|
+
<Kooby url="ws://localhost:8000">
|
|
299
|
+
<Kooby.Toolbar />
|
|
300
|
+
<Kooby.Conversation
|
|
301
|
+
negative={{
|
|
302
|
+
onSubmit: (feedback) => {
|
|
303
|
+
console.log(JSON.stringify(feedback, null, 2));
|
|
304
|
+
},
|
|
305
|
+
}}
|
|
306
|
+
positive={{
|
|
307
|
+
onSubmit: (feedback) => {
|
|
308
|
+
console.log(JSON.stringify(feedback, null, 2));
|
|
309
|
+
},
|
|
310
|
+
}}
|
|
311
|
+
/>
|
|
312
|
+
<Kooby.ConnectionStatus />
|
|
313
|
+
<Kooby.TextBox />
|
|
314
|
+
</Kooby>
|
|
315
|
+
);
|
|
316
|
+
};
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
Example Output from negative feedback log
|
|
320
|
+
|
|
321
|
+
```json
|
|
322
|
+
{
|
|
323
|
+
"feedback": "Wish it just told me what was new!",
|
|
324
|
+
"message": "Are you asking about new models, features, or updates? Could you please specify?",
|
|
325
|
+
"conversation": [
|
|
326
|
+
{
|
|
327
|
+
"role": "system",
|
|
328
|
+
"content": "Hello, I'm Test! I'm here to help you with any questions you may have."
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
"role": "user",
|
|
332
|
+
"content": "Hi!"
|
|
333
|
+
},
|
|
334
|
+
{
|
|
335
|
+
"role": "assistant",
|
|
336
|
+
"content": "Hello! How can I assist you today?"
|
|
337
|
+
},
|
|
338
|
+
{
|
|
339
|
+
"role": "user",
|
|
340
|
+
"content": "What new ? "
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
"role": "assistant",
|
|
344
|
+
"content": "Are you asking about new models, features, or updates? Could you please specify?"
|
|
345
|
+
}
|
|
346
|
+
]
|
|
347
|
+
}
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
## Showing feedback on all messages
|
|
351
|
+
|
|
352
|
+
By default feedback will only be shown on the most recent message. By passing `showFeedbackOnAllMessages` you can change that behavior.
|
|
353
|
+
|
|
354
|
+
```jsx
|
|
355
|
+
// Import the kooby component
|
|
356
|
+
import { Kooby } from "kooby";
|
|
357
|
+
|
|
358
|
+
const MyComponent = () => {
|
|
359
|
+
return (
|
|
360
|
+
<Kooby url="ws://localhost:8000">
|
|
361
|
+
<Kooby.Toolbar />
|
|
362
|
+
<Kooby.Conversation
|
|
363
|
+
showFeedbackOnAllMessages
|
|
364
|
+
negative={{
|
|
365
|
+
onSubmit: (feedback) => {
|
|
366
|
+
console.log(JSON.stringify(feedback, null, 2));
|
|
367
|
+
},
|
|
368
|
+
}}
|
|
369
|
+
positive={{
|
|
370
|
+
onSubmit: (feedback) => {
|
|
371
|
+
console.log(JSON.stringify(feedback, null, 2));
|
|
372
|
+
},
|
|
373
|
+
}}
|
|
374
|
+
/>
|
|
375
|
+
<Kooby.ConnectionStatus />
|
|
376
|
+
<Kooby.TextBox />
|
|
377
|
+
</Kooby>
|
|
378
|
+
);
|
|
379
|
+
};
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
---
|
|
383
|
+
|
|
384
|
+
## Project Layout
|
|
385
|
+
|
|
386
|
+
client - a simple react app with a chat window ( using the exported Kooby react component )
|
|
387
|
+
server - a simple js express server that serves up the chatbot and connects to the chat api
|
|
388
|
+
|
|
389
|
+
## Getting It Running
|
|
390
|
+
|
|
391
|
+
#### Step1: First you need to make sure you have node installed on your computer
|
|
392
|
+
|
|
393
|
+
Visit this site [HERE](https://nodejs.org/en/download)
|
|
394
|
+
|
|
395
|
+
### Step2: you need to install the dependencies
|
|
396
|
+
|
|
397
|
+
```bash
|
|
398
|
+
npm i
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
This will install all the dependencies
|
|
402
|
+
|
|
403
|
+
### Step3: Add required secrets
|
|
404
|
+
|
|
405
|
+
Get chat token and put it in the following file in the root of this project ( used grok in example )
|
|
406
|
+
|
|
407
|
+
```bash
|
|
408
|
+
touch token_grok.txt
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
### Step4: Run it!
|
|
412
|
+
|
|
413
|
+
```bash
|
|
414
|
+
npm run start:dev
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
This will start a express web server with basic chatbot and also a sample front end react app that connects to the servers endpoint
|
|
418
|
+
|
|
419
|
+
Go [here](http://localhost:3000/) to view the frontend example
|
|
420
|
+
|
|
421
|
+
```
|
|
422
|
+
http://localhost:3000/
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
### WebSocket URL (example app)
|
|
426
|
+
|
|
427
|
+
```
|
|
428
|
+
ws://localhost:3000/kooby
|
|
429
|
+
```
|