UNPKG

aichat-core

Version:

AI 流式聊天业务核心TS模型封装及业务实现模块

98 lines (97 loc) 5.94 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { memo, useState } from "react"; import Markdown from "react-markdown"; import { Prism } from "react-syntax-highlighter"; import remarkGfm from "remark-gfm"; import rehypeRaw from "rehype-raw"; import rehypeKatex from "rehype-katex"; import remarkMath from "remark-math"; import { oneDark, prism, vs, } from "react-syntax-highlighter/dist/esm/styles/prism"; import "katex/dist/katex.min.css"; import { Flex, message, Modal, Popover, Select, Space } from "antd"; import copy from "copy-to-clipboard"; import CodeEditorPreview from "../code-editor-preview"; import "../../styles/github-markdown.css"; import "../../styles/chat-markdown.scss"; import LLMUtil from "../../core/LLMUtil"; var SyntaxHighlighter = Prism; var ChatMesageMarkDown = memo(function (_a) { var content = _a.content; var _b = useState(oneDark), codeTheme = _b[0], setCodeTheme = _b[1]; var _c = useState("oneDark"), defulatValue = _c[0], setDefaultValue = _c[1]; var _d = useState(false), openHtmlPreview = _d[0], setOpenHtmlPreview = _d[1]; var _e = useState(""), editPreviewCode = _e[0], setEditPreviewCode = _e[1]; var spanCodeCopy = function (codeContent) { return (_jsx("span", { className: "code-copy-span", onClick: function () { copy(codeContent); message.success("复制成功!"); }, children: "\u590D\u5236" })); }; var onChangeCodeTheme = function (value) { if (value === "oneDark") { setCodeTheme(oneDark); setDefaultValue("oneDark"); } else if (value === "prism") { setCodeTheme(prism); setDefaultValue("prism"); } else if (value === "vs") { setCodeTheme(vs); setDefaultValue("vs"); } }; var popOverItem = (_jsx(Select, { defaultValue: defulatValue, style: { width: 160 }, onChange: onChangeCodeTheme, options: [ { key: "1", value: "oneDark", label: "深色版本" }, { key: "2", value: "prism", label: "浅色版本" }, { key: "3", value: "vs", label: "VSCode版本" }, ] })); var onHtmlCodePreview = function (code) { setEditPreviewCode(code); setOpenHtmlPreview(true); }; var LinkRenderer = function (_a) { var href = _a.href, children = _a.children; return (_jsx("a", { href: href, target: "_blank", rel: "nofollow noopener noreferrer ", children: children })); }; return (_jsxs("div", { className: "chat-markdown", children: [_jsx(Markdown, { remarkPlugins: [remarkGfm, remarkMath,], rehypePlugins: [rehypeRaw, rehypeKatex], className: "markdown-body", children: LLMUtil.processContent(content, false), components: { code: function (props) { var children = props.children, className = props.className, node = props.node, rest = __rest(props, ["children", "className", "node"]); var match = /language-(\w+)/.exec(className || ""); var code = match ? String(children).replace(/\n$/, "") : ""; return match ? (_jsxs("div", { className: "coder-container", children: [_jsx("div", { className: "coder-header", children: className && (_jsxs(Flex, { justify: "space-between", children: [_jsx("span", { className: "code-language-span", children: "<".concat(className .replace("language-", "") .toUpperCase(), ">") }), _jsxs(Space, { children: [className .replace("language-", "") .toUpperCase() == "HTML" ? _jsx("div", { style: { cursor: "pointer" }, onClick: function () { return onHtmlCodePreview(code); }, children: "\u6548\u679C\u9884\u89C8 |" }) : null, _jsxs("div", { children: [_jsx(Popover, { placement: "bottom", content: popOverItem, children: _jsx("span", { style: { cursor: "pointer" }, children: " \u4E3B\u9898\u8BBE\u7F6E " }) }), "|"] }), spanCodeCopy(code)] })] })) }), _jsx(SyntaxHighlighter, __assign({}, rest, { PreTag: "div", children: code, language: match[1], style: codeTheme })), className && (_jsx(Flex, { justify: "flex-end", children: spanCodeCopy(code) }))] })) : (_jsx("code", __assign({}, rest, { className: className, children: children }))); }, a: LinkRenderer } }), openHtmlPreview && _jsx(Modal, { title: "\u4EE3\u7801\u6548\u679C\u9884\u89C8", open: openHtmlPreview, width: "100%", centered: true, onCancel: function () { return setOpenHtmlPreview(false); }, footer: null, children: _jsx(CodeEditorPreview, { code: editPreviewCode, onChange: function (value) { value != "" && setEditPreviewCode(value); } }) })] })); }); export default ChatMesageMarkDown;