exporter.tsx 18 KB

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