exporter.tsx 18 KB

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