@object-ui/plugin-chatbot 0.3.0 → 0.5.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/.turbo/turbo-build.log +16 -0
- package/CHANGELOG.md +12 -0
- package/dist/index.js +31785 -360
- package/dist/index.umd.cjs +38 -2
- package/dist/src/ChatbotEnhanced.d.ts +38 -0
- package/dist/src/ChatbotEnhanced.d.ts.map +1 -0
- package/dist/src/utils.d.ts +13 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/package.json +11 -7
- package/src/ChatbotEnhanced.tsx +374 -0
- package/src/__tests__/ChatbotEnhanced.test.tsx +199 -0
- package/src/renderer.tsx +123 -2
- package/src/utils.ts +18 -0
- package/vite.config.ts +5 -0
package/src/renderer.tsx
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
import { ComponentRegistry } from '@object-ui/core';
|
|
10
10
|
import type { ChatbotSchema, ChatMessage } from '@object-ui/types';
|
|
11
11
|
import { Chatbot } from './index';
|
|
12
|
+
import { ChatbotEnhanced } from './ChatbotEnhanced';
|
|
13
|
+
import { generateUniqueId } from './utils';
|
|
12
14
|
import { useState } from 'react';
|
|
13
15
|
|
|
14
16
|
/**
|
|
@@ -41,7 +43,7 @@ ComponentRegistry.register('chatbot',
|
|
|
41
43
|
const handleSendMessage = (content: string) => {
|
|
42
44
|
// Create user message with robust ID generation
|
|
43
45
|
const userMessage: ChatMessage = {
|
|
44
|
-
id:
|
|
46
|
+
id: generateUniqueId('msg'),
|
|
45
47
|
role: 'user',
|
|
46
48
|
content,
|
|
47
49
|
timestamp: schema.showTimestamp ? new Date().toLocaleTimeString() : undefined,
|
|
@@ -59,7 +61,7 @@ ComponentRegistry.register('chatbot',
|
|
|
59
61
|
if (schema.autoResponse) {
|
|
60
62
|
setTimeout(() => {
|
|
61
63
|
const assistantMessage: ChatMessage = {
|
|
62
|
-
id:
|
|
64
|
+
id: generateUniqueId('msg'),
|
|
63
65
|
role: 'assistant',
|
|
64
66
|
content: schema.autoResponseText || 'Thank you for your message!',
|
|
65
67
|
timestamp: schema.showTimestamp ? new Date().toLocaleTimeString() : undefined,
|
|
@@ -87,6 +89,7 @@ ComponentRegistry.register('chatbot',
|
|
|
87
89
|
);
|
|
88
90
|
},
|
|
89
91
|
{
|
|
92
|
+
namespace: 'plugin-chatbot',
|
|
90
93
|
label: 'Chatbot',
|
|
91
94
|
inputs: [
|
|
92
95
|
{
|
|
@@ -191,3 +194,121 @@ ComponentRegistry.register('chatbot',
|
|
|
191
194
|
}
|
|
192
195
|
}
|
|
193
196
|
);
|
|
197
|
+
|
|
198
|
+
// Register Enhanced Chatbot
|
|
199
|
+
ComponentRegistry.register('chatbot-enhanced',
|
|
200
|
+
({ schema, className, ...props }: { schema: ChatbotSchema & {
|
|
201
|
+
enableMarkdown?: boolean;
|
|
202
|
+
enableFileUpload?: boolean;
|
|
203
|
+
onClear?: () => void;
|
|
204
|
+
}; className?: string; [key: string]: any }) => {
|
|
205
|
+
const [messages, setMessages] = useState<ChatMessage[]>(
|
|
206
|
+
schema.messages?.map((msg: any, idx: number) => ({
|
|
207
|
+
id: msg.id || `msg-${idx}`,
|
|
208
|
+
role: msg.role || 'user',
|
|
209
|
+
content: msg.content || '',
|
|
210
|
+
timestamp: typeof msg.timestamp === 'string' ? msg.timestamp : (msg.timestamp instanceof Date ? msg.timestamp.toISOString() : undefined),
|
|
211
|
+
avatar: msg.avatar,
|
|
212
|
+
avatarFallback: msg.avatarFallback,
|
|
213
|
+
})) || []
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
const handleSendMessage = (content: string, _files?: File[]) => {
|
|
217
|
+
const userMessage: ChatMessage = {
|
|
218
|
+
id: generateUniqueId('msg'),
|
|
219
|
+
role: 'user',
|
|
220
|
+
content,
|
|
221
|
+
timestamp: schema.showTimestamp ? new Date().toLocaleTimeString() : undefined,
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const updatedMessages = [...messages, userMessage];
|
|
225
|
+
setMessages(updatedMessages);
|
|
226
|
+
|
|
227
|
+
if (schema.onSend) {
|
|
228
|
+
schema.onSend(content, updatedMessages);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Auto-response with streaming simulation
|
|
232
|
+
if (schema.autoResponse) {
|
|
233
|
+
setTimeout(() => {
|
|
234
|
+
const assistantMessage: ChatMessage = {
|
|
235
|
+
id: generateUniqueId('msg'),
|
|
236
|
+
role: 'assistant',
|
|
237
|
+
content: schema.autoResponseText || 'Thank you for your message!',
|
|
238
|
+
timestamp: schema.showTimestamp ? new Date().toLocaleTimeString() : undefined,
|
|
239
|
+
};
|
|
240
|
+
setMessages((prev) => [...prev, assistantMessage]);
|
|
241
|
+
}, schema.autoResponseDelay || 1000);
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const handleClear = () => {
|
|
246
|
+
setMessages([]);
|
|
247
|
+
if (schema.onClear) {
|
|
248
|
+
schema.onClear();
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
return (
|
|
253
|
+
<ChatbotEnhanced
|
|
254
|
+
messages={messages as any}
|
|
255
|
+
placeholder={schema.placeholder}
|
|
256
|
+
onSendMessage={handleSendMessage}
|
|
257
|
+
onClear={handleClear}
|
|
258
|
+
disabled={schema.disabled}
|
|
259
|
+
showTimestamp={schema.showTimestamp}
|
|
260
|
+
userAvatarUrl={schema.userAvatarUrl}
|
|
261
|
+
userAvatarFallback={schema.userAvatarFallback}
|
|
262
|
+
assistantAvatarUrl={schema.assistantAvatarUrl}
|
|
263
|
+
assistantAvatarFallback={schema.assistantAvatarFallback}
|
|
264
|
+
maxHeight={schema.maxHeight}
|
|
265
|
+
enableMarkdown={schema.enableMarkdown ?? true}
|
|
266
|
+
enableFileUpload={schema.enableFileUpload ?? false}
|
|
267
|
+
className={className}
|
|
268
|
+
{...props}
|
|
269
|
+
/>
|
|
270
|
+
);
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
namespace: 'plugin-chatbot',
|
|
274
|
+
label: 'Chatbot (Enhanced)',
|
|
275
|
+
inputs: [
|
|
276
|
+
{ name: 'messages', type: 'array', label: 'Initial Messages' },
|
|
277
|
+
{ name: 'placeholder', type: 'string', label: 'Input Placeholder', defaultValue: 'Type your message...' },
|
|
278
|
+
{ name: 'showTimestamp', type: 'boolean', label: 'Show Timestamps', defaultValue: false },
|
|
279
|
+
{ name: 'disabled', type: 'boolean', label: 'Disabled', defaultValue: false },
|
|
280
|
+
{ name: 'enableMarkdown', type: 'boolean', label: 'Enable Markdown', defaultValue: true },
|
|
281
|
+
{ name: 'enableFileUpload', type: 'boolean', label: 'Enable File Upload', defaultValue: false },
|
|
282
|
+
{ name: 'userAvatarUrl', type: 'string', label: 'User Avatar URL' },
|
|
283
|
+
{ name: 'userAvatarFallback', type: 'string', label: 'User Avatar Fallback', defaultValue: 'You' },
|
|
284
|
+
{ name: 'assistantAvatarUrl', type: 'string', label: 'Assistant Avatar URL' },
|
|
285
|
+
{ name: 'assistantAvatarFallback', type: 'string', label: 'Assistant Avatar Fallback', defaultValue: 'AI' },
|
|
286
|
+
{ name: 'maxHeight', type: 'string', label: 'Max Height', defaultValue: '500px' },
|
|
287
|
+
{ name: 'autoResponse', type: 'boolean', label: 'Enable Auto Response (Demo)', defaultValue: false },
|
|
288
|
+
{ name: 'autoResponseText', type: 'string', label: 'Auto Response Text', defaultValue: 'Thank you for your message!' },
|
|
289
|
+
{ name: 'autoResponseDelay', type: 'number', label: 'Auto Response Delay (ms)', defaultValue: 1000 },
|
|
290
|
+
{ name: 'className', type: 'string', label: 'CSS Class' }
|
|
291
|
+
],
|
|
292
|
+
defaultProps: {
|
|
293
|
+
messages: [
|
|
294
|
+
{
|
|
295
|
+
id: 'welcome',
|
|
296
|
+
role: 'assistant',
|
|
297
|
+
content: 'Hello! How can I help you today?',
|
|
298
|
+
}
|
|
299
|
+
],
|
|
300
|
+
placeholder: 'Type your message...',
|
|
301
|
+
showTimestamp: false,
|
|
302
|
+
disabled: false,
|
|
303
|
+
enableMarkdown: true,
|
|
304
|
+
enableFileUpload: false,
|
|
305
|
+
userAvatarFallback: 'You',
|
|
306
|
+
assistantAvatarFallback: 'AI',
|
|
307
|
+
maxHeight: '500px',
|
|
308
|
+
autoResponse: true,
|
|
309
|
+
autoResponseText: 'Thank you for your message! This is an automated response.',
|
|
310
|
+
autoResponseDelay: 1000,
|
|
311
|
+
className: 'w-full max-w-2xl'
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
);
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Generates a unique ID for messages or other entities
|
|
11
|
+
* Uses crypto.randomUUID() if available, otherwise falls back to timestamp + random string
|
|
12
|
+
*/
|
|
13
|
+
export function generateUniqueId(prefix = 'msg'): string {
|
|
14
|
+
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
|
|
15
|
+
return crypto.randomUUID();
|
|
16
|
+
}
|
|
17
|
+
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
18
|
+
}
|