Repository: sunner/ChatALL
File: src/components/ChatSetting.vue (Vue)

Code before:

// This function downloads the chat history as a JSON file.
const downloadJson = () => {
  // Get the chat history from localStorage.
  const chatallMessages = localStorage.getItem("chatall-messages");
  if (!chatallMessages) {
    console.error("chatall-messages not found in localStorage");
    return;
  }

  const chats = JSON.parse(chatallMessages)?.chats ?? [];

  // Create an array of messages from the chat history.
  const messages = chats.map((chat) => ({
    // The title of the chat.
    title: chat.title,
    // The names of the bots that the user has selected.
    bots: chat.favBots
      .filter((bot) => bot.selected)
      .map((bot) => bot.classname),
    // The messages in the chat.
    chat: chat.messages.map((message) => ({
      type: message.type,
      content: message.content,
    })),
  }));

  // Create a blob that contains the JSON data.
  // The space parameter specifies the indentation of nested objects in the string representation.
  const blob = new Blob([JSON.stringify(messages, null, 2)], {
    // The type of the blob.
    type: "application/json",
  });

  const url = URL.createObjectURL(blob);

  // Create a file name for the JSON file.
  const formatter = new Intl.DateTimeFormat("en-US", {
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
  });
  const fileName = `chatall-history-${formatter.format(new Date())}`;

  const a = document.createElement("a");
  a.href = url;
  a.download = `${fileName}.json`;
  document.body.appendChild(a);

  // Click the anchor element to download the file.
  a.click();

Review comment (suggestion):
The data structure is not good enough. Perhaps:

1. Filter out all `hide == true` chats and messages
2.  Use the following structure:
```
[
    {
        title: ...,
        messages: 
            [
                   {
                            prompt: xxxxx
                            responses: [
                            {
                                    content:
                                    botName:
                                    botClassname:
                                    botModelName:
                                    highlighted:
                            }
                           ]
                    }
            ]
    }
]
```

Code after:
const store = useStore();

// This function downloads the chat history as a JSON file.
const downloadJson = () => {
  // Get the chat history from localStorage.
  const chatallMessages = localStorage.getItem("chatall-messages");
  if (!chatallMessages) {
    console.error("chatall-messages not found in localStorage");
    return;
  }

  const chats = JSON.parse(chatallMessages)?.chats ?? [];

  // Create an array of messages from the chat history.
  const messages = chats
    .filter((d) => !d.hide)
    .map((chat) => ({
      // The title of the chat.
      title: chat.title,
      // The messages in the chat.
      messages: chat.messages
        .filter((d) => !d.hide)
        .reduce((arr, message) => {
          const t = message.type;
          const content = message.content;
          if (t == "prompt") {
            arr.push({
              prompt: content,
              responses: [],
            });
          } else {
            const botClassname = message.className;
            const bot = bots.getBotByClassName(botClassname);
            const botName = bot.getFullname();
            arr.at(-1).responses.push({
              content,
              botName,
              botClassname,
              botModel: message.model,
              highlight: message.highlight,
            });
          }
          return arr;
        }, []),
    }));

  // Create a blob that contains the JSON data.
  // The space parameter specifies the indentation of nested objects in the string representation.
  const blob = new Blob([JSON.stringify({ chats: messages }, null, 2)], {
    // The type of the blob.
    type: "application/json",