import React, { useState } from "react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Item } from "@/components/types"; type FunctionCallsPanelProps = { items: Item[]; ws?: WebSocket | null; // pass down ws from parent }; const FunctionCallsPanel: React.FC = ({ items, ws, }) => { const [responses, setResponses] = useState>({}); // Filter function_call items const functionCalls = items.filter((it) => it.type === "function_call"); // For each function_call, check for a corresponding function_call_output const functionCallsWithStatus = functionCalls.map((call) => { const outputs = items.filter( (it) => it.type === "function_call_output" && it.call_id === call.call_id ); const outputItem = outputs[0]; const completed = call.status === "completed" || !!outputItem; const response = outputItem ? outputItem.output : undefined; return { ...call, completed, response, }; }); const handleChange = (call_id: string, value: string) => { setResponses((prev) => ({ ...prev, [call_id]: value })); }; const handleSubmit = (call: Item) => { if (!ws || ws.readyState !== WebSocket.OPEN) return; const call_id = call.call_id || ""; ws.send( JSON.stringify({ type: "conversation.item.create", item: { type: "function_call_output", call_id: call_id, output: JSON.stringify(responses[call_id] || ""), }, }) ); // Ask the model to continue after providing the tool response ws.send(JSON.stringify({ type: "response.create" })); }; return ( Function Calls
{functionCallsWithStatus.map((call) => (

{call.name}

{call.completed ? "Completed" : "Pending"}
{JSON.stringify(call.params)}
{!call.completed ? (
handleChange(call.call_id || "", e.target.value) } />
) : (
{JSON.stringify(JSON.parse(call.response || ""))}
)}
))}
); }; export default FunctionCallsPanel;