open-swe/apps/web/src/components/thread/messages/tool-calls.tsx
Brace Sproul de8d465695
feat: Dark mode (#241)
* feat: Dark mode

* more work

* more dark/light mode, refactor thread card

* fix rest of dark mode

* cr

* cr

* cr
2025-06-18 16:44:41 -07:00

195 lines
7.3 KiB
TypeScript

import { AIMessage, ToolMessage } from "@langchain/langgraph-sdk";
import { useState } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { ChevronDown, ChevronUp } from "lucide-react";
function isComplexValue(value: any): boolean {
return Array.isArray(value) || (typeof value === "object" && value !== null);
}
export function ToolCalls({
toolCalls,
}: {
toolCalls: AIMessage["tool_calls"];
}) {
if (!toolCalls || toolCalls.length === 0) return null;
return (
<div className="grid w-full grid-rows-[1fr_auto] gap-2">
{toolCalls.map((tc, idx) => {
const args = tc.args as Record<string, any>;
const hasArgs = Object.keys(args).length > 0;
return (
<div
key={idx}
className="border-border overflow-hidden rounded-lg border"
>
<div className="border-border bg-card border-b px-4 py-2 dark:bg-gray-800">
<h3 className="text-foreground font-medium">
{tc.name}
{tc.id && (
<code className="bg-muted ml-2 rounded px-2 py-1 text-sm dark:bg-gray-700">
{tc.id}
</code>
)}
</h3>
</div>
{hasArgs ? (
<table className="divide-border min-w-full divide-y">
<tbody className="divide-border divide-y">
{Object.entries(args).map(([key, value], argIdx) => (
<tr key={argIdx}>
<td className="text-foreground px-4 py-2 text-sm font-medium whitespace-nowrap">
{key}
</td>
<td className="text-foreground/80 px-4 py-2 text-sm">
{isComplexValue(value) ? (
<code className="bg-muted/70 rounded px-2 py-1 font-mono text-sm break-all dark:bg-gray-800">
{JSON.stringify(value, null, 2)}
</code>
) : (
String(value)
)}
</td>
</tr>
))}
</tbody>
</table>
) : (
<code className="text-foreground/80 block p-3 text-sm">
{"{}"}
</code>
)}
</div>
);
})}
</div>
);
}
export function ToolResult({ message }: { message: ToolMessage }) {
const [isExpanded, setIsExpanded] = useState(false);
let parsedContent: any;
let isJsonContent = false;
try {
if (typeof message.content === "string") {
parsedContent = JSON.parse(message.content);
isJsonContent = isComplexValue(parsedContent);
}
} 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..."
: contentStr;
return (
<div className="mx-auto grid grid-rows-[1fr_auto] gap-2">
<div className="border-border overflow-hidden rounded-lg border">
<div className="border-border bg-card border-b px-4 py-2 dark:bg-gray-800">
<div className="flex flex-wrap items-center justify-between gap-2">
{message.name ? (
<h3 className="text-foreground font-medium">
Tool Result:{" "}
<code className="bg-muted rounded px-2 py-1 dark:bg-gray-700">
{message.name}
</code>
</h3>
) : (
<h3 className="text-foreground font-medium">Tool Result</h3>
)}
{message.tool_call_id && (
<code className="bg-muted text-foreground ml-2 rounded px-2 py-1 text-sm dark:bg-gray-700">
{message.tool_call_id}
</code>
)}
</div>
</div>
<motion.div
className="bg-muted min-w-full dark:bg-gray-700"
initial={false}
animate={{ height: "auto" }}
transition={{ duration: 0.3 }}
>
<div className="p-3">
<AnimatePresence
mode="wait"
initial={false}
>
<motion.div
key={isExpanded ? "expanded" : "collapsed"}
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -20 }}
transition={{ duration: 0.2 }}
>
{isJsonContent ? (
<table className="divide-border min-w-full divide-y">
<tbody className="divide-border divide-y">
{(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="text-foreground px-4 py-2 text-sm font-medium whitespace-nowrap">
{key}
</td>
<td className="text-foreground/80 px-4 py-2 text-sm">
{isComplexValue(value) ? (
<code className="bg-muted/70 rounded px-2 py-1 font-mono text-sm break-all dark:bg-gray-800">
{JSON.stringify(value, null, 2)}
</code>
) : (
String(value)
)}
</td>
</tr>
);
})}
</tbody>
</table>
) : (
<code className="text-foreground/80 block text-sm">
{displayedContent}
</code>
)}
</motion.div>
</AnimatePresence>
</div>
{((shouldTruncate && !isJsonContent) ||
(isJsonContent &&
Array.isArray(parsedContent) &&
parsedContent.length > 5)) && (
<motion.button
onClick={() => setIsExpanded(!isExpanded)}
className="border-border text-muted-foreground hover:bg-muted hover:text-foreground flex w-full cursor-pointer items-center justify-center border-t-[1px] py-2 transition-all duration-200 ease-in-out dark:hover:bg-gray-800 dark:hover:text-gray-200"
initial={{ scale: 1 }}
whileHover={{ scale: 1.02 }}
whileTap={{ scale: 0.98 }}
>
{isExpanded ? <ChevronUp /> : <ChevronDown />}
</motion.button>
)}
</motion.div>
</div>
</div>
);
}