import type { RunToolApprovalItem } from '@openai/agents'; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Button } from './ui/Button'; import { useEffect, useState } from 'react'; type Item = ReturnType; function ToolApprovalEntry({ approval, onApprove, onReject, decision, }: { approval: Item; onApprove: () => void; onReject: () => void; decision: 'approved' | 'rejected' | undefined; }) { if (approval.rawItem?.type !== 'function_call') { return null; } return (

Tool {approval.rawItem?.name}

        {approval.rawItem?.arguments}
      
{decision === undefined && (
)} {decision === 'approved' && (

✔︎ Approved

)} {decision === 'rejected' && (

✖︎ Rejected

)}
); } /** * This component just renders all of the approval requests and tracks whether they were approved * or not by storing the callId in a decision Map with `approved` or `rejected` as the value. * Once all the approvals are done, we will call the onDone function to let the parent component * trigger the next run. */ export function Approvals({ approvals, onDone, }: { approvals: ReturnType[]; onDone: (decisions: Map) => void; }) { const [decisions, setDecisions] = useState< Map >(new Map()); const [isOpen, setIsOpen] = useState(approvals.length > 0); useEffect(() => { setDecisions(new Map()); if (approvals.length > 0) { setIsOpen(true); } }, [approvals]); function handleApprove(approval: Item) { setDecisions((prev) => { if (approval.rawItem?.type !== 'function_call') { return prev; } const newDecisions = new Map(prev); newDecisions.set(approval.rawItem?.callId ?? '', 'approved'); return newDecisions; }); } function handleReject(approval: Item) { setDecisions((prev) => { if (approval.rawItem?.type !== 'function_call') { return prev; } const newDecisions = new Map(prev); newDecisions.set(approval.rawItem?.callId ?? '', 'rejected'); return newDecisions; }); } function handleDone() { onDone(decisions); setIsOpen(false); } if (approvals.length === 0) { return null; } const agentName = approvals[0].agent.name; return ( Approval required The agent {agentName} is requesting approval for the following action{approvals.length > 1 ? 's' : ''}:
{approvals.map((approval) => approval.rawItem?.type === 'function_call' ? ( handleApprove(approval)} onReject={() => handleReject(approval)} /> ) : null, )}
); }