This commit is contained in:
bracesproul
2025-03-04 15:22:46 -08:00
parent fe75c8aca1
commit af659d598e

View File

@@ -64,20 +64,46 @@ export function ToolCalls({
export function ToolResult({ message }: { message: ToolMessage }) {
const [isExpanded, setIsExpanded] = useState(false);
const contentStr = JSON.stringify(message.content, null, 2);
const contentLines = contentStr.split('\n');
let parsedContent: any;
let isJsonContent = false;
try {
if (typeof message.content === "string") {
parsedContent = JSON.parse(message.content);
isJsonContent = true;
}
} catch {
// Content is not JSON, use as is
parsedContent = message.content;
}
const contentStr = isJsonContent
? JSON.stringify(parsedContent, null, 2)
: String(message.content);
const contentLines = contentStr.split("\n");
const shouldTruncate = contentLines.length > 4 || contentStr.length > 500;
const displayedContent = shouldTruncate && !isExpanded
? (contentStr.length > 500
? contentStr.slice(0, 500) + '...'
: contentLines.slice(0, 4).join('\n') + '\n...')
const displayedContent =
shouldTruncate && !isExpanded
? contentStr.length > 500
? contentStr.slice(0, 500) + "..."
: contentLines.slice(0, 4).join("\n") + "\n..."
: contentStr;
return (
<div className="border border-gray-200 rounded-lg overflow-hidden">
<div className="bg-gray-50 px-4 py-2 border-b border-gray-200">
<div className="flex items-center justify-between gap-2 flex-wrap">
{message.name ? <h3 className="font-medium text-gray-900">Tool Result: <code className="bg-gray-100 px-2 py-1 rounded">{message.name}</code></h3> : <h3 className="font-medium text-gray-900">Tool Result</h3>}
{message.name ? (
<h3 className="font-medium text-gray-900">
Tool Result:{" "}
<code className="bg-gray-100 px-2 py-1 rounded">
{message.name}
</code>
</h3>
) : (
<h3 className="font-medium text-gray-900">Tool Result</h3>
)}
{message.tool_call_id && (
<code className="ml-2 text-sm bg-gray-100 px-2 py-1 rounded">
{message.tool_call_id}
@@ -100,11 +126,47 @@ export function ToolResult({ message }: { message: ToolMessage }) {
exit={{ opacity: 0, y: -20 }}
transition={{ duration: 0.2 }}
>
{isJsonContent ? (
<table className="min-w-full divide-y divide-gray-200">
<tbody className="divide-y divide-gray-200">
{(Array.isArray(parsedContent)
? isExpanded
? parsedContent
: parsedContent.slice(0, 5)
: Object.entries(parsedContent)
).map((item, argIdx) => {
const [key, value] = Array.isArray(parsedContent)
? [argIdx, item]
: [item[0], item[1]];
return (
<tr key={argIdx}>
<td className="px-4 py-2 text-sm font-medium text-gray-900 whitespace-nowrap">
{key}
</td>
<td className="px-4 py-2 text-sm text-gray-500">
{isComplexValue(value) ? (
<code className="bg-gray-50 rounded px-2 py-1 font-mono text-sm">
{JSON.stringify(value, null, 2)}
</code>
) : (
String(value)
)}
</td>
</tr>
);
})}
</tbody>
</table>
) : (
<code className="text-sm block">{displayedContent}</code>
)}
</motion.div>
</AnimatePresence>
</div>
{shouldTruncate && (
{((shouldTruncate && !isJsonContent) ||
(isJsonContent &&
Array.isArray(parsedContent) &&
parsedContent.length > 5)) && (
<motion.button
onClick={() => setIsExpanded(!isExpanded)}
className="w-full py-2 flex items-center justify-center border-t-[1px] border-gray-200 text-gray-500 hover:text-gray-600 hover:bg-gray-50 transition-all ease-in-out duration-200 cursor-pointer"