exporter.tsx 17 KB

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