-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreact.html
More file actions
274 lines (238 loc) · 8.48 KB
/
Copy pathreact.html
File metadata and controls
274 lines (238 loc) · 8.48 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Kit CDN Example - React</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 2rem;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
border-radius: 12px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
padding: 2rem;
}
h1 {
color: #333;
margin-bottom: 0.5rem;
}
.badge {
display: inline-block;
background: #667eea;
color: white;
padding: 0.25rem 0.75rem;
border-radius: 12px;
font-size: 0.875rem;
margin-bottom: 1.5rem;
}
.chat-container {
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 1rem;
margin-bottom: 1rem;
min-height: 300px;
max-height: 500px;
overflow-y: auto;
background: #f7fafc;
}
.message {
margin-bottom: 1rem;
padding: 0.75rem;
border-radius: 8px;
}
.message.user {
background: #667eea;
color: white;
margin-left: 2rem;
}
.message.assistant {
background: white;
border: 1px solid #e2e8f0;
margin-right: 2rem;
}
.input-group {
display: flex;
gap: 0.5rem;
}
input {
flex: 1;
padding: 0.75rem;
border: 1px solid #e2e8f0;
border-radius: 8px;
font-size: 1rem;
}
button {
padding: 0.75rem 1.5rem;
background: #667eea;
color: white;
border: none;
border-radius: 8px;
font-size: 1rem;
cursor: pointer;
transition: background 0.2s;
}
button:hover {
background: #5568d3;
}
button:disabled {
background: #cbd5e0;
cursor: not-allowed;
}
.info {
margin-top: 2rem;
padding: 1rem;
background: #f7fafc;
border-left: 4px solid #667eea;
border-radius: 4px;
}
.info h3 {
color: #667eea;
margin-bottom: 0.5rem;
}
.info code {
background: #edf2f7;
padding: 0.125rem 0.375rem;
border-radius: 3px;
font-family: 'Monaco', 'Courier New', monospace;
font-size: 0.875rem;
}
.loading {
color: #718096;
font-style: italic;
}
</style>
</head>
<body>
<div id="root"></div>
<!-- Load React and ReactDOM from CDN -->
<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
<!-- Load AI Kit Core from CDN -->
<script src="https://cdn.jsdelivr.net/npm/@ainative/ai-kit-core@latest/dist/cdn/core.min.js"
crossorigin="anonymous"></script>
<!-- Load AI Kit React from CDN -->
<script src="https://cdn.jsdelivr.net/npm/@ainative/ai-kit@latest/dist/cdn/react.min.js"
crossorigin="anonymous"></script>
<!-- Babel Standalone for JSX transformation (development only) -->
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
const { useState, useRef, useEffect } = React;
function ChatMessage({ message, isUser }) {
return (
<div className={`message ${isUser ? 'user' : 'assistant'}`}>
{message}
</div>
);
}
function ChatApp() {
const [messages, setMessages] = useState([
{ text: 'Welcome to AI Kit React! This example uses CDN bundles without any build step.', isUser: false }
]);
const [input, setInput] = useState('');
const [isLoading, setIsLoading] = useState(false);
const chatContainerRef = useRef(null);
useEffect(() => {
if (chatContainerRef.current) {
chatContainerRef.current.scrollTop = chatContainerRef.current.scrollHeight;
}
}, [messages]);
const sendMessage = async () => {
const text = input.trim();
if (!text) return;
// Add user message
setMessages(prev => [...prev, { text, isUser: true }]);
setInput('');
setIsLoading(true);
try {
// Simulate AI response
await new Promise(resolve => setTimeout(resolve, 1000));
// Add assistant response
setMessages(prev => [...prev, {
text: `Echo: ${text}`,
isUser: false
}]);
} catch (error) {
console.error('Error:', error);
setMessages(prev => [...prev, {
text: 'Error: Failed to get response',
isUser: false
}]);
} finally {
setIsLoading(false);
}
};
const handleKeyPress = (e) => {
if (e.key === 'Enter') {
sendMessage();
}
};
return (
<div className="container">
<h1>AI Kit CDN Example</h1>
<span className="badge">React + CDN</span>
<div className="chat-container" ref={chatContainerRef}>
{messages.map((msg, idx) => (
<ChatMessage
key={idx}
message={msg.text}
isUser={msg.isUser}
/>
))}
{isLoading && (
<div className="message assistant loading">
Thinking...
</div>
)}
</div>
<div className="input-group">
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyPress={handleKeyPress}
placeholder="Type your message..."
disabled={isLoading}
/>
<button onClick={sendMessage} disabled={isLoading}>
Send
</button>
</div>
<div className="info">
<h3>Using AI Kit React from CDN</h3>
<p>This example loads React and AI Kit from CDN without any build tools.</p>
<p style={{ marginTop: '0.5rem' }}>
<strong>Loaded:</strong>{' '}
<code>React</code>,{' '}
<code>AIKitCore</code>,{' '}
<code>AIKitReact</code>
</p>
<p style={{ marginTop: '0.5rem', fontSize: '0.875rem', color: '#718096' }}>
Note: In production, compile JSX using a build tool for better performance.
</p>
</div>
</div>
);
}
// Render the app
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<ChatApp />);
console.log('AI Kit loaded:', {
React: typeof React !== 'undefined',
AIKitCore: typeof AIKitCore !== 'undefined',
AIKitReact: typeof AIKitReact !== 'undefined'
});
</script>
</body>
</html>