2026-03-20 18:47:06 +05:30
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { makeAssistantToolUI } from "@assistant-ui/react";
|
2026-03-21 13:20:13 +05:30
|
|
|
import { useSetAtom } from "jotai";
|
|
|
|
|
import { CornerDownLeftIcon, Pen, UserIcon, UsersIcon } from "lucide-react";
|
2026-03-20 18:47:06 +05:30
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
2026-03-21 13:20:13 +05:30
|
|
|
import type { ExtraField } from "@/atoms/chat/hitl-edit-panel.atom";
|
|
|
|
|
import { openHitlEditPanelAtom } from "@/atoms/chat/hitl-edit-panel.atom";
|
|
|
|
|
import { PlateEditor } from "@/components/editor/plate-editor";
|
|
|
|
|
import { TextShimmerLoader } from "@/components/prompt-kit/loader";
|
2026-03-20 18:47:06 +05:30
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import {
|
|
|
|
|
Select,
|
|
|
|
|
SelectContent,
|
|
|
|
|
SelectItem,
|
|
|
|
|
SelectTrigger,
|
|
|
|
|
SelectValue,
|
|
|
|
|
} from "@/components/ui/select";
|
2026-03-21 11:18:35 +05:30
|
|
|
import { useHitlPhase } from "@/hooks/use-hitl-phase";
|
2026-03-20 18:47:06 +05:30
|
|
|
|
|
|
|
|
interface GmailAccount {
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
|
|
|
|
email: string;
|
|
|
|
|
auth_expired?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface InterruptResult {
|
|
|
|
|
__interrupt__: true;
|
|
|
|
|
__decided__?: "approve" | "reject" | "edit";
|
2026-03-21 11:18:35 +05:30
|
|
|
__completed__?: boolean;
|
2026-03-20 18:47:06 +05:30
|
|
|
action_requests: Array<{
|
|
|
|
|
name: string;
|
|
|
|
|
args: Record<string, unknown>;
|
|
|
|
|
}>;
|
|
|
|
|
review_configs: Array<{
|
|
|
|
|
action_name: string;
|
|
|
|
|
allowed_decisions: Array<"approve" | "edit" | "reject">;
|
|
|
|
|
}>;
|
|
|
|
|
context?: {
|
|
|
|
|
accounts?: GmailAccount[];
|
|
|
|
|
error?: string;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface SuccessResult {
|
|
|
|
|
status: "success";
|
|
|
|
|
draft_id?: string;
|
|
|
|
|
message_id?: string;
|
|
|
|
|
message?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ErrorResult {
|
|
|
|
|
status: "error";
|
|
|
|
|
message: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface AuthErrorResult {
|
|
|
|
|
status: "auth_error";
|
|
|
|
|
message: string;
|
|
|
|
|
connector_type?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 19:36:00 +05:30
|
|
|
interface InsufficientPermissionsResult {
|
|
|
|
|
status: "insufficient_permissions";
|
|
|
|
|
connector_id: number;
|
|
|
|
|
message: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 18:47:06 +05:30
|
|
|
type CreateGmailDraftResult =
|
|
|
|
|
| InterruptResult
|
|
|
|
|
| SuccessResult
|
|
|
|
|
| ErrorResult
|
2026-03-20 19:36:00 +05:30
|
|
|
| InsufficientPermissionsResult
|
2026-03-20 18:47:06 +05:30
|
|
|
| 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"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 19:36:00 +05:30
|
|
|
function isInsufficientPermissionsResult(result: unknown): result is InsufficientPermissionsResult {
|
|
|
|
|
return (
|
|
|
|
|
typeof result === "object" &&
|
|
|
|
|
result !== null &&
|
|
|
|
|
"status" in result &&
|
|
|
|
|
(result as InsufficientPermissionsResult).status === "insufficient_permissions"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 18:47:06 +05:30
|
|
|
function ApprovalCard({
|
|
|
|
|
args,
|
|
|
|
|
interruptData,
|
|
|
|
|
onDecision,
|
|
|
|
|
}: {
|
|
|
|
|
args: { to: string; subject: string; body: string; cc?: string; bcc?: string };
|
|
|
|
|
interruptData: InterruptResult;
|
|
|
|
|
onDecision: (decision: {
|
|
|
|
|
type: "approve" | "reject" | "edit";
|
|
|
|
|
message?: string;
|
|
|
|
|
edited_action?: { name: string; args: Record<string, unknown> };
|
|
|
|
|
}) => void;
|
|
|
|
|
}) {
|
2026-03-21 11:18:35 +05:30
|
|
|
const { phase, setProcessing, setRejected } = useHitlPhase(interruptData);
|
2026-03-20 18:47:06 +05:30
|
|
|
const [isPanelOpen, setIsPanelOpen] = useState(false);
|
|
|
|
|
const openHitlEditPanel = useSetAtom(openHitlEditPanelAtom);
|
2026-03-20 21:38:19 +05:30
|
|
|
const [pendingEdits, setPendingEdits] = useState<{
|
2026-03-21 13:20:13 +05:30
|
|
|
subject: string;
|
|
|
|
|
body: string;
|
|
|
|
|
to: string;
|
|
|
|
|
cc: string;
|
|
|
|
|
bcc: string;
|
2026-03-20 21:38:19 +05:30
|
|
|
} | null>(null);
|
2026-03-20 18:47:06 +05:30
|
|
|
|
|
|
|
|
const accounts = interruptData.context?.accounts ?? [];
|
|
|
|
|
const validAccounts = accounts.filter((a) => !a.auth_expired);
|
|
|
|
|
const expiredAccounts = accounts.filter((a) => a.auth_expired);
|
|
|
|
|
|
|
|
|
|
const defaultAccountId = useMemo(() => {
|
|
|
|
|
if (validAccounts.length === 1) return String(validAccounts[0].id);
|
|
|
|
|
return "";
|
|
|
|
|
}, [validAccounts]);
|
|
|
|
|
|
|
|
|
|
const [selectedAccountId, setSelectedAccountId] = useState<string>(defaultAccountId);
|
|
|
|
|
|
|
|
|
|
const canApprove = !!selectedAccountId;
|
|
|
|
|
|
2026-03-21 11:18:35 +05:30
|
|
|
const reviewConfig = interruptData.review_configs?.[0];
|
2026-03-20 18:47:06 +05:30
|
|
|
const allowedDecisions = reviewConfig?.allowed_decisions ?? ["approve", "reject"];
|
|
|
|
|
const canEdit = allowedDecisions.includes("edit");
|
|
|
|
|
|
|
|
|
|
const handleApprove = useCallback(() => {
|
2026-03-21 11:18:35 +05:30
|
|
|
if (phase !== "pending") return;
|
|
|
|
|
if (isPanelOpen || !canApprove) return;
|
2026-03-20 18:47:06 +05:30
|
|
|
if (!allowedDecisions.includes("approve")) return;
|
2026-03-20 21:38:19 +05:30
|
|
|
const isEdited = pendingEdits !== null;
|
2026-03-21 11:18:35 +05:30
|
|
|
setProcessing();
|
2026-03-20 18:47:06 +05:30
|
|
|
onDecision({
|
2026-03-20 21:38:19 +05:30
|
|
|
type: isEdited ? "edit" : "approve",
|
2026-03-20 18:47:06 +05:30
|
|
|
edited_action: {
|
|
|
|
|
name: interruptData.action_requests[0].name,
|
|
|
|
|
args: {
|
|
|
|
|
...args,
|
2026-03-20 21:38:19 +05:30
|
|
|
...(pendingEdits && {
|
|
|
|
|
subject: pendingEdits.subject,
|
|
|
|
|
body: pendingEdits.body,
|
|
|
|
|
to: pendingEdits.to,
|
|
|
|
|
cc: pendingEdits.cc,
|
|
|
|
|
bcc: pendingEdits.bcc,
|
|
|
|
|
}),
|
2026-03-20 18:47:06 +05:30
|
|
|
connector_id: selectedAccountId ? Number(selectedAccountId) : null,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-03-21 13:20:13 +05:30
|
|
|
}, [
|
|
|
|
|
phase,
|
|
|
|
|
isPanelOpen,
|
|
|
|
|
canApprove,
|
|
|
|
|
allowedDecisions,
|
|
|
|
|
setProcessing,
|
|
|
|
|
onDecision,
|
|
|
|
|
interruptData,
|
|
|
|
|
args,
|
|
|
|
|
selectedAccountId,
|
|
|
|
|
pendingEdits,
|
|
|
|
|
]);
|
2026-03-20 18:47:06 +05:30
|
|
|
|
|
|
|
|
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 (
|
|
|
|
|
<div className="my-4 max-w-lg overflow-hidden rounded-2xl border bg-muted/30 transition-all duration-300">
|
|
|
|
|
{/* Header */}
|
|
|
|
|
<div className="flex items-start justify-between px-5 pt-5 pb-4 select-none">
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-sm font-semibold text-foreground">
|
2026-03-21 11:18:35 +05:30
|
|
|
{phase === "rejected"
|
2026-03-20 18:47:06 +05:30
|
|
|
? "Gmail Draft Rejected"
|
2026-03-21 11:18:35 +05:30
|
|
|
: phase === "processing" || phase === "complete"
|
2026-03-20 18:47:06 +05:30
|
|
|
? "Gmail Draft Approved"
|
|
|
|
|
: "Create Gmail Draft"}
|
|
|
|
|
</p>
|
2026-03-21 13:20:13 +05:30
|
|
|
{phase === "processing" ? (
|
|
|
|
|
<TextShimmerLoader
|
|
|
|
|
text={pendingEdits ? "Creating draft with your changes" : "Creating draft"}
|
|
|
|
|
size="sm"
|
|
|
|
|
/>
|
2026-03-21 11:18:35 +05:30
|
|
|
) : phase === "complete" ? (
|
|
|
|
|
<p className="text-xs text-muted-foreground mt-0.5">
|
|
|
|
|
{pendingEdits ? "Draft created with your changes" : "Draft created"}
|
|
|
|
|
</p>
|
|
|
|
|
) : phase === "rejected" ? (
|
2026-03-21 13:20:13 +05:30
|
|
|
<p className="text-xs text-muted-foreground mt-0.5">Draft creation was cancelled</p>
|
2026-03-20 20:53:59 +05:30
|
|
|
) : (
|
|
|
|
|
<p className="text-xs text-muted-foreground mt-0.5">
|
2026-03-21 11:18:35 +05:30
|
|
|
Requires your approval to proceed
|
2026-03-20 20:53:59 +05:30
|
|
|
</p>
|
|
|
|
|
)}
|
2026-03-20 18:47:06 +05:30
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-03-21 11:18:35 +05:30
|
|
|
{phase === "pending" && canEdit && (
|
2026-03-20 18:47:06 +05:30
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
className="rounded-lg text-muted-foreground -mt-1 -mr-2"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setIsPanelOpen(true);
|
|
|
|
|
const extraFields: ExtraField[] = [
|
2026-03-21 13:20:13 +05:30
|
|
|
{
|
|
|
|
|
key: "to",
|
|
|
|
|
label: "To",
|
|
|
|
|
type: "emails",
|
|
|
|
|
value: pendingEdits?.to ?? args.to ?? "",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: "cc",
|
|
|
|
|
label: "CC",
|
|
|
|
|
type: "emails",
|
|
|
|
|
value: pendingEdits?.cc ?? args.cc ?? "",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: "bcc",
|
|
|
|
|
label: "BCC",
|
|
|
|
|
type: "emails",
|
|
|
|
|
value: pendingEdits?.bcc ?? args.bcc ?? "",
|
|
|
|
|
},
|
2026-03-20 18:47:06 +05:30
|
|
|
];
|
|
|
|
|
openHitlEditPanel({
|
2026-03-21 13:20:13 +05:30
|
|
|
title: pendingEdits?.subject ?? args.subject ?? "",
|
|
|
|
|
content: pendingEdits?.body ?? args.body ?? "",
|
2026-03-20 18:47:06 +05:30
|
|
|
toolName: "Gmail Draft",
|
|
|
|
|
extraFields,
|
|
|
|
|
onSave: (newTitle, newContent, extraFieldValues) => {
|
|
|
|
|
setIsPanelOpen(false);
|
|
|
|
|
const extras = extraFieldValues ?? {};
|
2026-03-20 21:38:19 +05:30
|
|
|
setPendingEdits({
|
|
|
|
|
subject: newTitle,
|
|
|
|
|
body: newContent,
|
|
|
|
|
to: extras.to ?? pendingEdits?.to ?? args.to ?? "",
|
|
|
|
|
cc: extras.cc ?? pendingEdits?.cc ?? args.cc ?? "",
|
|
|
|
|
bcc: extras.bcc ?? pendingEdits?.bcc ?? args.bcc ?? "",
|
2026-03-20 18:47:06 +05:30
|
|
|
});
|
|
|
|
|
},
|
2026-03-20 23:50:50 +05:30
|
|
|
onClose: () => setIsPanelOpen(false),
|
2026-03-20 18:47:06 +05:30
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Pen className="size-3.5" />
|
|
|
|
|
Edit
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-03-21 11:18:35 +05:30
|
|
|
{/* Account selector — real dropdown in pending */}
|
|
|
|
|
{phase === "pending" && interruptData.context && (
|
2026-03-20 18:47:06 +05:30
|
|
|
<>
|
|
|
|
|
<div className="mx-5 h-px bg-border/50" />
|
|
|
|
|
<div className="px-5 py-4 space-y-4 select-none">
|
|
|
|
|
{interruptData.context.error ? (
|
|
|
|
|
<p className="text-sm text-destructive">{interruptData.context.error}</p>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{accounts.length > 0 && (
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<p className="text-xs font-medium text-muted-foreground">
|
|
|
|
|
Gmail Account <span className="text-destructive">*</span>
|
|
|
|
|
</p>
|
|
|
|
|
<Select value={selectedAccountId} onValueChange={setSelectedAccountId}>
|
|
|
|
|
<SelectTrigger className="w-full">
|
|
|
|
|
<SelectValue placeholder="Select an account" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
{validAccounts.map((account) => (
|
|
|
|
|
<SelectItem key={account.id} value={String(account.id)}>
|
2026-03-20 22:02:35 +05:30
|
|
|
{account.name}
|
2026-03-20 18:47:06 +05:30
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
{expiredAccounts.map((a) => (
|
|
|
|
|
<div
|
|
|
|
|
key={a.id}
|
|
|
|
|
className="relative flex w-full cursor-default items-center gap-2 rounded-sm py-1.5 px-2 text-sm select-none opacity-50 pointer-events-none"
|
|
|
|
|
>
|
2026-03-20 22:02:35 +05:30
|
|
|
{a.name} (expired, retry after re-auth)
|
2026-03-20 18:47:06 +05:30
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-03-21 11:18:35 +05:30
|
|
|
{/* Email headers + body preview — visible in ALL phases */}
|
2026-03-20 18:47:06 +05:30
|
|
|
<div className="mx-5 h-px bg-border/50" />
|
|
|
|
|
<div className="px-5 pt-3 pb-2 space-y-1.5 select-none">
|
2026-03-20 21:38:19 +05:30
|
|
|
{(pendingEdits?.to ?? args.to) && (
|
2026-03-20 18:47:06 +05:30
|
|
|
<div className="flex items-center gap-1.5 text-xs text-muted-foreground">
|
|
|
|
|
<UserIcon className="size-3 shrink-0" />
|
2026-03-20 21:38:19 +05:30
|
|
|
<span>To: {pendingEdits?.to ?? args.to}</span>
|
2026-03-20 18:47:06 +05:30
|
|
|
</div>
|
|
|
|
|
)}
|
2026-03-20 21:38:19 +05:30
|
|
|
{(pendingEdits?.cc ?? args.cc) && (pendingEdits?.cc ?? args.cc)?.trim() !== "" && (
|
2026-03-20 18:47:06 +05:30
|
|
|
<div className="flex items-center gap-1.5 text-xs text-muted-foreground">
|
|
|
|
|
<UsersIcon className="size-3 shrink-0" />
|
2026-03-20 21:38:19 +05:30
|
|
|
<span>CC: {pendingEdits?.cc ?? args.cc}</span>
|
2026-03-20 18:47:06 +05:30
|
|
|
</div>
|
|
|
|
|
)}
|
2026-03-20 21:38:19 +05:30
|
|
|
{(pendingEdits?.bcc ?? args.bcc) && (pendingEdits?.bcc ?? args.bcc)?.trim() !== "" && (
|
2026-03-20 18:47:06 +05:30
|
|
|
<div className="flex items-center gap-1.5 text-xs text-muted-foreground">
|
|
|
|
|
<UsersIcon className="size-3 shrink-0" />
|
2026-03-20 21:38:19 +05:30
|
|
|
<span>BCC: {pendingEdits?.bcc ?? args.bcc}</span>
|
2026-03-20 18:47:06 +05:30
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="px-5 pt-1">
|
2026-03-20 21:38:19 +05:30
|
|
|
{(pendingEdits?.subject ?? args.subject) != null && (
|
2026-03-21 13:20:13 +05:30
|
|
|
<p className="text-sm font-medium text-foreground">
|
|
|
|
|
{pendingEdits?.subject ?? args.subject}
|
|
|
|
|
</p>
|
2026-03-20 18:47:06 +05:30
|
|
|
)}
|
2026-03-20 21:38:19 +05:30
|
|
|
{(pendingEdits?.body ?? args.body) != null && (
|
2026-03-20 18:47:06 +05:30
|
|
|
<div
|
|
|
|
|
className="mt-2 max-h-[7rem] overflow-hidden text-sm"
|
|
|
|
|
style={{
|
|
|
|
|
maskImage: "linear-gradient(to bottom, black 50%, transparent 100%)",
|
|
|
|
|
WebkitMaskImage: "linear-gradient(to bottom, black 50%, transparent 100%)",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<PlateEditor
|
2026-03-20 21:38:19 +05:30
|
|
|
markdown={String(pendingEdits?.body ?? args.body)}
|
2026-03-20 18:47:06 +05:30
|
|
|
readOnly
|
|
|
|
|
preset="readonly"
|
|
|
|
|
editorVariant="none"
|
|
|
|
|
className="h-auto [&_[data-slate-editor]]:!min-h-0 [&_[data-slate-editor]>*:first-child]:!mt-0"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-03-21 11:18:35 +05:30
|
|
|
{/* Action buttons — only in pending */}
|
|
|
|
|
{phase === "pending" && (
|
2026-03-20 18:47:06 +05:30
|
|
|
<>
|
|
|
|
|
<div className="mx-5 h-px bg-border/50" />
|
|
|
|
|
<div className="px-5 py-4 flex items-center gap-2 select-none">
|
|
|
|
|
{allowedDecisions.includes("approve") && (
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
className="rounded-lg gap-1.5"
|
|
|
|
|
onClick={handleApprove}
|
2026-03-20 23:50:50 +05:30
|
|
|
disabled={!canApprove || isPanelOpen}
|
2026-03-20 18:47:06 +05:30
|
|
|
>
|
|
|
|
|
Approve
|
|
|
|
|
<CornerDownLeftIcon className="size-3 opacity-60" />
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
{allowedDecisions.includes("reject") && (
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
className="rounded-lg text-muted-foreground"
|
2026-03-20 23:50:50 +05:30
|
|
|
disabled={isPanelOpen}
|
2026-03-20 18:47:06 +05:30
|
|
|
onClick={() => {
|
2026-03-21 11:18:35 +05:30
|
|
|
setRejected();
|
2026-03-20 18:47:06 +05:30
|
|
|
onDecision({ type: "reject", message: "User rejected the action." });
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Reject
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ErrorCard({ result }: { result: ErrorResult }) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="my-4 max-w-lg overflow-hidden rounded-2xl border bg-muted/30 select-none">
|
|
|
|
|
<div className="px-5 pt-5 pb-4">
|
|
|
|
|
<p className="text-sm font-semibold text-destructive">Failed to create Gmail draft</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="mx-5 h-px bg-border/50" />
|
|
|
|
|
<div className="px-5 py-4">
|
|
|
|
|
<p className="text-sm text-muted-foreground">{result.message}</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function AuthErrorCard({ result }: { result: AuthErrorResult }) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="my-4 max-w-lg overflow-hidden rounded-2xl border bg-muted/30 select-none">
|
|
|
|
|
<div className="px-5 pt-5 pb-4">
|
2026-03-21 13:20:13 +05:30
|
|
|
<p className="text-sm font-semibold text-destructive">Gmail authentication expired</p>
|
2026-03-20 18:47:06 +05:30
|
|
|
</div>
|
|
|
|
|
<div className="mx-5 h-px bg-border/50" />
|
|
|
|
|
<div className="px-5 py-4">
|
|
|
|
|
<p className="text-sm text-muted-foreground">{result.message}</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 19:36:00 +05:30
|
|
|
function InsufficientPermissionsCard({ result }: { result: InsufficientPermissionsResult }) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="my-4 max-w-lg overflow-hidden rounded-2xl border bg-muted/30 select-none">
|
|
|
|
|
<div className="px-5 pt-5 pb-4">
|
|
|
|
|
<p className="text-sm font-semibold text-destructive">
|
|
|
|
|
Additional Gmail permissions required
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="mx-5 h-px bg-border/50" />
|
|
|
|
|
<div className="px-5 py-4">
|
|
|
|
|
<p className="text-sm text-muted-foreground">{result.message}</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 18:47:06 +05:30
|
|
|
function SuccessCard({ result }: { result: SuccessResult }) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="my-4 max-w-lg overflow-hidden rounded-2xl border bg-muted/30 select-none">
|
|
|
|
|
<div className="px-5 pt-5 pb-4">
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<p className="text-sm font-semibold text-foreground">
|
|
|
|
|
{result.message || "Gmail draft created successfully"}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const CreateGmailDraftToolUI = makeAssistantToolUI<
|
|
|
|
|
{ to: string; subject: string; body: string; cc?: string; bcc?: string },
|
|
|
|
|
CreateGmailDraftResult
|
|
|
|
|
>({
|
|
|
|
|
toolName: "create_gmail_draft",
|
2026-03-21 11:18:35 +05:30
|
|
|
render: function CreateGmailDraftUI({ args, result }) {
|
2026-03-20 18:47:06 +05:30
|
|
|
if (!result) return null;
|
|
|
|
|
|
|
|
|
|
if (isInterruptResult(result)) {
|
|
|
|
|
return (
|
|
|
|
|
<ApprovalCard
|
|
|
|
|
args={args}
|
|
|
|
|
interruptData={result}
|
|
|
|
|
onDecision={(decision) => {
|
|
|
|
|
window.dispatchEvent(
|
|
|
|
|
new CustomEvent("hitl-decision", { detail: { decisions: [decision] } })
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
typeof result === "object" &&
|
|
|
|
|
result !== null &&
|
|
|
|
|
"status" in result &&
|
|
|
|
|
(result as { status: string }).status === "rejected"
|
|
|
|
|
) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isAuthErrorResult(result)) return <AuthErrorCard result={result} />;
|
2026-03-20 19:36:00 +05:30
|
|
|
if (isInsufficientPermissionsResult(result))
|
|
|
|
|
return <InsufficientPermissionsCard result={result} />;
|
2026-03-20 18:47:06 +05:30
|
|
|
if (isErrorResult(result)) return <ErrorCard result={result} />;
|
|
|
|
|
|
|
|
|
|
return <SuccessCard result={result as SuccessResult} />;
|
|
|
|
|
},
|
|
|
|
|
});
|