-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchatbot.tsx
More file actions
185 lines (179 loc) · 5.56 KB
/
chatbot.tsx
File metadata and controls
185 lines (179 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { ComponentRegistry } from '@object-ui/core';
import type { ChatbotSchema, ChatMessage } from '@object-ui/types';
import { Chatbot } from '@/ui';
import { useState } from 'react';
/**
* Chatbot component for Object UI
*
* @remarks
* This component supports an optional `onSend` callback in the schema:
* - Signature: `onSend(content: string, messages: ChatMessage[]): void`
* - Parameters:
* - content: The message text that was sent
* - messages: Array of all messages including the newly added message
* - Called when: User sends a message via the input field
* - Use case: Connect to backend API or trigger custom actions on message send
*/
ComponentRegistry.register('chatbot',
({ schema, className, ...props }) => {
// Initialize messages from schema or use empty array
const [messages, setMessages] = useState<ChatMessage[]>(
schema.messages?.map((msg: Partial<ChatMessage>, idx: number) => ({
id: msg.id || `msg-${idx}`,
role: msg.role || 'user',
content: msg.content || '',
timestamp: msg.timestamp,
avatar: msg.avatar,
avatarFallback: msg.avatarFallback,
})) || []
);
// Handle sending new messages
const handleSendMessage = (content: string) => {
// Create user message with robust ID generation
const userMessage: ChatMessage = {
id: crypto.randomUUID(),
role: 'user',
content,
timestamp: schema.showTimestamp ? new Date().toLocaleTimeString() : undefined,
};
const updatedMessages = [...messages, userMessage];
setMessages(updatedMessages);
// If onSend callback is provided in schema, call it with updated messages
if (schema.onSend) {
schema.onSend(content, updatedMessages);
}
// Auto-response feature for demo purposes
if (schema.autoResponse) {
setTimeout(() => {
const assistantMessage: ChatMessage = {
id: crypto.randomUUID(),
role: 'assistant',
content: schema.autoResponseText || 'Thank you for your message!',
timestamp: schema.showTimestamp ? new Date().toLocaleTimeString() : undefined,
};
setMessages((prev) => [...prev, assistantMessage]);
}, schema.autoResponseDelay || 1000);
}
};
return (
<Chatbot
messages={messages}
placeholder={schema.placeholder}
onSendMessage={handleSendMessage}
disabled={schema.disabled}
showTimestamp={schema.showTimestamp}
userAvatarUrl={schema.userAvatarUrl}
userAvatarFallback={schema.userAvatarFallback}
assistantAvatarUrl={schema.assistantAvatarUrl}
assistantAvatarFallback={schema.assistantAvatarFallback}
maxHeight={schema.maxHeight}
className={className}
{...props}
/>
);
},
{
label: 'Chatbot',
inputs: [
{
name: 'messages',
type: 'array',
label: 'Initial Messages',
description: 'Array of message objects with id, role, content, and optional timestamp'
},
{
name: 'placeholder',
type: 'string',
label: 'Input Placeholder',
defaultValue: 'Type your message...'
},
{
name: 'showTimestamp',
type: 'boolean',
label: 'Show Timestamps',
defaultValue: false
},
{
name: 'disabled',
type: 'boolean',
label: 'Disabled',
defaultValue: false
},
{
name: 'userAvatarUrl',
type: 'string',
label: 'User Avatar URL',
description: 'URL of the user avatar image'
},
{
name: 'userAvatarFallback',
type: 'string',
label: 'User Avatar Fallback',
defaultValue: 'You',
description: 'Fallback text shown when user avatar image is not available'
},
{
name: 'assistantAvatarUrl',
type: 'string',
label: 'Assistant Avatar URL',
description: 'URL of the assistant avatar image'
},
{
name: 'assistantAvatarFallback',
type: 'string',
label: 'Assistant Avatar Fallback',
defaultValue: 'AI',
description: 'Fallback text shown when assistant avatar image is not available'
},
{
name: 'maxHeight',
type: 'string',
label: 'Max Height',
defaultValue: '500px'
},
{
name: 'autoResponse',
type: 'boolean',
label: 'Enable Auto Response (Demo)',
defaultValue: false,
description: 'Automatically send a response after user message (for demo purposes)'
},
{
name: 'autoResponseText',
type: 'string',
label: 'Auto Response Text',
defaultValue: 'Thank you for your message!'
},
{
name: 'autoResponseDelay',
type: 'number',
label: 'Auto Response Delay (ms)',
defaultValue: 1000
},
{
name: 'className',
type: 'string',
label: 'CSS Class'
}
],
defaultProps: {
messages: [
{
id: 'welcome',
role: 'assistant',
content: 'Hello! How can I help you today?',
}
],
placeholder: 'Type your message...',
showTimestamp: false,
disabled: false,
userAvatarFallback: 'You',
assistantAvatarFallback: 'AI',
maxHeight: '500px',
autoResponse: true,
autoResponseText: 'Thank you for your message! This is an automated response.',
autoResponseDelay: 1000,
className: 'w-full max-w-2xl'
}
}
);