"use client"; import { useState } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { MessageSquare, Briefcase, Scale, Bot, User, Loader2, Send } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { askContractQuestionAction } from "@/features/contracts/api/contract.action"; interface Contract { id: string; fileName: string; } interface ChatMessage { role: "user" | "assistant"; content: string; } interface ContractChatModalProps { isOpen: boolean; onOpenChange: (open: boolean) => void; contract: Contract | null; renderRichParagraphs: (text: string, prefix: string) => React.ReactNode[]; } export function ContractChatModal({ isOpen, onOpenChange, contract, renderRichParagraphs, }: ContractChatModalProps) { const [question, setQuestion] = useState(""); const [isAsking, setIsAsking] = useState(false); const [messages, setMessages] = useState([ { role: "assistant", content: "Ask me anything about this contract. I will answer based on the file analysis.", }, ]); const quickQuestions = [ "What are the main obligations and deadlines?", "What are the non-compliance risks under general EU/US principles?", "What are the most important exclusions and liabilities?", ]; const handleAskQuestion = async () => { if (!contract) return; const trimmedQuestion = question.trim(); if (!trimmedQuestion) return; setMessages((prev) => [...prev, { role: "user", content: trimmedQuestion }]); setQuestion(""); setIsAsking(true); try { const result = await askContractQuestionAction(contract.id, trimmedQuestion); if (result.success && result.answer) { setMessages((prev) => [...prev, { role: "assistant", content: result.answer as string }]); } else { const errorMessage = result.error || "Failed to get AI response"; setMessages((prev) => [...prev, { role: "assistant", content: `Error: ${errorMessage}` }]); } } catch (error) { const fallbackMessage = error instanceof Error ? error.message : "Unknown error occurred"; setMessages((prev) => [...prev, { role: "assistant", content: `Error: ${fallbackMessage}` }]); } finally { setIsAsking(false); } }; return ( Ask About This File {contract && (

Contract Intelligence Assistant

{contract.fileName}

Business Legal Context

Quick prompts

{quickQuestions.map((quickQuestion) => ( ))}
{messages.map((message, index) => (
{message.role === "assistant" && ( )}
{message.role === "assistant" ? renderRichParagraphs(message.content, `chat-assistant-${index}`) : message.content}
{message.role === "user" && ( )}
))} {isAsking && (
Preparing a professional legal-business answer...
)}