exporter.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /* eslint-disable @next/next/no-img-element */
  2. import { ChatMessage, ModelType, useAppConfig, useChatStore } from "../store";
  3. import Locale from "../locales";
  4. import styles from "./exporter.module.scss";
  5. import {
  6. List,
  7. ListItem,
  8. Modal,
  9. Select,
  10. showImageModal,
  11. showModal,
  12. showToast,
  13. } from "./ui-lib";
  14. import { IconButton } from "./button";
  15. import {
  16. copyToClipboard,
  17. downloadAs,
  18. getMessageImages,
  19. useMobileScreen,
  20. } from "../utils";
  21. import CopyIcon from "../icons/copy.svg";
  22. import LoadingIcon from "../icons/three-dots.svg";
  23. import ChatGptIcon from "../icons/chatgpt.png";
  24. import ShareIcon from "../icons/share.svg";
  25. import DownloadIcon from "../icons/download.svg";
  26. import { useEffect, useMemo, useRef, useState } from "react";
  27. import { MessageSelector, useMessageSelector } from "./message-selector";
  28. // Avatar组件替代实现
  29. import BotIcon from "../icons/bot.svg";
  30. import BlackBotIcon from "../icons/black-bot.svg";
  31. function Avatar(props: { model?: string; avatar?: string }) {
  32. if (props.model) {
  33. return (
  34. <div className="no-dark">
  35. {props.model?.startsWith("gpt-4") ? (
  36. <BlackBotIcon className="user-avatar" />
  37. ) : (
  38. <BotIcon className="user-avatar" />
  39. )}
  40. </div>
  41. );
  42. }
  43. return (
  44. <div className="user-avatar">
  45. {/* 移除emoji头像,使用默认bot图标 */}
  46. <BotIcon className="user-avatar" />
  47. </div>
  48. );
  49. }
  50. import dynamic from "next/dynamic";
  51. import NextImage from "next/image";
  52. import { toBlob, toPng } from "html-to-image";
  53. import { DEFAULT_MASK_AVATAR } from "../store/mask";
  54. import { prettyObject } from "../utils/format";
  55. import { EXPORT_MESSAGE_CLASS_NAME } from "../constant";
  56. import { getClientConfig } from "../config/client";
  57. import { type ClientApi, getClientApi } from "../client/api";
  58. import { getMessageTextContent } from "../utils";
  59. const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
  60. loading: () => <LoadingIcon />,
  61. });
  62. export function ExportMessageModal(props: { onClose: () => void }) {
  63. return (
  64. <div className="modal-mask">
  65. <Modal
  66. title={Locale.Export.Title}
  67. onClose={props.onClose}
  68. footer={
  69. <div
  70. style={{
  71. width: "100%",
  72. textAlign: "center",
  73. fontSize: 14,
  74. opacity: 0.5,
  75. }}
  76. >
  77. {Locale.Exporter.Description.Title}
  78. </div>
  79. }
  80. >
  81. <div style={{ minHeight: "40vh" }}>
  82. <MessageExporter />
  83. </div>
  84. </Modal>
  85. </div>
  86. );
  87. }
  88. function useSteps(
  89. steps: Array<{
  90. name: string;
  91. value: string;
  92. }>,
  93. ) {
  94. const stepCount = steps.length;
  95. const [currentStepIndex, setCurrentStepIndex] = useState(0);
  96. const nextStep = () =>
  97. setCurrentStepIndex((currentStepIndex + 1) % stepCount);
  98. const prevStep = () =>
  99. setCurrentStepIndex((currentStepIndex - 1 + stepCount) % stepCount);
  100. return {
  101. currentStepIndex,
  102. setCurrentStepIndex,
  103. nextStep,
  104. prevStep,
  105. currentStep: steps[currentStepIndex],
  106. };
  107. }
  108. function Steps<
  109. T extends {
  110. name: string;
  111. value: string;
  112. }[],
  113. >(props: { steps: T; onStepChange?: (index: number) => void; index: number }) {
  114. const steps = props.steps;
  115. const stepCount = steps.length;
  116. return (
  117. <div className={styles["steps"]}>
  118. <div className={styles["steps-progress"]}>
  119. <div
  120. className={styles["steps-progress-inner"]}
  121. style={{
  122. width: `${((props.index + 1) / stepCount) * 100}%`,
  123. }}
  124. ></div>
  125. </div>
  126. <div className={styles["steps-inner"]}>
  127. {steps.map((step, i) => {
  128. return (
  129. <div
  130. key={i}
  131. className={`${styles["step"]} ${styles[i <= props.index ? "step-finished" : ""]
  132. } ${i === props.index && styles["step-current"]} clickable`}
  133. onClick={() => {
  134. props.onStepChange?.(i);
  135. }}
  136. role="button"
  137. >
  138. <span className={styles["step-index"]}>{i + 1}</span>
  139. <span className={styles["step-name"]}>{step.name}</span>
  140. </div>
  141. );
  142. })}
  143. </div>
  144. </div>
  145. );
  146. }
  147. export function MessageExporter() {
  148. const steps = [
  149. {
  150. name: Locale.Export.Steps.Select,
  151. value: "select",
  152. },
  153. {
  154. name: Locale.Export.Steps.Preview,
  155. value: "preview",
  156. },
  157. ];
  158. const { currentStep, setCurrentStepIndex, currentStepIndex } =
  159. useSteps(steps);
  160. const formats = ["text", "image", "json"] as const;
  161. type ExportFormat = (typeof formats)[number];
  162. const [exportConfig, setExportConfig] = useState({
  163. format: "image" as ExportFormat,
  164. includeContext: true,
  165. });
  166. function updateExportConfig(updater: (config: typeof exportConfig) => void) {
  167. const config = { ...exportConfig };
  168. updater(config);
  169. setExportConfig(config);
  170. }
  171. const chatStore = useChatStore();
  172. const session = chatStore.currentSession();
  173. const { selection, updateSelection } = useMessageSelector();
  174. const selectedMessages = useMemo(() => {
  175. const ret: ChatMessage[] = [];
  176. if (exportConfig.includeContext) {
  177. ret.push(...session.mask.context);
  178. }
  179. ret.push(...session.messages.filter((m) => selection.has(m.id)));
  180. return ret;
  181. }, [
  182. exportConfig.includeContext,
  183. session.messages,
  184. session.mask.context,
  185. selection,
  186. ]);
  187. function preview() {
  188. if (exportConfig.format === "text") {
  189. return (
  190. <MarkdownPreviewer messages={selectedMessages} topic={session.topic} />
  191. );
  192. } else if (exportConfig.format === "json") {
  193. return (
  194. <JsonPreviewer messages={selectedMessages} topic={session.topic} />
  195. );
  196. } else {
  197. return (
  198. <ImagePreviewer messages={selectedMessages} topic={session.topic} />
  199. );
  200. }
  201. }
  202. return (
  203. <>
  204. <Steps
  205. steps={steps}
  206. index={currentStepIndex}
  207. onStepChange={setCurrentStepIndex}
  208. />
  209. <div
  210. className={styles["message-exporter-body"]}
  211. style={currentStep.value !== "select" ? { display: "none" } : {}}
  212. >
  213. <List>
  214. <ListItem
  215. title={Locale.Export.Format.Title}
  216. subTitle={Locale.Export.Format.SubTitle}
  217. >
  218. <Select
  219. value={exportConfig.format}
  220. onChange={(e) =>
  221. updateExportConfig(
  222. (config) =>
  223. (config.format = e.currentTarget.value as ExportFormat),
  224. )
  225. }
  226. >
  227. {formats.map((f) => (
  228. <option key={f} value={f}>
  229. {f}
  230. </option>
  231. ))}
  232. </Select>
  233. </ListItem>
  234. <ListItem
  235. title={Locale.Export.IncludeContext.Title}
  236. subTitle={Locale.Export.IncludeContext.SubTitle}
  237. >
  238. <input
  239. type="checkbox"
  240. checked={exportConfig.includeContext}
  241. onChange={(e) => {
  242. updateExportConfig(
  243. (config) => (config.includeContext = e.currentTarget.checked),
  244. );
  245. }}
  246. ></input>
  247. </ListItem>
  248. </List>
  249. <MessageSelector
  250. selection={selection}
  251. updateSelection={updateSelection}
  252. defaultSelectAll
  253. />
  254. </div>
  255. {currentStep.value === "preview" && (
  256. <div className={styles["message-exporter-body"]}>{preview()}</div>
  257. )}
  258. </>
  259. );
  260. }
  261. export function RenderExport(props: {
  262. messages: ChatMessage[];
  263. onRender: (messages: ChatMessage[]) => void;
  264. }) {
  265. const domRef = useRef<HTMLDivElement>(null);
  266. useEffect(() => {
  267. if (!domRef.current) return;
  268. const dom = domRef.current;
  269. const messages = Array.from(
  270. dom.getElementsByClassName(EXPORT_MESSAGE_CLASS_NAME),
  271. );
  272. if (messages.length !== props.messages.length) {
  273. return;
  274. }
  275. const renderMsgs = messages.map((v, i) => {
  276. const [role, _] = v.id.split(":");
  277. return {
  278. id: i.toString(),
  279. role: role as any,
  280. content: role === "user" ? v.textContent ?? "" : v.innerHTML,
  281. date: "",
  282. };
  283. });
  284. props.onRender(renderMsgs);
  285. // eslint-disable-next-line react-hooks/exhaustive-deps
  286. }, []);
  287. return (
  288. <div ref={domRef}>
  289. {props.messages.map((m, i) => (
  290. <div
  291. key={i}
  292. id={`${m.role}:${i}`}
  293. className={EXPORT_MESSAGE_CLASS_NAME}
  294. >
  295. <Markdown content={getMessageTextContent(m)} defaultShow />
  296. </div>
  297. ))}
  298. </div>
  299. );
  300. }
  301. export function PreviewActions(props: {
  302. download: () => void;
  303. copy: () => void;
  304. showCopy?: boolean;
  305. messages?: ChatMessage[];
  306. }) {
  307. const [loading, setLoading] = useState(false);
  308. const [shouldExport, setShouldExport] = useState(false);
  309. const config = useAppConfig();
  310. const onRenderMsgs = (msgs: ChatMessage[]) => {
  311. setShouldExport(false);
  312. const api: ClientApi = getClientApi(config.modelConfig.providerName);
  313. api
  314. .share(msgs)
  315. .then((res) => {
  316. if (!res) return;
  317. showModal({
  318. title: Locale.Export.Share,
  319. children: [
  320. <input
  321. type="text"
  322. value={res}
  323. key="input"
  324. style={{
  325. width: "100%",
  326. maxWidth: "unset",
  327. }}
  328. readOnly
  329. onClick={(e) => e.currentTarget.select()}
  330. ></input>,
  331. ],
  332. actions: [
  333. <IconButton
  334. icon={<CopyIcon />}
  335. text={Locale.Chat.Actions.Copy}
  336. key="copy"
  337. onClick={() => copyToClipboard(res)}
  338. />,
  339. ],
  340. });
  341. setTimeout(() => {
  342. window.open(res, "_blank");
  343. }, 800);
  344. })
  345. .catch((e) => {
  346. console.error("[Share]", e);
  347. showToast(prettyObject(e));
  348. })
  349. .finally(() => setLoading(false));
  350. };
  351. const share = async () => {
  352. if (props.messages?.length) {
  353. setLoading(true);
  354. setShouldExport(true);
  355. }
  356. };
  357. return (
  358. <>
  359. <div className={styles["preview-actions"]}>
  360. {props.showCopy && (
  361. <IconButton
  362. text={Locale.Export.Copy}
  363. bordered
  364. shadow
  365. icon={<CopyIcon />}
  366. onClick={props.copy}
  367. ></IconButton>
  368. )}
  369. <IconButton
  370. text={Locale.Export.Download}
  371. bordered
  372. shadow
  373. icon={<DownloadIcon />}
  374. onClick={props.download}
  375. ></IconButton>
  376. <IconButton
  377. text={Locale.Export.Share}
  378. bordered
  379. shadow
  380. icon={loading ? <LoadingIcon /> : <ShareIcon />}
  381. onClick={share}
  382. ></IconButton>
  383. </div>
  384. <div
  385. style={{
  386. position: "fixed",
  387. right: "200vw",
  388. pointerEvents: "none",
  389. }}
  390. >
  391. {shouldExport && (
  392. <RenderExport
  393. messages={props.messages ?? []}
  394. onRender={onRenderMsgs}
  395. />
  396. )}
  397. </div>
  398. </>
  399. );
  400. }
  401. function ExportAvatar(props: { avatar: string }) {
  402. if (props.avatar === DEFAULT_MASK_AVATAR) {
  403. return (
  404. <img
  405. src={BotIcon.src}
  406. width={30}
  407. height={30}
  408. alt="bot"
  409. className="user-avatar"
  410. />
  411. );
  412. }
  413. return <Avatar avatar={props.avatar} />;
  414. }
  415. export function ImagePreviewer(props: {
  416. messages: ChatMessage[];
  417. topic: string;
  418. }) {
  419. const chatStore = useChatStore();
  420. const session = chatStore.currentSession();
  421. const mask = session.mask;
  422. const config = useAppConfig();
  423. const previewRef = useRef<HTMLDivElement>(null);
  424. const copy = () => {
  425. showToast(Locale.Export.Image.Toast);
  426. const dom = previewRef.current;
  427. if (!dom) return;
  428. toBlob(dom).then((blob) => {
  429. if (!blob) return;
  430. try {
  431. navigator.clipboard
  432. .write([
  433. new ClipboardItem({
  434. "image/png": blob,
  435. }),
  436. ])
  437. .then(() => {
  438. showToast(Locale.Copy.Success);
  439. refreshPreview();
  440. });
  441. } catch (e) {
  442. console.error("[Copy Image] ", e);
  443. showToast(Locale.Copy.Failed);
  444. }
  445. });
  446. };
  447. const isMobile = useMobileScreen();
  448. const download = async () => {
  449. showToast(Locale.Export.Image.Toast);
  450. const dom = previewRef.current;
  451. if (!dom) return;
  452. const isApp = getClientConfig()?.isApp;
  453. try {
  454. const blob = await toPng(dom);
  455. if (!blob) return;
  456. if (isMobile || (isApp && window.__TAURI__)) {
  457. if (isApp && window.__TAURI__) {
  458. const result = await window.__TAURI__.dialog.save({
  459. defaultPath: `${props.topic}.png`,
  460. filters: [
  461. {
  462. name: "PNG Files",
  463. extensions: ["png"],
  464. },
  465. {
  466. name: "All Files",
  467. extensions: ["*"],
  468. },
  469. ],
  470. });
  471. if (result !== null) {
  472. const response = await fetch(blob);
  473. const buffer = await response.arrayBuffer();
  474. const uint8Array = new Uint8Array(buffer);
  475. await window.__TAURI__.fs.writeBinaryFile(result, uint8Array);
  476. showToast(Locale.Download.Success);
  477. } else {
  478. showToast(Locale.Download.Failed);
  479. }
  480. } else {
  481. showImageModal(blob);
  482. }
  483. } else {
  484. const link = document.createElement("a");
  485. link.download = `${props.topic}.png`;
  486. link.href = blob;
  487. link.click();
  488. refreshPreview();
  489. }
  490. } catch (error) {
  491. showToast(Locale.Download.Failed);
  492. }
  493. };
  494. const refreshPreview = () => {
  495. const dom = previewRef.current;
  496. if (dom) {
  497. dom.innerHTML = dom.innerHTML; // Refresh the content of the preview by resetting its HTML for fix a bug glitching
  498. }
  499. };
  500. return (
  501. <div className={styles["image-previewer"]}>
  502. <PreviewActions
  503. copy={copy}
  504. download={download}
  505. showCopy={!isMobile}
  506. messages={props.messages}
  507. />
  508. <div
  509. className={`${styles["preview-body"]} ${styles["default-theme"]}`}
  510. ref={previewRef}
  511. >
  512. <div className={styles["chat-info"]}>
  513. <div className={styles["logo"] + " no-dark"}>
  514. <NextImage
  515. src={ChatGptIcon.src}
  516. alt="logo"
  517. width={50}
  518. height={50}
  519. />
  520. </div>
  521. <div>
  522. <div className={styles["main-title"]}>NextChat</div>
  523. <div className={styles["sub-title"]}>
  524. github.com/ChatGPTNextWeb/ChatGPT-Next-Web
  525. </div>
  526. <div className={styles["icons"]}>
  527. <ExportAvatar avatar={config.avatar} />
  528. <span className={styles["icon-space"]}>&</span>
  529. <ExportAvatar avatar={mask.avatar} />
  530. </div>
  531. </div>
  532. <div>
  533. <div className={styles["chat-info-item"]}>
  534. {Locale.Exporter.Model}: {mask.modelConfig.model}
  535. </div>
  536. <div className={styles["chat-info-item"]}>
  537. {Locale.Exporter.Messages}: {props.messages.length}
  538. </div>
  539. <div className={styles["chat-info-item"]}>
  540. {Locale.Exporter.Topic}: {session.topic}
  541. </div>
  542. <div className={styles["chat-info-item"]}>
  543. {Locale.Exporter.Time}:{" "}
  544. {new Date(
  545. props.messages.at(-1)?.date ?? Date.now(),
  546. ).toLocaleString()}
  547. </div>
  548. </div>
  549. </div>
  550. {props.messages.map((m, i) => {
  551. return (
  552. <div
  553. className={styles["message"] + " " + styles["message-" + m.role]}
  554. key={i}
  555. >
  556. <div className={styles["avatar"]}>
  557. <ExportAvatar
  558. avatar={m.role === "user" ? config.avatar : mask.avatar}
  559. />
  560. </div>
  561. <div className={styles["body"]}>
  562. <Markdown
  563. content={getMessageTextContent(m)}
  564. fontSize={config.fontSize}
  565. fontFamily={config.fontFamily}
  566. defaultShow
  567. />
  568. {getMessageImages(m).length == 1 && (
  569. <img
  570. key={i}
  571. src={getMessageImages(m)[0]}
  572. alt="message"
  573. className={styles["message-image"]}
  574. />
  575. )}
  576. {getMessageImages(m).length > 1 && (
  577. <div
  578. className={styles["message-images"]}
  579. style={
  580. {
  581. "--image-count": getMessageImages(m).length,
  582. } as React.CSSProperties
  583. }
  584. >
  585. {getMessageImages(m).map((src, i) => (
  586. <img
  587. key={i}
  588. src={src}
  589. alt="message"
  590. className={styles["message-image-multi"]}
  591. />
  592. ))}
  593. </div>
  594. )}
  595. </div>
  596. </div>
  597. );
  598. })}
  599. </div>
  600. </div>
  601. );
  602. }
  603. export function MarkdownPreviewer(props: {
  604. messages: ChatMessage[];
  605. topic: string;
  606. }) {
  607. const mdText =
  608. `# ${props.topic}\n\n` +
  609. props.messages
  610. .map((m) => {
  611. return m.role === "user"
  612. ? `## ${Locale.Export.MessageFromYou}:\n${getMessageTextContent(m)}`
  613. : `## ${Locale.Export.MessageFromChatGPT}:\n${getMessageTextContent(
  614. m,
  615. ).trim()}`;
  616. })
  617. .join("\n\n");
  618. const copy = () => {
  619. copyToClipboard(mdText);
  620. };
  621. const download = () => {
  622. downloadAs(mdText, `${props.topic}.md`);
  623. };
  624. return (
  625. <>
  626. <PreviewActions
  627. copy={copy}
  628. download={download}
  629. showCopy={true}
  630. messages={props.messages}
  631. />
  632. <div className="markdown-body">
  633. <pre className={styles["export-content"]}>{mdText}</pre>
  634. </div>
  635. </>
  636. );
  637. }
  638. export function JsonPreviewer(props: {
  639. messages: ChatMessage[];
  640. topic: string;
  641. }) {
  642. const msgs = {
  643. messages: [
  644. {
  645. role: "system",
  646. content: `${Locale.FineTuned.Sysmessage} ${props.topic}`,
  647. },
  648. ...props.messages.map((m) => ({
  649. role: m.role,
  650. content: m.content,
  651. })),
  652. ],
  653. };
  654. const mdText = "```json\n" + JSON.stringify(msgs, null, 2) + "\n```";
  655. const minifiedJson = JSON.stringify(msgs);
  656. const copy = () => {
  657. copyToClipboard(minifiedJson);
  658. };
  659. const download = () => {
  660. downloadAs(JSON.stringify(msgs), `${props.topic}.json`);
  661. };
  662. return (
  663. <>
  664. <PreviewActions
  665. copy={copy}
  666. download={download}
  667. showCopy={false}
  668. messages={props.messages}
  669. />
  670. <div className="markdown-body" onClick={copy}>
  671. <Markdown content={mdText} />
  672. </div>
  673. </>
  674. );
  675. }