2025-12-30 16:38:57 +02:00
|
|
|
import {
|
|
|
|
|
ActionBarPrimitive,
|
2026-03-24 02:22:51 +05:30
|
|
|
AuiIf,
|
2025-12-30 16:38:57 +02:00
|
|
|
ErrorPrimitive,
|
|
|
|
|
MessagePrimitive,
|
2026-03-24 02:22:51 +05:30
|
|
|
useAuiState,
|
2025-12-30 16:38:57 +02:00
|
|
|
} from "@assistant-ui/react";
|
2026-03-10 14:00:29 +05:30
|
|
|
import { useAtomValue } from "jotai";
|
2026-01-19 16:50:51 -08:00
|
|
|
import { CheckIcon, CopyIcon, DownloadIcon, MessageSquare, RefreshCwIcon } from "lucide-react";
|
2025-12-30 16:38:57 +02:00
|
|
|
import type { FC } from "react";
|
2026-03-24 02:23:05 +05:30
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
2026-03-10 16:17:12 +05:30
|
|
|
import { commentsEnabledAtom, targetCommentIdAtom } from "@/atoms/chat/current-thread.atom";
|
2026-01-16 15:09:51 +02:00
|
|
|
import { activeSearchSpaceIdAtom } from "@/atoms/search-spaces/search-space-query.atoms";
|
2025-12-30 16:38:57 +02:00
|
|
|
import { MarkdownText } from "@/components/assistant-ui/markdown-text";
|
|
|
|
|
import { ToolFallback } from "@/components/assistant-ui/tool-fallback";
|
|
|
|
|
import { TooltipIconButton } from "@/components/assistant-ui/tooltip-icon-button";
|
2026-01-16 15:09:51 +02:00
|
|
|
import { CommentPanelContainer } from "@/components/chat-comments/comment-panel-container/comment-panel-container";
|
2026-01-19 16:50:51 -08:00
|
|
|
import { CommentSheet } from "@/components/chat-comments/comment-sheet/comment-sheet";
|
2026-03-24 02:22:51 +05:30
|
|
|
import { CreateConfluencePageToolUI, DeleteConfluencePageToolUI, UpdateConfluencePageToolUI } from "@/components/tool-ui/confluence";
|
|
|
|
|
import { DisplayImageToolUI } from "@/components/tool-ui/display-image";
|
2026-03-24 16:28:11 +05:30
|
|
|
import { GenerateImageToolUI } from "@/components/tool-ui/generate-image";
|
2026-03-24 02:22:51 +05:30
|
|
|
import { GeneratePodcastToolUI } from "@/components/tool-ui/generate-podcast";
|
|
|
|
|
import { GenerateReportToolUI } from "@/components/tool-ui/generate-report";
|
|
|
|
|
import { GenerateVideoPresentationToolUI } from "@/components/tool-ui/video-presentation";
|
|
|
|
|
import { CreateGmailDraftToolUI, SendGmailEmailToolUI, TrashGmailEmailToolUI, UpdateGmailDraftToolUI } from "@/components/tool-ui/gmail";
|
|
|
|
|
import { CreateCalendarEventToolUI, DeleteCalendarEventToolUI, UpdateCalendarEventToolUI } from "@/components/tool-ui/google-calendar";
|
|
|
|
|
import { CreateGoogleDriveFileToolUI, DeleteGoogleDriveFileToolUI } from "@/components/tool-ui/google-drive";
|
|
|
|
|
import { CreateJiraIssueToolUI, DeleteJiraIssueToolUI, UpdateJiraIssueToolUI } from "@/components/tool-ui/jira";
|
|
|
|
|
import { CreateLinearIssueToolUI, DeleteLinearIssueToolUI, UpdateLinearIssueToolUI } from "@/components/tool-ui/linear";
|
|
|
|
|
import { CreateNotionPageToolUI, DeleteNotionPageToolUI, UpdateNotionPageToolUI } from "@/components/tool-ui/notion";
|
|
|
|
|
import { SandboxExecuteToolUI } from "@/components/tool-ui/sandbox-execute";
|
|
|
|
|
import { RecallMemoryToolUI, SaveMemoryToolUI } from "@/components/tool-ui/user-memory";
|
2026-01-16 15:09:51 +02:00
|
|
|
import { useComments } from "@/hooks/use-comments";
|
2026-01-19 16:50:51 -08:00
|
|
|
import { useMediaQuery } from "@/hooks/use-media-query";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2025-12-30 16:38:57 +02:00
|
|
|
|
|
|
|
|
export const MessageError: FC = () => {
|
|
|
|
|
return (
|
|
|
|
|
<MessagePrimitive.Error>
|
|
|
|
|
<ErrorPrimitive.Root className="aui-message-error-root mt-2 rounded-md border border-destructive bg-destructive/10 p-3 text-destructive text-sm dark:bg-destructive/5 dark:text-red-200">
|
|
|
|
|
<ErrorPrimitive.Message className="aui-message-error-message line-clamp-2" />
|
|
|
|
|
</ErrorPrimitive.Root>
|
|
|
|
|
</MessagePrimitive.Error>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const AssistantMessageInner: FC = () => {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<div className="aui-assistant-message-content wrap-break-word px-2 text-foreground leading-relaxed">
|
|
|
|
|
<MessagePrimitive.Parts
|
|
|
|
|
components={{
|
|
|
|
|
Text: MarkdownText,
|
2026-03-24 02:22:51 +05:30
|
|
|
tools: {
|
|
|
|
|
by_name: {
|
|
|
|
|
generate_report: GenerateReportToolUI,
|
|
|
|
|
generate_podcast: GeneratePodcastToolUI,
|
|
|
|
|
generate_video_presentation: GenerateVideoPresentationToolUI,
|
|
|
|
|
display_image: DisplayImageToolUI,
|
2026-03-24 16:28:11 +05:30
|
|
|
generate_image: GenerateImageToolUI,
|
2026-03-24 02:22:51 +05:30
|
|
|
save_memory: SaveMemoryToolUI,
|
|
|
|
|
recall_memory: RecallMemoryToolUI,
|
|
|
|
|
execute: SandboxExecuteToolUI,
|
|
|
|
|
create_notion_page: CreateNotionPageToolUI,
|
|
|
|
|
update_notion_page: UpdateNotionPageToolUI,
|
|
|
|
|
delete_notion_page: DeleteNotionPageToolUI,
|
|
|
|
|
create_linear_issue: CreateLinearIssueToolUI,
|
|
|
|
|
update_linear_issue: UpdateLinearIssueToolUI,
|
|
|
|
|
delete_linear_issue: DeleteLinearIssueToolUI,
|
|
|
|
|
create_google_drive_file: CreateGoogleDriveFileToolUI,
|
|
|
|
|
delete_google_drive_file: DeleteGoogleDriveFileToolUI,
|
|
|
|
|
create_calendar_event: CreateCalendarEventToolUI,
|
|
|
|
|
update_calendar_event: UpdateCalendarEventToolUI,
|
|
|
|
|
delete_calendar_event: DeleteCalendarEventToolUI,
|
|
|
|
|
create_gmail_draft: CreateGmailDraftToolUI,
|
|
|
|
|
update_gmail_draft: UpdateGmailDraftToolUI,
|
|
|
|
|
send_gmail_email: SendGmailEmailToolUI,
|
|
|
|
|
trash_gmail_email: TrashGmailEmailToolUI,
|
|
|
|
|
create_jira_issue: CreateJiraIssueToolUI,
|
|
|
|
|
update_jira_issue: UpdateJiraIssueToolUI,
|
|
|
|
|
delete_jira_issue: DeleteJiraIssueToolUI,
|
|
|
|
|
create_confluence_page: CreateConfluencePageToolUI,
|
|
|
|
|
update_confluence_page: UpdateConfluencePageToolUI,
|
|
|
|
|
delete_confluence_page: DeleteConfluencePageToolUI,
|
|
|
|
|
},
|
|
|
|
|
Fallback: ToolFallback,
|
|
|
|
|
},
|
2025-12-30 16:38:57 +02:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<MessageError />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="aui-assistant-message-footer mt-1 mb-5 ml-2 flex">
|
|
|
|
|
<AssistantActionBar />
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-16 15:09:51 +02:00
|
|
|
function parseMessageId(assistantUiMessageId: string | undefined): number | null {
|
|
|
|
|
if (!assistantUiMessageId) return null;
|
|
|
|
|
const match = assistantUiMessageId.match(/^msg-(\d+)$/);
|
|
|
|
|
return match ? Number.parseInt(match[1], 10) : null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 16:38:57 +02:00
|
|
|
export const AssistantMessage: FC = () => {
|
2026-01-19 16:50:51 -08:00
|
|
|
const [isSheetOpen, setIsSheetOpen] = useState(false);
|
2026-03-10 14:00:29 +05:30
|
|
|
const [isInlineOpen, setIsInlineOpen] = useState(false);
|
2026-01-19 14:37:06 +02:00
|
|
|
const messageRef = useRef<HTMLDivElement>(null);
|
2026-03-10 14:00:29 +05:30
|
|
|
const commentPanelRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const commentTriggerRef = useRef<HTMLButtonElement>(null);
|
2026-03-24 02:22:51 +05:30
|
|
|
const messageId = useAuiState(({ message }) => message?.id);
|
2026-01-16 15:09:51 +02:00
|
|
|
const searchSpaceId = useAtomValue(activeSearchSpaceIdAtom);
|
|
|
|
|
const dbMessageId = parseMessageId(messageId);
|
2026-01-19 14:37:06 +02:00
|
|
|
const commentsEnabled = useAtomValue(commentsEnabledAtom);
|
|
|
|
|
|
2026-03-10 14:00:29 +05:30
|
|
|
// Desktop: >= 1024px (inline expandable), Medium: 768px-1023px (right sheet), Mobile: <768px (bottom sheet)
|
2026-01-19 16:50:51 -08:00
|
|
|
const isMediumScreen = useMediaQuery("(min-width: 768px) and (max-width: 1023px)");
|
|
|
|
|
const isDesktop = useMediaQuery("(min-width: 1024px)");
|
|
|
|
|
|
2026-03-24 02:22:51 +05:30
|
|
|
const isThreadRunning = useAuiState(({ thread }) => thread.isRunning);
|
|
|
|
|
const isLastMessage = useAuiState(({ message }) => message?.isLast ?? false);
|
2026-01-19 14:37:06 +02:00
|
|
|
const isMessageStreaming = isThreadRunning && isLastMessage;
|
2026-01-16 15:09:51 +02:00
|
|
|
|
2026-01-27 22:14:02 +05:30
|
|
|
const { data: commentsData, isSuccess: commentsLoaded } = useComments({
|
2026-01-16 15:09:51 +02:00
|
|
|
messageId: dbMessageId ?? 0,
|
|
|
|
|
enabled: !!dbMessageId,
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-27 22:14:02 +05:30
|
|
|
const targetCommentId = useAtomValue(targetCommentIdAtom);
|
|
|
|
|
|
|
|
|
|
const hasTargetComment = useMemo(() => {
|
|
|
|
|
if (!targetCommentId || !commentsData?.comments) return false;
|
|
|
|
|
return commentsData.comments.some(
|
|
|
|
|
(c) => c.id === targetCommentId || c.replies?.some((r) => r.id === targetCommentId)
|
|
|
|
|
);
|
|
|
|
|
}, [targetCommentId, commentsData]);
|
|
|
|
|
|
2026-01-19 16:50:51 -08:00
|
|
|
const commentCount = commentsData?.total_count ?? 0;
|
|
|
|
|
const hasComments = commentCount > 0;
|
2026-01-19 14:37:06 +02:00
|
|
|
|
2026-03-10 14:00:29 +05:30
|
|
|
const showCommentTrigger = searchSpaceId && commentsEnabled && !isMessageStreaming && dbMessageId;
|
2026-01-19 16:50:51 -08:00
|
|
|
|
2026-03-10 14:00:29 +05:30
|
|
|
// Close floating panel when clicking outside (but not on portaled popover/dropdown content)
|
2026-01-19 14:37:06 +02:00
|
|
|
useEffect(() => {
|
2026-03-10 14:00:29 +05:30
|
|
|
if (!isInlineOpen) return;
|
|
|
|
|
const handleClickOutside = (e: MouseEvent) => {
|
|
|
|
|
const target = e.target as Element;
|
|
|
|
|
if (
|
|
|
|
|
commentPanelRef.current?.contains(target) ||
|
|
|
|
|
commentTriggerRef.current?.contains(target) ||
|
|
|
|
|
target.closest?.("[data-radix-popper-content-wrapper]")
|
2026-03-10 16:17:12 +05:30
|
|
|
)
|
|
|
|
|
return;
|
2026-03-10 14:00:29 +05:30
|
|
|
setIsInlineOpen(false);
|
|
|
|
|
};
|
|
|
|
|
document.addEventListener("mousedown", handleClickOutside);
|
|
|
|
|
return () => document.removeEventListener("mousedown", handleClickOutside);
|
|
|
|
|
}, [isInlineOpen]);
|
|
|
|
|
|
|
|
|
|
// Auto-open floating panel on desktop when this message has the target comment
|
2026-01-27 22:14:02 +05:30
|
|
|
useEffect(() => {
|
2026-03-10 14:00:29 +05:30
|
|
|
if (hasTargetComment && isDesktop && commentsLoaded) {
|
|
|
|
|
setIsInlineOpen(true);
|
2026-01-27 22:14:02 +05:30
|
|
|
const timeoutId = setTimeout(() => {
|
|
|
|
|
messageRef.current?.scrollIntoView({ behavior: "smooth", block: "center" });
|
|
|
|
|
}, 100);
|
|
|
|
|
return () => clearTimeout(timeoutId);
|
|
|
|
|
}
|
|
|
|
|
}, [hasTargetComment, isDesktop, commentsLoaded]);
|
|
|
|
|
|
2026-03-10 14:00:29 +05:30
|
|
|
// Auto-open sheet on mobile/tablet when this message has the target comment
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (hasTargetComment && !isDesktop && commentsLoaded) {
|
|
|
|
|
setIsSheetOpen(true);
|
|
|
|
|
}
|
|
|
|
|
}, [hasTargetComment, isDesktop, commentsLoaded]);
|
2026-01-19 16:50:51 -08:00
|
|
|
|
|
|
|
|
const sheetSide = isMediumScreen ? "right" : "bottom";
|
|
|
|
|
|
2025-12-30 16:38:57 +02:00
|
|
|
return (
|
|
|
|
|
<MessagePrimitive.Root
|
2026-01-19 14:37:06 +02:00
|
|
|
ref={messageRef}
|
2026-01-16 15:09:51 +02:00
|
|
|
className="aui-assistant-message-root group fade-in slide-in-from-bottom-1 relative mx-auto w-full max-w-(--thread-max-width) animate-in py-3 duration-150"
|
2025-12-30 16:38:57 +02:00
|
|
|
data-role="assistant"
|
|
|
|
|
>
|
2026-03-10 14:00:29 +05:30
|
|
|
{/* Comment trigger — right-aligned, just below user query on all screen sizes */}
|
|
|
|
|
{showCommentTrigger && (
|
|
|
|
|
<div className="mr-2 mb-1 flex justify-end">
|
2026-01-19 16:50:51 -08:00
|
|
|
<button
|
2026-03-10 14:00:29 +05:30
|
|
|
ref={isDesktop ? commentTriggerRef : undefined}
|
2026-01-19 16:50:51 -08:00
|
|
|
type="button"
|
2026-03-10 16:17:12 +05:30
|
|
|
onClick={
|
|
|
|
|
isDesktop ? () => setIsInlineOpen((prev) => !prev) : () => setIsSheetOpen(true)
|
|
|
|
|
}
|
2026-01-19 16:50:51 -08:00
|
|
|
className={cn(
|
2026-03-10 14:00:29 +05:30
|
|
|
"flex items-center gap-1.5 rounded-full px-3 py-1 text-sm transition-colors",
|
|
|
|
|
isDesktop && isInlineOpen
|
|
|
|
|
? "bg-primary/10 text-primary"
|
|
|
|
|
: hasComments
|
|
|
|
|
? "text-primary hover:bg-primary/10"
|
|
|
|
|
: "text-muted-foreground hover:text-foreground hover:bg-muted"
|
2026-01-19 16:50:51 -08:00
|
|
|
)}
|
|
|
|
|
>
|
2026-03-10 14:00:29 +05:30
|
|
|
<MessageSquare className={cn("size-3.5", hasComments && "fill-current")} />
|
2026-01-19 16:50:51 -08:00
|
|
|
{hasComments ? (
|
2026-01-20 00:32:31 -08:00
|
|
|
<span>
|
|
|
|
|
{commentCount} {commentCount === 1 ? "comment" : "comments"}
|
|
|
|
|
</span>
|
2026-01-19 16:50:51 -08:00
|
|
|
) : (
|
|
|
|
|
<span>Add comment</span>
|
|
|
|
|
)}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-03-10 14:00:29 +05:30
|
|
|
{/* Desktop floating comment panel — overlays on top of chat content */}
|
|
|
|
|
{showCommentTrigger && isDesktop && isInlineOpen && dbMessageId && (
|
|
|
|
|
<div
|
|
|
|
|
ref={commentPanelRef}
|
|
|
|
|
className="absolute right-0 top-10 z-30 w-full max-w-md animate-in fade-in slide-in-from-top-2 duration-200"
|
|
|
|
|
>
|
2026-03-10 16:17:12 +05:30
|
|
|
<CommentPanelContainer messageId={dbMessageId} isOpen={true} variant="inline" />
|
2026-03-10 14:00:29 +05:30
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<AssistantMessageInner />
|
|
|
|
|
|
|
|
|
|
{/* Comment sheet — bottom for mobile, right for medium screens */}
|
2026-01-19 16:50:51 -08:00
|
|
|
{showCommentTrigger && !isDesktop && (
|
|
|
|
|
<CommentSheet
|
|
|
|
|
messageId={dbMessageId}
|
|
|
|
|
isOpen={isSheetOpen}
|
|
|
|
|
onOpenChange={setIsSheetOpen}
|
|
|
|
|
commentCount={commentCount}
|
|
|
|
|
side={sheetSide}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2025-12-30 16:38:57 +02:00
|
|
|
</MessagePrimitive.Root>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const AssistantActionBar: FC = () => {
|
2026-03-24 02:22:51 +05:30
|
|
|
const isLast = useAuiState((s) => s.message.isLast);
|
2026-02-04 18:04:53 +02:00
|
|
|
|
2025-12-30 16:38:57 +02:00
|
|
|
return (
|
2026-03-24 02:22:51 +05:30
|
|
|
<ActionBarPrimitive.Root
|
2025-12-30 16:38:57 +02:00
|
|
|
hideWhenRunning
|
|
|
|
|
autohide="not-last"
|
|
|
|
|
autohideFloat="single-branch"
|
2026-03-21 21:10:19 +05:30
|
|
|
className="aui-assistant-action-bar-root -ml-1 col-start-3 row-start-2 flex gap-1 text-muted-foreground md:data-floating:absolute md:data-floating:rounded-md md:data-floating:p-1 [&>button]:opacity-100 md:[&>button]:opacity-[var(--aui-button-opacity,1)]"
|
2025-12-30 16:38:57 +02:00
|
|
|
>
|
2026-03-24 02:22:51 +05:30
|
|
|
<ActionBarPrimitive.Copy asChild>
|
2025-12-30 16:38:57 +02:00
|
|
|
<TooltipIconButton tooltip="Copy">
|
2026-03-24 02:22:51 +05:30
|
|
|
<AuiIf condition={({ message }) => message.isCopied}>
|
2025-12-30 16:38:57 +02:00
|
|
|
<CheckIcon />
|
2026-03-24 02:22:51 +05:30
|
|
|
</AuiIf>
|
|
|
|
|
<AuiIf condition={({ message }) => !message.isCopied}>
|
2025-12-30 16:38:57 +02:00
|
|
|
<CopyIcon />
|
2026-03-24 02:22:51 +05:30
|
|
|
</AuiIf>
|
2025-12-30 16:38:57 +02:00
|
|
|
</TooltipIconButton>
|
|
|
|
|
</ActionBarPrimitive.Copy>
|
2026-03-24 02:22:51 +05:30
|
|
|
<ActionBarPrimitive.ExportMarkdown asChild>
|
2026-03-21 21:10:19 +05:30
|
|
|
<TooltipIconButton tooltip="Download">
|
2025-12-30 16:38:57 +02:00
|
|
|
<DownloadIcon />
|
|
|
|
|
</TooltipIconButton>
|
|
|
|
|
</ActionBarPrimitive.ExportMarkdown>
|
2026-03-24 02:22:51 +05:30
|
|
|
{/* Only allow regenerating the last assistant message */}
|
|
|
|
|
{isLast && (
|
2026-02-04 18:04:53 +02:00
|
|
|
<ActionBarPrimitive.Reload asChild>
|
|
|
|
|
<TooltipIconButton tooltip="Refresh">
|
|
|
|
|
<RefreshCwIcon />
|
|
|
|
|
</TooltipIconButton>
|
|
|
|
|
</ActionBarPrimitive.Reload>
|
|
|
|
|
)}
|
2026-03-24 02:22:51 +05:30
|
|
|
</ActionBarPrimitive.Root>
|
|
|
|
|
);
|
2025-12-30 16:38:57 +02:00
|
|
|
};
|