exporter.tsx 19 KB

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