"use client"; import type { ToolCallMessagePartProps } from "@assistant-ui/react"; import { CornerDownLeftIcon } 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"; interface InterruptResult { __interrupt__: true; __decided__?: "approve" | "reject"; __completed__?: boolean; action_requests: Array<{ name: string; args: Record; }>; review_configs: Array<{ action_name: string; allowed_decisions: Array<"approve" | "reject">; }>; interrupt_type?: string; context?: { account?: { id: number; name: string; base_url: string; auth_expired?: boolean; }; page?: { page_id: string; page_title: string; space_id: string; connector_id?: number; document_id?: number; indexed_at?: string; }; error?: string; }; } interface SuccessResult { status: "success"; page_id?: string; deleted_from_kb?: boolean; message?: string; } interface ErrorResult { status: "error"; message: string; } interface NotFoundResult { status: "not_found"; message: string; } interface WarningResult { status: "success"; warning: string; message?: string; } interface AuthErrorResult { status: "auth_error"; message: string; connector_id?: number; connector_type: string; } interface InsufficientPermissionsResult { status: "insufficient_permissions"; connector_id: number; message: string; } type DeleteConfluencePageResult = | InterruptResult | SuccessResult | ErrorResult | NotFoundResult | WarningResult | AuthErrorResult | InsufficientPermissionsResult; function isInterruptResult(result: unknown): result is InterruptResult { return ( typeof result === "object" && result !== null && "__interrupt__" in result && (result as InterruptResult).__interrupt__ === true ); } 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 isWarningResult(result: unknown): result is WarningResult { return ( typeof result === "object" && result !== null && "status" in result && (result as WarningResult).status === "success" && "warning" in result && typeof (result as WarningResult).warning === "string" ); } 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 ApprovalCard({ interruptData, onDecision, }: { interruptData: InterruptResult; onDecision: (decision: { type: "approve" | "reject"; message?: string; edited_action?: { name: string; args: Record }; }) => void; }) { const { phase, setProcessing, setRejected } = useHitlPhase(interruptData); const [deleteFromKb, setDeleteFromKb] = useState(false); const context = interruptData.context; const page = context?.page; const handleApprove = useCallback(() => { if (phase !== "pending") return; setProcessing(); onDecision({ type: "approve", edited_action: { name: interruptData.action_requests[0].name, args: { page_id: page?.page_id, connector_id: context?.account?.id, delete_from_kb: deleteFromKb, }, }, }); }, [ phase, setProcessing, onDecision, interruptData, page?.page_id, context?.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" ? "Confluence Page Deletion Rejected" : phase === "processing" || phase === "complete" ? "Confluence Page Deletion Approved" : "Delete Confluence Page"}

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

Page deleted

) : phase === "rejected" ? (

Page deletion was cancelled

) : (

Requires your approval to proceed

)}
{/* Context section — account + page info (visible unless rejected) */} {phase !== "rejected" && context && ( <>
{context.error ? (

{context.error}

) : ( <> {context.account && (

Confluence Account

{context.account.name}
)} {page && (

Page to Delete

{page.page_title}
{page.space_id && (
Space: {page.space_id}
)}
)} )}
)} {/* delete_from_kb toggle */} {phase === "pending" && ( <>
setDeleteFromKb(v === true)} className="shrink-0" />
)} {/* Action buttons - only shown when pending */} {phase === "pending" && ( <>
)}
); } function AuthErrorCard({ result }: { result: AuthErrorResult }) { return (

Confluence authentication expired

{result.message}

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

Additional Confluence permissions required

{result.message}

); } function ErrorCard({ result }: { result: ErrorResult }) { return (

Failed to delete Confluence page

{result.message}

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

Page not found

{result.message}

); } function WarningCard({ result }: { result: WarningResult }) { return (

Partial success

{result.warning}

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

{result.message || "Confluence page deleted successfully"}

{result.deleted_from_kb && ( <>
Also removed from knowledge base
)}
); } export const DeleteConfluencePageToolUI = ({ result }: ToolCallMessagePartProps< { page_title_or_id: string; delete_from_kb?: boolean }, DeleteConfluencePageResult >) => { if (!result) return null; if (isInterruptResult(result)) { return ( { const event = new CustomEvent("hitl-decision", { detail: { decisions: [decision] }, }); window.dispatchEvent(event); }} /> ); } if ( typeof result === "object" && result !== null && "status" in result && (result as { status: string }).status === "rejected" ) { return null; } if (isNotFoundResult(result)) return ; if (isAuthErrorResult(result)) return ; if (isInsufficientPermissionsResult(result)) return ; if (isWarningResult(result)) return ; if (isErrorResult(result)) return ; return ; };