"use client"; import type { ToolCallMessagePartProps } from "@assistant-ui/react"; import { CornerDownLeftIcon, InfoIcon } 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 DropboxAccount { id: number; name: string; user_email?: string; auth_expired?: boolean; } interface DropboxFile { file_id: string; file_path: string; name: string; document_id?: number; } type DropboxTrashFileContext = { account?: DropboxAccount; file?: DropboxFile; error?: string; }; interface SuccessResult { status: "success"; file_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; } type DeleteDropboxFileResult = | InterruptResult | SuccessResult | ErrorResult | NotFoundResult | 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 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 file = context?.file; const handleApprove = useCallback(() => { if (phase !== "pending") return; setProcessing(); onDecision({ type: "approve", edited_action: { name: interruptData.action_requests[0].name, args: { file_path: file?.file_path, connector_id: account?.id, delete_from_kb: deleteFromKb, }, }, }); }, [phase, setProcessing, onDecision, interruptData, file?.file_path, 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 (

{phase === "rejected" ? "Dropbox File Deletion Rejected" : phase === "processing" || phase === "complete" ? "Dropbox File Deletion Approved" : "Delete Dropbox File"}

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

File deleted

) : phase === "rejected" ? (

File deletion was cancelled

) : (

Requires your approval to proceed

)}
{phase !== "rejected" && context && ( <>
{context.error ? (

{context.error}

) : ( <> {account && (

Dropbox Account

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

File to Delete

{file.name}
{file.file_path && (
{file.file_path}
)}
)} )}
)} {phase === "pending" && ( <>

The file will be permanently deleted from Dropbox.

setDeleteFromKb(v === true)} className="shrink-0" />
)} {phase === "pending" && ( <>
)}
); } function ErrorCard({ result }: { result: ErrorResult }) { return (

Failed to delete file

{result.message}

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

{result.message}

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

Dropbox authentication expired

{result.message}

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

{result.message || "File deleted from Dropbox"}

{result.deleted_from_kb && ( <>
Also removed from knowledge base
)}
); } export const DeleteDropboxFileToolUI = ({ result, }: ToolCallMessagePartProps< { file_name: string; delete_from_kb?: boolean }, DeleteDropboxFileResult >) => { 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 (isNotFoundResult(result)) return ; if (isErrorResult(result)) return ; return ; };