Spaces:
Runtime error
Runtime error
File size: 4,300 Bytes
13095e0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
import { Conversation } from '@/types/chat';
import {
ExportFormatV1,
ExportFormatV2,
ExportFormatV3,
ExportFormatV4,
LatestExportFormat,
SupportedExportFormats,
} from '@/types/export';
import { FolderInterface } from '@/types/folder';
import { Prompt } from '@/types/prompt';
import { cleanConversationHistory } from './clean';
export function isExportFormatV1(obj: any): obj is ExportFormatV1 {
return Array.isArray(obj);
}
export function isExportFormatV2(obj: any): obj is ExportFormatV2 {
return !('version' in obj) && 'folders' in obj && 'history' in obj;
}
export function isExportFormatV3(obj: any): obj is ExportFormatV3 {
return obj.version === 3;
}
export function isExportFormatV4(obj: any): obj is ExportFormatV4 {
return obj.version === 4;
}
export const isLatestExportFormat = isExportFormatV4;
export function cleanData(data: SupportedExportFormats): LatestExportFormat {
if (isExportFormatV1(data)) {
return {
version: 4,
history: cleanConversationHistory(data),
folders: [],
prompts: [],
};
}
if (isExportFormatV2(data)) {
return {
version: 4,
history: cleanConversationHistory(data.history || []),
folders: (data.folders || []).map((chatFolder) => ({
id: chatFolder.id.toString(),
name: chatFolder.name,
type: 'chat',
})),
prompts: [],
};
}
if (isExportFormatV3(data)) {
return { ...data, version: 4, prompts: [] };
}
if (isExportFormatV4(data)) {
return data;
}
throw new Error('Unsupported data format');
}
function currentDate() {
const date = new Date();
const month = date.getMonth() + 1;
const day = date.getDate();
return `${month}-${day}`;
}
export const exportData = () => {
let history = localStorage.getItem('conversationHistory');
let folders = localStorage.getItem('folders');
let prompts = localStorage.getItem('prompts');
if (history) {
history = JSON.parse(history);
}
if (folders) {
folders = JSON.parse(folders);
}
if (prompts) {
prompts = JSON.parse(prompts);
}
const data = {
version: 4,
history: history || [],
folders: folders || [],
prompts: prompts || [],
} as LatestExportFormat;
const blob = new Blob([JSON.stringify(data, null, 2)], {
type: 'application/json',
});
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.download = `chatbot_ui_history_${currentDate()}.json`;
link.href = url;
link.style.display = 'none';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
};
export const importData = (
data: SupportedExportFormats,
): LatestExportFormat => {
const { history, folders, prompts } = cleanData(data);
const oldConversations = localStorage.getItem('conversationHistory');
const oldConversationsParsed = oldConversations
? JSON.parse(oldConversations)
: [];
const newHistory: Conversation[] = [
...oldConversationsParsed,
...history,
].filter(
(conversation, index, self) =>
index === self.findIndex((c) => c.id === conversation.id),
);
localStorage.setItem('conversationHistory', JSON.stringify(newHistory));
if (newHistory.length > 0) {
localStorage.setItem(
'selectedConversation',
JSON.stringify(newHistory[newHistory.length - 1]),
);
} else {
localStorage.removeItem('selectedConversation');
}
const oldFolders = localStorage.getItem('folders');
const oldFoldersParsed = oldFolders ? JSON.parse(oldFolders) : [];
const newFolders: FolderInterface[] = [
...oldFoldersParsed,
...folders,
].filter(
(folder, index, self) =>
index === self.findIndex((f) => f.id === folder.id),
);
localStorage.setItem('folders', JSON.stringify(newFolders));
const oldPrompts = localStorage.getItem('prompts');
const oldPromptsParsed = oldPrompts ? JSON.parse(oldPrompts) : [];
const newPrompts: Prompt[] = [...oldPromptsParsed, ...prompts].filter(
(prompt, index, self) =>
index === self.findIndex((p) => p.id === prompt.id),
);
localStorage.setItem('prompts', JSON.stringify(newPrompts));
return {
version: 4,
history: newHistory,
folders: newFolders,
prompts: newPrompts,
};
};
|