feat: add prompt examples panel and replicate flowchart functionality in ChatPanel

This commit is contained in:
dayuan.jiang
2025-03-23 13:15:28 +00:00
parent 51d4536dbb
commit b3f8820444
2 changed files with 53 additions and 0 deletions

View File

@@ -29,6 +29,8 @@ export default function ChatPanel({
const stepCounterRef = useRef<number>(0); const stepCounterRef = useRef<number>(0);
// Add state for file attachments // Add state for file attachments
const [files, setFiles] = useState<FileList | undefined>(undefined); const [files, setFiles] = useState<FileList | undefined>(undefined);
// Add state to control visibility of prompt examples panel
const [showExamples, setShowExamples] = useState<boolean>(true);
// Remove the currentXmlRef and related useEffect // Remove the currentXmlRef and related useEffect
const { const {
@@ -70,6 +72,9 @@ export default function ChatPanel({
e.preventDefault(); e.preventDefault();
if (input.trim() && status !== "streaming") { if (input.trim() && status !== "streaming") {
try { try {
// Hide examples panel after sending a message
setShowExamples(false);
// Fetch chart data before setting input // Fetch chart data before setting input
const chartXml = await onFetchChart(); const chartXml = await onFetchChart();
@@ -103,6 +108,30 @@ export default function ChatPanel({
setFiles(newFiles); setFiles(newFiles);
}; };
// New utility function to create a FileList from a File
const createFileList = (file: File): FileList => {
const dt = new DataTransfer();
dt.items.add(file);
return dt.files;
};
// New handler for the "Replicate this flowchart" button
const handleReplicateFlowchart = async () => {
setInput("Replicate this flowchart.");
try {
// Fetch the example image
const response = await fetch("/example.png");
const blob = await response.blob();
const file = new File([blob], "example.png", { type: "image/png" });
// Set the file to the files state
setFiles(createFileList(file));
} catch (error) {
console.error("Error loading example image:", error);
}
};
// Helper function to render tool invocations // Helper function to render tool invocations
const renderToolInvocation = (toolInvocation: any) => { const renderToolInvocation = (toolInvocation: any) => {
const callId = toolInvocation.toolCallId; const callId = toolInvocation.toolCallId;
@@ -301,6 +330,30 @@ export default function ChatPanel({
<div ref={messagesEndRef} /> <div ref={messagesEndRef} />
</ScrollArea> </ScrollArea>
</CardContent> </CardContent>
{/* Conditionally render Prompt Examples Panel */}
{showExamples && (
<div className="px-4 py-2 border-t border-b border-gray-100">
<p className="text-sm text-gray-500 mb-2">
Try these examples:
</p>
<div className="flex flex-wrap gap-2">
<button
className="text-xs bg-gray-100 hover:bg-gray-200 text-gray-800 font-medium py-1 px-2 rounded"
onClick={() => setInput("Draw a cat for me.")}
>
Draw a cat for me.
</button>
<button
className="text-xs bg-gray-100 hover:bg-gray-200 text-gray-800 font-medium py-1 px-2 rounded"
onClick={handleReplicateFlowchart}
>
Replicate this flowchart
</button>
</div>
</div>
)}
<CardFooter className="p-2"> <CardFooter className="p-2">
<ChatInput <ChatInput
input={input} input={input}

BIN
public/example.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB