From b380421fd56da4d9faa5646afa4468c2a954ddc7 Mon Sep 17 00:00:00 2001 From: wangwentong Date: Tue, 15 Aug 2023 13:32:34 +0800 Subject: [PATCH] support json export --- app/components/exporter.tsx | 63 +++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/app/components/exporter.tsx b/app/components/exporter.tsx index ab6fad29..604b8823 100644 --- a/app/components/exporter.tsx +++ b/app/components/exporter.tsx @@ -127,7 +127,7 @@ export function MessageExporter() { ]; const { currentStep, setCurrentStepIndex, currentStepIndex } = useSteps(steps); - const formats = ["text", "image"] as const; + const formats = ["text", "image", "json"] as const; type ExportFormat = (typeof formats)[number]; const [exportConfig, setExportConfig] = useState({ @@ -157,7 +157,21 @@ export function MessageExporter() { session.mask.context, selection, ]); - + function preview() { + if (exportConfig.format === "text") { + return ( + + ); + } else if (exportConfig.format === "json") { + return ( + + ); + } else { + return ( + + ); + } + } return ( <> {currentStep.value === "preview" && ( -
- {exportConfig.format === "text" ? ( - - ) : ( - - )} -
+
{preview()}
)} ); @@ -545,12 +550,44 @@ export function MarkdownPreviewer(props: { const download = () => { downloadAs(mdText, `${props.topic}.md`); }; - return ( <> +
+
{mdText}
+
+ + ); +} + +export function JsonPreviewer(props: { + messages: ChatMessage[]; + topic: string; +}) { + const msgs = props.messages.map((m) => ({ + role: m.role, + content: m.content, + })); + const mdText = "\n" + JSON.stringify(msgs, null, 2) + "\n"; + + const copy = () => { + copyToClipboard(JSON.stringify(msgs, null, 2)); + }; + const download = () => { + downloadAs(JSON.stringify(msgs, null, 2), `${props.topic}.json`); + }; + + return ( + <> +