"use client"; import type { ToolCallMessagePartProps } from "@assistant-ui/react"; import { CalendarIcon, CornerDownLeftIcon, MailIcon, UserIcon } from "lucide-react"; import { useCallback, useEffect, useState } from "react"; import { TextShimmerLoader } from "@/components/prompt-kit/loader"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { useHitlPhase } from "@/hooks/use-hitl-phase"; import type { HitlDecision, InterruptResult } from "@/lib/hitl"; import { isInterruptResult, useHitlDecision } from "@/lib/hitl"; interface GmailAccount { id: number; name: string; email: string; auth_expired?: boolean; } interface GmailMessage { message_id: string; thread_id?: string; subject: string; sender: string; date: string; connector_id: number; document_id: number; } type GmailTrashEmailContext = { account?: GmailAccount; email?: GmailMessage; error?: string; }; interface SuccessResult { status: "success"; message_id?: string; message?: string; deleted_from_kb?: boolean; } interface ErrorResult { status: "error"; message: string; } interface NotFoundResult { status: "not_found"; message: string; } interface AuthErrorResult { status: "auth_error"; message: string; connector_type?: string; } interface InsufficientPermissionsResult { status: "insufficient_permissions"; connector_id: number; message: string; } type TrashGmailEmailResult = | InterruptResult | SuccessResult | ErrorResult | NotFoundResult | InsufficientPermissionsResult | AuthErrorResult; function isErrorResult(result: unknown): result is ErrorResult { return ( typeof result === "object" && result !== null && "status" in result && (result as ErrorResult).status === "error" ); } function isNotFoundResult(result: unknown): result is NotFoundResult { return ( typeof result === "object" && result !== null && "status" in result && (result as NotFoundResult).status === "not_found" ); } function isAuthErrorResult(result: unknown): result is AuthErrorResult { return ( typeof result === "object" && result !== null && "status" in result && (result as AuthErrorResult).status === "auth_error" ); } function isInsufficientPermissionsResult(result: unknown): result is InsufficientPermissionsResult { return ( typeof result === "object" && result !== null && "status" in result && (result as InsufficientPermissionsResult).status === "insufficient_permissions" ); } function formatDate(dateStr: string): string { return new Date(dateStr).toLocaleDateString(undefined, { dateStyle: "medium" }); } function ApprovalCard({ interruptData, onDecision, }: { interruptData: InterruptResult; onDecision: (decision: HitlDecision) => void; }) { const { phase, setProcessing, setRejected } = useHitlPhase(interruptData); const [deleteFromKb, setDeleteFromKb] = useState(false); const context = interruptData.context; const account = context?.account; const email = context?.email; const handleApprove = useCallback(() => { if (phase !== "pending") return; setProcessing(); onDecision({ type: "approve", edited_action: { name: interruptData.action_requests[0].name, args: { message_id: email?.message_id, connector_id: email?.connector_id ?? account?.id, delete_from_kb: deleteFromKb, }, }, }); }, [phase, setProcessing, onDecision, interruptData, email, account?.id, deleteFromKb]); useEffect(() => { const handler = (e: KeyboardEvent) => { if (e.key === "Enter" && !e.shiftKey && !e.ctrlKey && !e.metaKey) { handleApprove(); } }; window.addEventListener("keydown", handler); return () => window.removeEventListener("keydown", handler); }, [handleApprove]); return (
{/* Header */}

{phase === "rejected" ? "Email Trash Rejected" : phase === "processing" || phase === "complete" ? "Email Trash Approved" : "Trash Email"}

{phase === "processing" ? ( ) : phase === "complete" ? (

Email trashed

) : phase === "rejected" ? (

Email trash was cancelled

) : (

Requires your approval to proceed

)}
{/* Context — read-only account and email info */} {phase !== "rejected" && context && ( <>
{context.error ? (

{context.error}

) : ( <> {account && (

Gmail Account

{account.name}
)} {email && (

Email to Trash

{email.subject}
From: {email.sender}
Date: {formatDate(email.date)}
)} )}
)} {/* delete_from_kb toggle */} {phase === "pending" && ( <>
setDeleteFromKb(v === true)} className="shrink-0" />
)} {/* Action buttons */} {phase === "pending" && ( <>
)}
); } function ErrorCard({ result }: { result: ErrorResult }) { return (

Failed to trash email

{result.message}

); } function AuthErrorCard({ result }: { result: AuthErrorResult }) { return (

Gmail authentication expired

{result.message}

); } function InsufficientPermissionsCard({ result }: { result: InsufficientPermissionsResult }) { return (

Additional Gmail permissions required

{result.message}

); } function NotFoundCard({ result }: { result: NotFoundResult }) { return (

Email not found

{result.message}

); } function SuccessCard({ result }: { result: SuccessResult }) { return (

{result.message || "Email moved to trash successfully"}

{result.deleted_from_kb && ( <>
Also removed from knowledge base
)}
); } export const TrashGmailEmailToolUI = ({ result, }: ToolCallMessagePartProps< { email_subject_or_id: string; delete_from_kb?: boolean }, TrashGmailEmailResult >) => { const { dispatch } = useHitlDecision(); if (!result) return null; if (isInterruptResult(result)) { return ( } onDecision={(decision) => dispatch([decision])} /> ); } if ( typeof result === "object" && result !== null && "status" in result && (result as { status: string }).status === "rejected" ) { return null; } if (isAuthErrorResult(result)) return ; if (isInsufficientPermissionsResult(result)) return ; if (isNotFoundResult(result)) return ; if (isErrorResult(result)) return ; return ; };