Spaces:
Build error
Build error
Update frontend/src/components/playground.tsx
Browse files
frontend/src/components/playground.tsx
CHANGED
@@ -1,85 +1,82 @@
|
|
1 |
-
|
2 |
import { Input } from "@/components/ui/input";
|
3 |
import { Button } from "@/components/ui/button";
|
4 |
import { v4 as uuidv4 } from "uuid";
|
|
|
5 |
import {
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
} from "@chainlit/react-client";
|
10 |
-
|
11 |
-
|
12 |
|
13 |
export function Playground() {
|
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 |
-
const date = new Date(message.createdAt).toLocaleTimeString(undefined, dateOptions);
|
39 |
-
return (
|
40 |
-
<div key={message.id} className="flex items-start space-x-2">
|
41 |
-
<div className="w-20 text-sm text-green-500">{message.name}</div>
|
42 |
-
<div className="flex-1 border rounded-lg p-2">
|
43 |
-
<p className="text-black dark:text-white">{message.output}</p>
|
44 |
-
<small className="text-xs text-gray-500">{date}</small>
|
45 |
-
</div>
|
46 |
-
</div>
|
47 |
-
);
|
48 |
};
|
49 |
-
|
|
|
|
|
|
|
50 |
return (
|
51 |
-
|
52 |
-
<div className="
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
</div>
|
57 |
-
|
58 |
-
<div className="flex-1 overflow-auto p-6">
|
59 |
-
<div className="space-y-4">
|
60 |
-
{messages.map((message) => renderMessage(message))}
|
61 |
-
</div>
|
62 |
-
</div>
|
63 |
-
<div className="border-t p-4 bg-white dark:bg-gray-800">
|
64 |
-
<div className="flex items-center space-x-2">
|
65 |
-
<Input
|
66 |
-
autoFocus
|
67 |
-
className="flex-1"
|
68 |
-
id="message-input"
|
69 |
-
placeholder="Type a message"
|
70 |
-
value={inputValue}
|
71 |
-
onChange={(e) => setInputValue(e.target.value)}
|
72 |
-
onKeyUp={(e) => {
|
73 |
-
if (e.key === "Enter") {
|
74 |
-
handleSendMessage();
|
75 |
-
}
|
76 |
-
}}
|
77 |
-
/>
|
78 |
-
<Button onClick={handleSendMessage} type="submit">
|
79 |
-
Send
|
80 |
-
</Button>
|
81 |
-
</div>
|
82 |
-
</div>
|
83 |
</div>
|
|
|
84 |
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
}
|
|
|
1 |
+
|
2 |
import { Input } from "@/components/ui/input";
|
3 |
import { Button } from "@/components/ui/button";
|
4 |
import { v4 as uuidv4 } from "uuid";
|
5 |
+
|
6 |
import {
|
7 |
+
useChatInteract,
|
8 |
+
useChatMessages,
|
9 |
+
IStep,
|
10 |
} from "@chainlit/react-client";
|
11 |
+
import { useState } from "react";
|
|
|
12 |
|
13 |
export function Playground() {
|
14 |
+
const [inputValue, setInputValue] = useState("");
|
15 |
+
const { sendMessage } = useChatInteract();
|
16 |
+
const { messages } = useChatMessages();
|
17 |
|
18 |
+
const handleSendMessage = () => {
|
19 |
+
const content = inputValue.trim();
|
20 |
+
if (content) {
|
21 |
+
const message: IStep = {
|
22 |
+
id: uuidv4(),
|
23 |
+
name: "user",
|
24 |
+
type: "user_message",
|
25 |
+
output: content,
|
26 |
+
createdAt: new Date().toISOString(),
|
27 |
+
};
|
28 |
+
sendMessage(message, []);
|
29 |
+
setInputValue("");
|
30 |
+
}
|
31 |
+
};
|
32 |
|
33 |
+
const renderMessage = (message: IStep) => {
|
34 |
+
const dateOptions: Intl.DateTimeFormatOptions = {
|
35 |
+
hour: "2-digit",
|
36 |
+
minute: "2-digit",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
};
|
38 |
+
const date = new Date(message.createdAt).toLocaleTimeString(
|
39 |
+
undefined,
|
40 |
+
dateOptions
|
41 |
+
);
|
42 |
return (
|
43 |
+
<div key={message.id} className="flex items-start space-x-2">
|
44 |
+
<div className="w-20 text-sm text-green-500">{message.name}</div>
|
45 |
+
<div className="flex-1 border rounded-lg p-2">
|
46 |
+
<p className="text-black dark:text-white">{message.output}</p>
|
47 |
+
<small className="text-xs text-gray-500">{date}</small>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
</div>
|
49 |
+
</div>
|
50 |
);
|
51 |
+
};
|
52 |
+
|
53 |
+
return (
|
54 |
+
<div className="min-h-screen bg-gray-100 dark:bg-gray-900 flex flex-col">
|
55 |
+
<div className="flex-1 overflow-auto p-6">
|
56 |
+
<div className="space-y-4">
|
57 |
+
{messages.map((message) => renderMessage(message))}
|
58 |
+
</div>
|
59 |
+
</div>
|
60 |
+
<div className="border-t p-4 bg-white dark:bg-gray-800">
|
61 |
+
<div className="flex items-center space-x-2">
|
62 |
+
<Input
|
63 |
+
autoFocus
|
64 |
+
className="flex-1"
|
65 |
+
id="message-input"
|
66 |
+
placeholder="Type a message"
|
67 |
+
value={inputValue}
|
68 |
+
onChange={(e) => setInputValue(e.target.value)}
|
69 |
+
onKeyUp={(e) => {
|
70 |
+
if (e.key === "Enter") {
|
71 |
+
handleSendMessage();
|
72 |
+
}
|
73 |
+
}}
|
74 |
+
/>
|
75 |
+
<Button onClick={handleSendMessage} type="submit">
|
76 |
+
Send
|
77 |
+
</Button>
|
78 |
+
</div>
|
79 |
+
</div>
|
80 |
+
</div>
|
81 |
+
);
|
82 |
}
|