agent-content.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { ChatContext } from '@/app/chat-context';
  2. import { IChatDialogueMessageSchema } from '@/types/chat';
  3. import { GPTVis } from '@antv/gpt-vis';
  4. import classNames from 'classnames';
  5. import { memo, useContext } from 'react';
  6. import rehypeRaw from 'rehype-raw';
  7. import markdownComponents from './chat-content/config';
  8. interface Props {
  9. content: IChatDialogueMessageSchema;
  10. }
  11. function formatMarkdownVal(val: string) {
  12. return val?.replace(/<table(\w*=[^>]+)>/gi, '<table $1>').replace(/<tr(\w*=[^>]+)>/gi, '<tr $1>');
  13. }
  14. function AgentContent({ content }: Props) {
  15. const { scene } = useContext(ChatContext);
  16. const isView = content.role === 'view';
  17. return (
  18. <div
  19. className={classNames('relative w-full p-2 md:p-4 rounded-xl break-words', {
  20. 'bg-white dark:bg-[#232734]': isView,
  21. 'lg:w-full xl:w-full pl-0': ['chat_with_db_execute', 'chat_dashboard'].includes(scene),
  22. })}
  23. >
  24. {isView ? (
  25. <GPTVis components={markdownComponents} rehypePlugins={[rehypeRaw]}>
  26. {formatMarkdownVal(content.context)}
  27. </GPTVis>
  28. ) : (
  29. <div className=''>{content.context}</div>
  30. )}
  31. </div>
  32. );
  33. }
  34. export default memo(AgentContent);