File size: 6,506 Bytes
ff0e173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use client';

import { motion } from 'motion/react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import FileTypeIcon from './FileTypeIcon';
import Badge from './Badge';
import { ChatMessage } from '@/lib/kb-data';

interface SourceCardProps {
  name: string;
  type: string;
}

function SourceCard({ name, type }: SourceCardProps) {
  const isFileType = ['PDF', 'DOC', 'DOCX', 'EXCEL', 'XLS', 'XLSX', 'CSV'].includes(
    type.toUpperCase()
  );

  const getBadgeColor = (): 'danger' | 'success' | 'info' | 'purple' | 'neutral' => {
    switch (type.toUpperCase()) {
      case 'PDF':
        return 'danger';
      case 'EXCEL':
      case 'XLS':
        return 'success';
      case 'DOCX':
        return 'info';
      case 'Q&A':
        return 'purple';
      default:
        return 'neutral';
    }
  };

  return (
    <div className="inline-flex items-center gap-2 px-3 py-1.5 bg-white hover:bg-neutral-50 border border-neutral-200 rounded-full text-xs text-neutral-600 shadow-sm transition-colors duration-200 cursor-default">
      <span className="flex-shrink-0 flex items-center justify-center">
        {isFileType ? (
          <FileTypeIcon type={type} size={16} className="block" />
        ) : (
          <span className="text-[13px] text-violet-500">*</span>
        )}
      </span>
      <span className="font-medium truncate max-w-[150px]" title={name}>
        {name}
      </span>
      <Badge variant={getBadgeColor()} className="px-1.5 py-0 text-[8px] font-bold">
        {type}
      </Badge>
    </div>
  );
}

interface MessageBubbleProps {
  message: ChatMessage;
}

function MarkdownContent({ text, compact = false }: { text: string; compact?: boolean }) {
  return (
    <ReactMarkdown
      remarkPlugins={[remarkGfm]}
      components={{
        h1: ({ children }) => (
          <h1 className="mt-6 first:mt-0 mb-2 text-xl font-bold tracking-tight text-neutral-950">
            {children}
          </h1>
        ),
        h2: ({ children }) => (
          <h2 className="mt-5 first:mt-0 mb-2 text-lg font-bold tracking-tight text-neutral-950">
            {children}
          </h2>
        ),
        h3: ({ children }) => (
          <h3 className="mt-5 first:mt-0 mb-1 text-[13px] font-bold uppercase tracking-wide text-neutral-900">
            {children}
          </h3>
        ),
        p: ({ children }) => (
          <p className={`${compact ? 'mt-1 leading-6' : 'mt-3 leading-7'} first:mt-0 text-[15px] text-neutral-700`}>
            {children}
          </p>
        ),
        strong: ({ children }) => (
          <strong className="font-semibold text-neutral-900">{children}</strong>
        ),
        em: ({ children }) => <em className="italic text-neutral-800">{children}</em>,
        a: ({ href, children }) => (
          <a
            href={href}
            target="_blank"
            rel="noreferrer"
            className="font-medium text-violet-700 underline decoration-violet-300 underline-offset-2 hover:text-violet-900"
          >
            {children}
          </a>
        ),
        ul: ({ children }) => (
          <ul className={`${compact ? 'mt-2' : 'mt-3'} space-y-1.5 pl-5 text-[15px] text-neutral-700 list-disc`}>
            {children}
          </ul>
        ),
        ol: ({ children }) => (
          <ol className={`${compact ? 'mt-2' : 'mt-3'} space-y-1.5 pl-5 text-[15px] text-neutral-700 list-decimal`}>
            {children}
          </ol>
        ),
        li: ({ children }) => (
          <li className="leading-7 marker:text-violet-400">{children}</li>
        ),
        blockquote: ({ children }) => (
          <blockquote className="mt-4 border-l-2 border-violet-300 pl-4 text-neutral-600">
            {children}
          </blockquote>
        ),
        code: ({ className, children, ...props }) => {
          const isBlock = className?.startsWith('language-');

          if (isBlock) {
            return (
              <code className={`block overflow-x-auto whitespace-pre p-4 text-[13px] ${className ?? ''}`} {...props}>
                {children}
              </code>
            );
          }

          return (
            <code
              className="rounded border border-violet-200 bg-violet-50 px-1.5 py-0.5 font-mono text-[13px] font-medium text-violet-700"
              {...props}
            >
              {children}
            </code>
          );
        },
        pre: ({ children }) => (
          <pre className="mt-4 overflow-x-auto rounded-xl border border-neutral-200 bg-neutral-950 text-neutral-50 shadow-sm">
            {children}
          </pre>
        ),
        table: ({ children }) => (
          <div className="mt-4 overflow-x-auto rounded-xl border border-neutral-200">
            <table className="min-w-full border-collapse text-left text-sm text-neutral-700">
              {children}
            </table>
          </div>
        ),
        th: ({ children }) => (
          <th className="border-b border-neutral-200 bg-neutral-50 px-3 py-2 font-semibold text-neutral-900">
            {children}
          </th>
        ),
        td: ({ children }) => (
          <td className="border-t border-neutral-100 px-3 py-2 align-top">{children}</td>
        ),
        hr: () => <hr className="my-5 border-neutral-200" />,
      }}
    >
      {text}
    </ReactMarkdown>
  );
}

export default function MessageBubble({ message }: MessageBubbleProps) {
  const isUser = message.sender === 'user';

  if (isUser) {
    return (
      <motion.div
        initial={{ opacity: 0, y: 8 }}
        animate={{ opacity: 1, y: 0 }}
        transition={{ duration: 0.3, ease: [0.16, 1, 0.3, 1] }}
        className="flex w-full justify-end mb-6"
      >
        <div className="max-w-[80%] rounded-2xl rounded-tr-md bg-neutral-100 border border-neutral-200/80 px-4 py-2.5">
          <MarkdownContent text={message.text} compact />
        </div>
      </motion.div>
    );
  }

  return (
    <motion.div
      initial={{ opacity: 0, y: 8 }}
      animate={{ opacity: 1, y: 0 }}
      transition={{ duration: 0.35, ease: [0.16, 1, 0.3, 1] }}
      className="w-full mb-10"
    >
      <MarkdownContent text={message.text} />

      {message.sources && message.sources.length > 0 && (
        <div className="flex flex-wrap gap-1.5 mt-4">
          {message.sources.map((src, idx) => (
            <SourceCard key={idx} name={src.name} type={src.type} />
          ))}
        </div>
      )}
    </motion.div>
  );
}