"use client"; import type { ToolCallMessagePartProps } from "@assistant-ui/react"; import { useSetAtom } from "jotai"; import { CornerDownLeftIcon, Pen } from "lucide-react"; import { useCallback, useEffect, useState } from "react"; import { openHitlEditPanelAtom } from "@/atoms/chat/hitl-edit-panel.atom"; import { PlateEditor } from "@/components/editor/plate-editor"; import { TextShimmerLoader } from "@/components/prompt-kit/loader"; import { Button } from "@/components/ui/button"; import { useHitlPhase } from "@/hooks/use-hitl-phase"; interface InterruptResult { __interrupt__: true; __decided__?: "approve" | "reject" | "edit"; __completed__?: boolean; action_requests: Array<{ name: string; args: Record; description?: string; }>; review_configs: Array<{ action_name: string; allowed_decisions: Array<"approve" | "edit" | "reject">; }>; interrupt_type?: string; message?: string; context?: { account?: { id: number; name: string; workspace_id: string | null; workspace_name: string; workspace_icon: string; }; page_id?: string; current_title?: string; document_id?: number; indexed_at?: string; error?: string; }; } interface SuccessResult { status: "success"; page_id: string; title: string; url: string; content_preview?: string; content_length?: number; message?: string; } interface ErrorResult { status: "error"; message: string; } interface InfoResult { status: "not_found"; message: string; } interface AuthErrorResult { status: "auth_error"; message: string; connector_id?: number; connector_type: string; } type UpdateNotionPageResult = | InterruptResult | SuccessResult | ErrorResult | InfoResult | AuthErrorResult; 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 isAuthErrorResult(result: unknown): result is AuthErrorResult { return ( typeof result === "object" && result !== null && "status" in result && (result as AuthErrorResult).status === "auth_error" ); } function isInfoResult(result: unknown): result is InfoResult { return ( typeof result === "object" && result !== null && "status" in result && (result as InfoResult).status === "not_found" ); } function ApprovalCard({ args, interruptData, onDecision, }: { args: Record; interruptData: InterruptResult; onDecision: (decision: { type: "approve" | "reject" | "edit"; message?: string; edited_action?: { name: string; args: Record }; }) => void; }) { const { phase, setProcessing, setRejected } = useHitlPhase(interruptData); const [isPanelOpen, setIsPanelOpen] = useState(false); const openHitlEditPanel = useSetAtom(openHitlEditPanelAtom); const [pendingEdits, setPendingEdits] = useState<{ content: string } | null>(null); const account = interruptData.context?.account; const currentTitle = interruptData.context?.current_title; const reviewConfig = interruptData.review_configs[0]; const allowedDecisions = reviewConfig?.allowed_decisions ?? ["approve", "reject"]; const canEdit = allowedDecisions.includes("edit"); const handleApprove = useCallback(() => { if (phase !== "pending") return; if (isPanelOpen) return; if (!allowedDecisions.includes("approve")) return; const isEdited = pendingEdits !== null; setProcessing(); onDecision({ type: isEdited ? "edit" : "approve", edited_action: { name: interruptData.action_requests[0].name, args: { page_id: args.page_id, content: pendingEdits?.content ?? args.content, connector_id: account?.id, }, }, }); }, [ phase, isPanelOpen, allowedDecisions, setProcessing, onDecision, interruptData, args, account?.id, pendingEdits, ]); 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" ? "Notion Page Update Rejected" : phase === "processing" || phase === "complete" ? "Notion Page Update Approved" : "Update Notion Page"}

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

{pendingEdits ? "Page updated with your changes" : "Page updated"}

) : phase === "rejected" ? (

Page update was cancelled

) : (

Requires your approval to proceed

)}
{phase === "pending" && canEdit && ( )}
{/* Context section — real UI in pending/processing/complete */} {phase !== "rejected" && interruptData.context && ( <>
{interruptData.context.error ? (

{interruptData.context.error}

) : ( <> {account && (

Notion Account

{account.workspace_name}
)} {currentTitle && (

Current Page

{currentTitle}
)} )}
)} {/* Content preview */}
{(pendingEdits?.content ?? args.content) != null ? (
) : (

No content update specified

)}
{/* Action buttons - only shown when pending */} {phase === "pending" && ( <>
{allowedDecisions.includes("approve") && ( )} {allowedDecisions.includes("reject") && ( )}
)}
); } function AuthErrorCard({ result }: { result: AuthErrorResult }) { return (

Notion authentication expired

{result.message}

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

Failed to update Notion page

{result.message}

); } function InfoCard({ result }: { result: InfoResult }) { return (

Page not found

{result.message}

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

{result.message || "Notion page updated successfully"}

Title: {result.title}
{result.url && ( )}
); } export const UpdateNotionPageToolUI = ({ args, result }: ToolCallMessagePartProps<{ page_title: string; content: string }, UpdateNotionPageResult>) => { 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 (isInfoResult(result)) { return ; } if (isAuthErrorResult(result)) { return ; } if (isErrorResult(result)) { return ; } return ; };