mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-02 22:32:27 +08:00
fix: Implement auto-collapse for tool invocation args in chat message display
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import type React from "react";
|
import type React from "react";
|
||||||
import { useRef, useEffect } from "react";
|
import { useRef, useEffect, useState } from "react";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import { ScrollArea } from "@/components/ui/scroll-area";
|
import { ScrollArea } from "@/components/ui/scroll-area";
|
||||||
import ExamplePanel from "./chat-example-panel";
|
import ExamplePanel from "./chat-example-panel";
|
||||||
@@ -26,12 +26,35 @@ export function ChatMessageDisplay({
|
|||||||
const { chartXML, loadDiagram: onDisplayChart } = useDiagram();
|
const { chartXML, loadDiagram: onDisplayChart } = useDiagram();
|
||||||
const messagesEndRef = useRef<HTMLDivElement>(null);
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
||||||
const previousXML = useRef<string>("");
|
const previousXML = useRef<string>("");
|
||||||
|
const [expandedTools, setExpandedTools] = useState<Record<string, boolean>>(
|
||||||
|
{}
|
||||||
|
);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (messagesEndRef.current) {
|
if (messagesEndRef.current) {
|
||||||
messagesEndRef.current.scrollIntoView({ behavior: "smooth" });
|
messagesEndRef.current.scrollIntoView({ behavior: "smooth" });
|
||||||
}
|
}
|
||||||
}, [messages]);
|
}, [messages]);
|
||||||
|
|
||||||
|
// Auto-collapse args when diagrams are generated
|
||||||
|
useEffect(() => {
|
||||||
|
messages.forEach((message) => {
|
||||||
|
if (message.parts) {
|
||||||
|
message.parts.forEach((part) => {
|
||||||
|
if (
|
||||||
|
part.type === "tool-invocation" &&
|
||||||
|
part.toolInvocation.state === "result"
|
||||||
|
) {
|
||||||
|
const callId = part.toolInvocation.toolCallId;
|
||||||
|
setExpandedTools((prev) => ({
|
||||||
|
...prev,
|
||||||
|
[callId]: false,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, [messages]);
|
||||||
|
|
||||||
function handleDisplayChart(xml: string) {
|
function handleDisplayChart(xml: string) {
|
||||||
const currentXml = xml || "";
|
const currentXml = xml || "";
|
||||||
const convertedXml = convertToLegalXml(currentXml);
|
const convertedXml = convertToLegalXml(currentXml);
|
||||||
@@ -45,16 +68,34 @@ export function ChatMessageDisplay({
|
|||||||
const renderToolInvocation = (toolInvocation: any) => {
|
const renderToolInvocation = (toolInvocation: any) => {
|
||||||
const callId = toolInvocation.toolCallId;
|
const callId = toolInvocation.toolCallId;
|
||||||
const { toolName, args, state } = toolInvocation;
|
const { toolName, args, state } = toolInvocation;
|
||||||
|
const isExpanded = expandedTools[callId] ?? true;
|
||||||
handleDisplayChart(args?.xml);
|
handleDisplayChart(args?.xml);
|
||||||
|
|
||||||
|
const toggleExpanded = () => {
|
||||||
|
setExpandedTools((prev) => ({
|
||||||
|
...prev,
|
||||||
|
[callId]: !isExpanded,
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={callId}
|
key={callId}
|
||||||
className="p-4 my-2 text-gray-500 border border-gray-300 rounded"
|
className="p-4 my-2 text-gray-500 border border-gray-300 rounded"
|
||||||
>
|
>
|
||||||
<div className="flex flex-col gap-2">
|
<div className="flex flex-col gap-2">
|
||||||
<div className="text-xs">Tool: display_diagram</div>
|
<div className="flex items-center justify-between">
|
||||||
{args && (
|
<div className="text-xs">Tool: display_diagram</div>
|
||||||
|
{args && Object.keys(args).length > 0 && (
|
||||||
|
<button
|
||||||
|
onClick={toggleExpanded}
|
||||||
|
className="text-xs text-gray-500 hover:text-gray-700"
|
||||||
|
>
|
||||||
|
{isExpanded ? "Hide Args" : "Show Args"}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{args && isExpanded && (
|
||||||
<div className="mt-1 font-mono text-xs overflow-hidden">
|
<div className="mt-1 font-mono text-xs overflow-hidden">
|
||||||
{typeof args === "object" &&
|
{typeof args === "object" &&
|
||||||
Object.keys(args).length > 0 &&
|
Object.keys(args).length > 0 &&
|
||||||
|
|||||||
Reference in New Issue
Block a user