"use client";
import { memo, useState, useCallback } from "react";
import ReactMarkdown from "react-markdown";
import { IconWand, IconMessageDots, IconFile } from "@tabler/icons-react";
import { cn } from "@/lib/utils";
import type { Message } from "@/lib/types/http";
import { RichBlocks } from "@/components/task/chat/messages/rich-blocks";
import { MessageActions } from "@/components/task/chat/messages/message-actions";
import { useMessageNavigation } from "@/hooks/use-message-navigation";
import { SenderTaskBadge, type SenderTaskInfo } from "./sender-task-badge";
import {
markdownComponents,
normalizeMarkdown,
remarkPlugins,
} from "@/components/shared/markdown-components";
import { ImagePreviewDialog } from "@/components/task/chat/image-preview-dialog";
import {
WorkflowStepMessageBadge,
workflowMessageInfoFromMetadata,
type WorkflowMessageMetadata,
type WorkflowStepMessageInfo,
} from "./workflow-step-message-badge";
type ChatMessageProps = {
comment: Message;
label: string;
className: string;
showRichBlocks?: boolean;
allMessages?: Message[];
onScrollToMessage?: (messageId: string) => void;
};
// Regex to match @file references (file paths after @)
// Matches @path/to/file.ext or @file.ext patterns
const FILE_REF_REGEX = /@([\w./-]+\.[\w]+|[\w/-]+)/g;
/**
* Renders content with file references highlighted in code style
*/
function renderContentWithFileRefs(content: string): React.ReactNode[] {
const parts: React.ReactNode[] = [];
let lastIndex = 0;
let match;
let keyIndex = 0;
FILE_REF_REGEX.lastIndex = 0; // Reset regex state
while ((match = FILE_REF_REGEX.exec(content)) !== null) {
// Add text before the match
if (match.index > lastIndex) {
parts.push(content.slice(lastIndex, match.index));
}
// Add the file reference with code styling
const filePath = match[1];
parts.push(
@{filePath}
,
);
lastIndex = match.index + match[0].length;
}
// Add remaining text after last match
if (lastIndex < content.length) {
parts.push(content.slice(lastIndex));
}
return parts.length > 0 ? parts : [content];
}
// ── Markdown component overrides imported from shared/markdown-components ─────
function renderUserMessageBody(
hasContent: boolean,
showRaw: boolean,
hasAttachments: boolean,
content: string,
rawContent?: string,
): React.ReactNode {
if (hasContent && showRaw) {
return
{rawContent || content};
}
if (hasContent) {
return (
(empty)
; } return null; } // ── User message sub-component ────────────────────────────────────── type UserMessageProps = { comment: Message; showRaw: boolean; onToggleRaw: () => void; allMessages?: Message[]; onScrollToMessage?: (messageId: string) => void; }; type UserMessageMetadata = WorkflowMessageMetadata & { attachments?: Array<{ type: string; data: string; mime_type: string; name?: string }>; plan_mode?: boolean; has_review_comments?: boolean; has_hidden_prompts?: boolean; context_files?: Array<{ path: string; name: string }>; sender_task_id?: string; sender_task_title?: string; sender_session_id?: string; }; function parseUserMessageMetadata(comment: Message) { const metadata = comment.metadata as UserMessageMetadata | undefined; const imageAttachments = (metadata?.attachments || []).filter((att) => att.type === "image"); const fileAttachments = (metadata?.attachments || []).filter((att) => att.type === "resource"); const contextFiles = metadata?.context_files || []; const hasPlanMode = !!metadata?.plan_mode; const hasReviewComments = !!metadata?.has_review_comments; const hasHiddenPrompts = !!metadata?.has_hidden_prompts; const hasContent = !!(comment.content && comment.content.trim() !== ""); const hasAttachments = imageAttachments.length > 0 || fileAttachments.length > 0; const senderTask: SenderTaskInfo | null = metadata?.sender_task_id ? { id: metadata.sender_task_id, snapshotTitle: metadata.sender_task_title || "" } : null; const workflowMessage = workflowMessageInfoFromMetadata(metadata); return { imageAttachments, fileAttachments, contextFiles, hasPlanMode, hasReviewComments, hasHiddenPrompts, hasContent, hasAttachments, senderTask, workflowMessage, }; } function UserContextBadges({ hasPlanMode, hasReviewComments, contextFiles, senderTask, workflowMessage, }: { hasPlanMode: boolean; hasReviewComments: boolean; contextFiles: Array<{ path: string; name: string }>; senderTask: SenderTaskInfo | null; workflowMessage: WorkflowStepMessageInfo | null; }) { if ( !hasPlanMode && !hasReviewComments && contextFiles.length === 0 && !senderTask && !workflowMessage ) return null; return (
{comment.content || "(empty)"}
) : (
{comment.requests_input ? ( Needs input ) : null}
{comment.content ? renderContentWithFileRefs(comment.content) : "(empty)"}