exporter.tsx 18 KB

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