Chat History — Quick Start

Give signed-in readers a chat page that remembers.

Since 1.14.0

Two panels: past conversations and the open thread. Readers find their threads again on the next visit.

Chat History on the Wikinews sample data. The thread list is on the left. The open thread shows an answer with numbered citations, and the question box is at the bottom.

Takes about fifteen minutes. Try the live demo.

Before you start

Get your publishable API key. Copy it from Overview in the Miso Dashboard. The SDK runs in the browser, so it takes the publishable key, never the secret key. See Authentication.

Put your content in Miso. Everything below works from your catalog. See Integrating Your Data to upload it, and confirm the result in Dashboard ▸ Data Sets ▸ Catalog.

No content of your own yet? Load a real one in a few minutes. Example: Wikinews parses a free public archive of about 20,900 news articles and sends it to Miso, so you have something to ask questions about.

Ask Miso to turn on User History. Miso enables it per app. Until then, the history endpoints return 403. See User History.

Add an endpoint that signs a JWT for the reader. History belongs to one reader, so the browser must prove who the reader is. Your server signs a short-lived JWT with your secret key and returns it to the page. See Create a browser JWT.


1. Install the SDK

As a node module

In your project directory, run:

npm install --save @miso.ai/client-sdk

Then import MisoClient from the SDK:

import MisoClient from "@miso.ai/client-sdk";

Using a script tag

<script async src="https://cdn.jsdelivr.net/npm/@miso.ai/client-sdk@latest/dist/umd/miso.min.js"></script>

The script tag loads the SDK asynchronously, so your code must wait for it. Use the misocmd queue, which runs your function as soon as the SDK is ready:

const misocmd = window.misocmd || (window.misocmd = []);
misocmd.push(() => {
  const MisoClient = window.MisoClient;
  // Your code goes here.
});

Beta. The history panels ship in version 1.14.0, which is in beta now. Until the stable release, write @beta where the snippets above say @latest. For npm, install @miso.ai/client-sdk@beta.

2. Add the panels

<miso-history> is the list of threads. <miso-conversation> is the thread the reader has open. Put the two side by side:

<div id="chat">
  <miso-history class="miso-history">
    <miso-new-thread></miso-new-thread>
    <miso-threads></miso-threads>
  </miso-history>

  <miso-conversation class="miso-conversation">
    <div class="miso-conversation__header" visible-when="nonempty">
      <div class="miso-conversation__header-row">
        <miso-title></miso-title>
        <miso-rename></miso-rename>
        <miso-delete></miso-delete>
        <miso-subscription></miso-subscription>
      </div>
    </div>
    <div class="miso-conversation__intro" visible-when="ready+empty">
      What can I help with?
    </div>
    <miso-messages></miso-messages>
    <miso-query visible-when="ready"></miso-query>
  </miso-conversation>
</div>
ElementWhat the reader sees
<miso-new-thread>The New chat button.
<miso-threads>The list of threads, newest first. Each item has a menu to rename, subscribe, or delete.
<miso-title>, <miso-rename>, <miso-delete>The title of the open thread, and its rename and delete buttons.
<miso-subscription>The toggle for Answer Updates on the open thread.
<miso-messages>The questions and answers of the open thread.
<miso-query>The box where the reader asks the next question.

The class names miso-history and miso-conversation apply Miso's default layout: each panel scrolls inside its own box, and the question box stays below the messages. The items keep their styles without the class names, so omit them when you write a layout of your own. The page decides the size and position of each panel:

html, body {
  height: 100%;
  margin: 0;
}
#chat {
  display: flex;
  gap: 1rem;
  height: 100%;
  padding: 1rem;
  box-sizing: border-box;
}
#chat miso-history {
  flex: 0 0 20rem;
}
#chat miso-conversation {
  flex: 1 1 auto;
}

3. Sign the reader in and start

const misocmd = window.misocmd || (window.misocmd = []);
misocmd.push(async () => {
  const client = new MisoClient("YOUR_PUBLISHABLE_API_KEY");
  const workflow = client.ui.history;

  // The same id that your server signs into the JWT.
  client.context.user_id = "u-10293";

  // Your endpoint. It returns a JWT for the signed-in reader.
  async function fetchToken() {
    const response = await fetch("/api/miso-token");
    return (await response.json()).token;
  }

  // The styles load while your server signs the token.
  const [token] = await Promise.all([fetchToken(), client.ui.ready]);
  client.context.auth = `Bearer ${token}`;

  workflow.start();                         // load the thread list
});

client.ui.ready resolves when the styles are loaded. The token request runs at the same time, so neither waits for the other.

Set client.context.user_id too, although the JWT already names the reader. The JWT signs the reader in for the history requests. The SDK records interactions, such as a click on a citation, with the id in the context, and those do not read the JWT. Use the same id as the user_id claim of the JWT. If the two are different, Miso returns 401.

The SDK sends client.context.auth as the Authorization header on every request. start() loads the reader's threads. The conversation panel opens on a new, empty thread and needs no call of its own.

4. Renew the token when it expires

A short-lived JWT expires while the page is open, and Miso then answers 401. Set client.api.onError to get a new token and send the request again:

let renewing;

async function renewToken(hold) {
  // New requests wait here until the new token is set.
  const unhold = hold();
  try {
    client.context.auth = `Bearer ${await fetchToken()}`;
  } finally {
    unhold();
    renewing = undefined;
  }
}

client.api.onError = async (error, { hold, resend }) => {
  // Any other error, or a second 401 for the same request: stop here.
  if (error.status !== 401 || error.resent) {
    throw error;
  }
  // Requests that fail together share one renewal.
  await (renewing || (renewing = renewToken(hold)));
  return resend();
};

hold() makes the SDK keep each new request until you call the function it returns. Without it, a request that starts during the renewal goes out with the expired token and fails too. Release the hold in finally, or the requests wait forever when the renewal fails.

error.resent counts how many times the SDK sent this request again. The check stops a loop when the new token is rejected too.


The complete page

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Miso Chat History</title>
  <script async src="https://cdn.jsdelivr.net/npm/@miso.ai/client-sdk@beta/dist/umd/miso.min.js"></script>
  <style>
    html, body {
      height: 100%;
      margin: 0;
    }
    #chat {
      display: flex;
      gap: 1rem;
      height: 100%;
      padding: 1rem;
      box-sizing: border-box;
    }
    #chat miso-history {
      flex: 0 0 20rem;
    }
    #chat miso-conversation {
      flex: 1 1 auto;
    }
  </style>
</head>
<body>
  <div id="chat">
    <miso-history class="miso-history">
      <miso-new-thread></miso-new-thread>
      <miso-threads></miso-threads>
    </miso-history>

    <miso-conversation class="miso-conversation">
      <div class="miso-conversation__header" visible-when="nonempty">
        <div class="miso-conversation__header-row">
          <miso-title></miso-title>
          <miso-rename></miso-rename>
          <miso-delete></miso-delete>
          <miso-subscription></miso-subscription>
        </div>
      </div>
      <div class="miso-conversation__intro" visible-when="ready+empty">
        What can I help with?
      </div>
      <miso-messages></miso-messages>
      <miso-query visible-when="ready"></miso-query>
    </miso-conversation>
  </div>

  <script>
    const misocmd = window.misocmd || (window.misocmd = []);
    misocmd.push(async () => {
      const client = new MisoClient("YOUR_PUBLISHABLE_API_KEY");
      const workflow = client.ui.history;

      client.context.user_id = "u-10293";

      async function fetchToken() {
        const response = await fetch("/api/miso-token");
        return (await response.json()).token;
      }

      const [token] = await Promise.all([fetchToken(), client.ui.ready]);
      client.context.auth = `Bearer ${token}`;

      let renewing;
      async function renewToken(hold) {
        const unhold = hold();
        try {
          client.context.auth = `Bearer ${await fetchToken()}`;
        } finally {
          unhold();
          renewing = undefined;
        }
      }
      client.api.onError = async (error, { hold, resend }) => {
        if (error.status !== 401 || error.resent) {
          throw error;
        }
        await (renewing || (renewing = renewToken(hold)));
        return resend();
      };

      workflow.start();
    });
  </script>
</body>
</html>

Check it works

Open the page as a signed-in reader and ask a question. The answer streams into the conversation panel, and a new thread appears at the top of the list. Reload the page: the thread is still in the list, and a click opens it again.

To match the image at the top, add the Inter font to the <head>:

<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" />

Then add these rules after the layout styles. They hide the first question of each thread, because the title shows it. A renamed thread hides it too. To keep the question, remove that rule.

body {
  background: radial-gradient(circle at 50% 110%, #4a4d55, #111216 65%);
  color: #1b1c1f;
  font-family: Inter, system-ui, sans-serif;
}
#chat {
  max-width: 76rem;
  height: calc(100% - 4rem);
  margin: 2rem auto;
  padding: 0.375rem;
  gap: 0;
  background: #e8e8ea;
  border-radius: 14px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.55);
}
#chat miso-history {
  flex-basis: 16rem;
  padding: 0.75rem 0.5rem;
  font-size: 0.8125rem;
}
#chat miso-conversation {
  padding: 1.5rem 1rem 1rem;
  background: #fff;
  border-radius: 10px;
}
#chat .miso-conversation__header,
#chat miso-query,
#chat .miso-messages__list {
  box-sizing: border-box;
  width: 100%;
  max-width: 42rem;
  margin-inline: auto;
}
#chat .miso-conversation__header [data-role="title"] {
  overflow: visible;
  white-space: normal;
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}
#chat .miso-conversation__header,
#chat .miso-conversation__header-row {
  border-color: #ececee;
}
/* The title already shows the first question. */
#chat .miso-messages__item:first-child .miso-messages__question {
  display: none;
}
#chat .miso-question {
  background: #f0f0f1;
  font-size: 0.875rem;
}
#chat .miso-markdown,
#chat .miso-markdown * {
  color: #4a4b50;
  font-size: 0.875rem;
  line-height: 1.65;
}
#chat .miso-markdown h3,
#chat .miso-markdown strong {
  color: #1b1c1f;
}
#chat .miso-markdown h3 {
  font-size: 1rem;
}
#chat .miso-markdown a,
#chat .miso-markdown a * {
  color: #1b1c1f;
  font-style: normal;
}
#chat miso-new-thread button {
  background: none;
  border: 0;
}
#chat .miso-search-box__input-group {
  padding: 0.375rem;
  background: #f6f6f7;
  border: 1px solid #e6e6e8;
  border-radius: 12px;
}
#chat .miso-search-box__input {
  height: 2rem;
  padding-block: 0;
  background: none;
  font-size: 0.875rem;
}
#chat .miso-search-box__button {
  display: grid;
  flex: none;
  place-items: center;
  align-self: center;
  width: 2rem;
  height: 2rem;
  padding: 0;
  background: #1b1c1f;
  border: 0;
  border-radius: 8px;
}
#chat .miso-search-box__button svg {
  display: none;
}
#chat .miso-search-box__button::before {
  content: "→";
  color: #fff;
  font-size: 1rem;
  line-height: 1;
}
SymptomCause
The list shows an error, and the requests return 403User History is off for this app. Ask Miso to enable it.
The list shows "Your session has expired"The JWT is missing or expired, or it was signed with a different key.
The panels never renderThe page loads a release older than 1.14.0, or your code ran before the SDK loaded.

Next

  • UI Customization — change the parts of the two panels, and when they show.
  • API Configuration — how many threads load at a time, and which articles the answers can cite.
  • Citation Links — the class, the attributes and the tooltip of each citation.
  • User History — the API behind the panels, for your own front end.
  • Answer Updates — what the subscription toggle does, and how the unread indicators clear.
  • JWT (user authentication) — the claims Miso reads, and what each 401 means.