forked from langwatch/better-agents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
363 lines (340 loc) · 12.5 KB
/
App.tsx
File metadata and controls
363 lines (340 loc) · 12.5 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import {
CopilotKit,
useCopilotAction,
type CatchAllActionRenderProps,
} from "@copilotkit/react-core";
import { CopilotChat } from "@copilotkit/react-ui";
import "@copilotkit/react-ui/styles.css";
import ReactMarkdown from "react-markdown";
import { useState } from "react";
import type {
DocumentResponse,
OrderSummaryResponse,
} from "@langwatch/create-agent-app";
function App() {
return (
<CopilotKit runtimeUrl="/copilotkit" agent="customerSupportAgent">
<CustomerSupportChat />
</CopilotKit>
);
}
function CustomerSupportChat() {
useCopilotAction({
name: "*",
render: (({ name, status, result }: CatchAllActionRenderProps) => {
if (status === "executing") {
return (
<div className="flex items-center justify-center p-6 bg-gradient-to-r from-blue-50 to-indigo-50 rounded-lg border border-blue-200 shadow-sm">
<div className="flex items-center space-x-3">
<div className="animate-spin rounded-full h-6 w-6 border-b-2 border-blue-600"></div>
<span className="text-blue-700 font-medium">
Calling tool: {name}
</span>
</div>
</div>
);
}
if (status === "complete") {
if (name === "getCustomerOrderHistory") {
return <OrderHistoryTable orders={result} />;
}
if (
name === "getCompanyPolicy" ||
name === "getTroubleshootingGuide"
) {
return <DocumentViewer document={result} />;
}
if (name === "escalateToHuman") {
return <EscalationModal />;
}
}
return null;
}) as (props: CatchAllActionRenderProps) => React.ReactElement,
});
return (
<div className="max-w-2xl mx-auto">
<CopilotChat
labels={{
title: "Your Assistant",
initial: "Hi! 👋 How can I assist you today?",
}}
/>
</div>
);
}
const EscalationModal = () => {
const [isModalOpen, setIsModalOpen] = useState(true);
const handleConfirmEscalation = () => {
// Here you would typically call an API to escalate to human support
alert(
"Your request has been escalated to human support. A representative will be with you shortly."
);
setIsModalOpen(false);
};
const handleCancel = () => {
setIsModalOpen(false);
};
if (!isModalOpen) return null;
return (
<div className="fixed inset-0 bg-[#00000060] flex items-center justify-center z-50 p-4">
<div className="bg-white rounded-lg shadow-xl max-w-md w-full mx-4 overflow-hidden">
<div className="bg-gradient-to-r from-orange-500 to-red-500 px-6 py-4">
<div className="flex items-center space-x-3">
<svg
className="w-6 h-6 text-white"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M17 8h2a2 2 0 012 2v6a2 2 0 01-2 2h-2v4l-4-4H9a2 2 0 01-2-2v-6a2 2 0 012-2h8z"
/>
</svg>
<h3 className="text-lg font-semibold text-white">
Connect with Human Support
</h3>
</div>
<p className="text-orange-100 text-sm mt-1">
Get help from our support team
</p>
</div>
<div className="px-6 py-6">
<div className="mb-6">
<p className="text-gray-700 mb-4">
Our AI assistant has determined that your request would be best
handled by a human representative. Our support team is standing by
to provide personalized assistance.
</p>
<div className="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-4">
<div className="flex items-center space-x-2">
<svg
className="w-5 h-5 text-blue-500"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
<p className="text-blue-800 text-sm font-medium">
A support representative will be with you shortly
</p>
</div>
</div>
</div>
<div className="flex space-x-3">
<button
onClick={handleConfirmEscalation}
className="flex-1 bg-gradient-to-r from-orange-500 to-red-500 text-white font-medium py-3 px-4 rounded-lg hover:from-orange-600 hover:to-red-600 transition-all duration-200 flex items-center justify-center space-x-2"
>
<svg
className="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M17 8h2a2 2 0 012 2v6a2 2 0 01-2 2h-2v4l-4-4H9a2 2 0 01-2-2v-6a2 2 0 012-2h8z"
/>
</svg>
<span>Connect Now</span>
</button>
<button
onClick={handleCancel}
className="flex-1 bg-gray-100 text-gray-700 font-medium py-3 px-4 rounded-lg hover:bg-gray-200 transition-colors duration-200"
>
Continue Chat
</button>
</div>
</div>
</div>
</div>
);
};
const OrderHistoryTable = ({ orders }: { orders: OrderSummaryResponse[] }) => {
return (
<div className="bg-white rounded-lg shadow-lg border border-gray-200 overflow-hidden">
<div className="bg-gradient-to-r from-blue-600 to-indigo-600 px-6 py-4">
<h3 className="text-lg font-semibold text-white">Order History</h3>
<p className="text-blue-100 text-sm mt-1">Customer's recent orders</p>
</div>
<div className="overflow-x-auto">
<table className="min-w-full divide-y divide-gray-200">
<thead className="bg-gray-50">
<tr>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Order ID
</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Total Amount
</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Order Date
</th>
</tr>
</thead>
<tbody className="bg-white divide-y divide-gray-200">
{orders.map((order: OrderSummaryResponse, index: number) => (
<tr
key={order.orderId}
className={`hover:bg-gray-50 transition-colors duration-150 ${
index % 2 === 0 ? "bg-white" : "bg-gray-25"
}`}
>
<td className="px-6 py-4 whitespace-nowrap">
<div className="text-sm font-medium text-gray-900">
{order.orderId}
</div>
</td>
<td className="px-6 py-4 whitespace-nowrap">
<div className="text-sm font-semibold text-green-600">
{order.totalAmount}
</div>
</td>
<td className="px-6 py-4 whitespace-nowrap">
<div className="text-sm text-gray-600">{order.orderDate}</div>
</td>
</tr>
))}
</tbody>
</table>
</div>
{orders.length === 0 && (
<div className="px-6 py-8 text-center">
<div className="text-gray-400 text-sm">
No orders found for this customer
</div>
</div>
)}
</div>
);
};
const DocumentViewer = ({ document }: { document: DocumentResponse }) => {
const [isExpanded, setIsExpanded] = useState(false);
const toggleExpanded = () => {
setIsExpanded(!isExpanded);
};
return (
<div className="bg-white rounded-lg shadow-lg border border-gray-200 overflow-hidden">
<div className="bg-gradient-to-r from-purple-600 to-pink-600 px-6 py-4">
<h3 className="text-lg font-semibold text-white">
{document.documentName}
</h3>
<p className="text-purple-100 text-sm mt-1">
Document ID: {document.documentId}
</p>
</div>
<div className="relative">
<div
className={`px-6 py-6 transition-all duration-300 ease-in-out overflow-hidden ${
isExpanded ? "max-h-none" : "max-h-[200px]"
}`}
>
<div className="prose prose-gray max-w-none">
<ReactMarkdown
components={{
h1: ({ children }) => (
<h1 className="text-2xl font-bold text-gray-900 mt-6 mb-4 first:mt-0">
{children}
</h1>
),
h2: ({ children }) => (
<h2 className="text-xl font-semibold text-gray-800 mt-5 mb-3">
{children}
</h2>
),
h3: ({ children }) => (
<h3 className="text-lg font-medium text-gray-700 mt-4 mb-2">
{children}
</h3>
),
p: ({ children }) => (
<p className="text-gray-700 mb-3 leading-relaxed">
{children}
</p>
),
ul: ({ children }) => (
<ul className="list-disc list-inside mb-4 space-y-1 text-gray-700">
{children}
</ul>
),
ol: ({ children }) => (
<ol className="list-decimal list-inside mb-4 space-y-1 text-gray-700">
{children}
</ol>
),
li: ({ children }) => (
<li className="text-gray-700 ml-2">{children}</li>
),
strong: ({ children }) => (
<strong className="font-semibold text-gray-900">
{children}
</strong>
),
em: ({ children }) => (
<em className="italic text-gray-700">{children}</em>
),
code: ({ children }) => (
<code className="bg-gray-100 text-purple-600 px-1.5 py-0.5 rounded text-sm font-mono">
{children}
</code>
),
pre: ({ children }) => (
<pre className="bg-gray-50 border border-gray-200 rounded-lg p-4 overflow-x-auto mb-4">
{children}
</pre>
),
blockquote: ({ children }) => (
<blockquote className="border-l-4 border-purple-500 pl-4 italic text-gray-600 mb-4">
{children}
</blockquote>
),
}}
>
{document.documentContent}
</ReactMarkdown>
</div>
</div>
{/* Gradient overlay when collapsed */}
{!isExpanded && (
<div className="absolute bottom-0 left-0 right-0 h-16 bg-gradient-to-t from-white to-transparent pointer-events-none" />
)}
{/* Expand/Collapse button */}
<div className="px-6 pb-4">
<button
onClick={toggleExpanded}
className="w-full flex items-center justify-center space-x-2 py-2 px-4 text-sm font-medium text-purple-600 bg-purple-50 hover:bg-purple-100 rounded-lg transition-colors duration-200 border border-purple-200"
>
<span>{isExpanded ? "Show less" : "Show more"}</span>
<svg
className={`w-4 h-4 transition-transform duration-200 ${
isExpanded ? "rotate-180" : ""
}`}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M19 9l-7 7-7-7"
/>
</svg>
</button>
</div>
</div>
</div>
);
};
export default App;