exporter.tsx 19 KB

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