exporter.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 {
  303. api = new ClientApi(ModelProvider.GPT);
  304. }
  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. }