Skip to main content

useConversationHistory Hook

The useConversationHistory hook gives access to conversations stored in the user's browser, so you can build a history UI where users reopen a past conversation and continue it.

Persistence​

Conversation history is on by default: completed conversations are saved to the browser's localStorage as the user chats. To turn it off, pass conversationHistory={{ enabled: false }} to KapaProvider:

<KapaProvider
integrationId="your-integration-id"
conversationHistory={{ enabled: false }}
>
<App />
</KapaProvider>

See KapaProvider for the available options.

Usage​

import { useConversationHistory } from "@kapaai/react-sdk";

function HistoryList() {
const { enabled, threads, restoreThread } = useConversationHistory();

// Use the hook values and functions to build your UI
// ...
}

Return values​

The useConversationHistory hook returns an object with the following properties:

PropertyTypeDescription
enabledbooleanWhether persistence is enabled on the provider.
threadsStoredConversationThread[]Stored conversations, most recent first. Updates as the user chats.
restoreThread(threadId: string) => voidRestores a stored conversation so its transcript renders and follow-up questions continue the same thread. Does nothing when persistence is disabled or the thread is not stored.
clear() => voidDeletes the stored conversations. The open conversation stays on screen but is no longer saved.

StoredConversationThread​

Each entry in threads has the following shape:

PropertyTypeDescription
threadIdstringBackend thread ID of the conversation.
titlestringFirst question of the conversation, a file name for file-only questions, or an empty string.
createdAtnumberTimestamp (epoch ms) of the first stored answer.
updatedAtnumberTimestamp (epoch ms) of the latest answer. Only a new answer updates it; restoring the conversation or voting does not.
qasQACompleted[]The completed question-answer pairs of the conversation, including sources, file attachments, and feedback state.

Examples​

History sidebar​

import { useConversationHistory } from "@kapaai/react-sdk";

function HistorySidebar() {
const { enabled, threads, restoreThread } = useConversationHistory();

if (!enabled || threads.length === 0) {
return null;
}

return (
<ul>
{threads.map((thread) => (
<li key={thread.threadId}>
<button onClick={() => restoreThread(thread.threadId)}>
{thread.title || "Untitled conversation"}
</button>
</li>
))}
</ul>
);
}

After restoreThread is called, useChat returns the restored conversation and thread ID, and the next submitQuery continues that thread with full context on the backend.

Clearing history​

Call clear from the hook to delete the stored history, for example when a user signs out. The list empties, and the open conversation stays on screen but is no longer saved:

const { clear } = useConversationHistory();

To delete the history outside a KapaProvider, for example after turning the feature off, use clearConversationHistory with the same integrationId and storageKey you give KapaProvider:

import { clearConversationHistory } from "@kapaai/react-sdk";

clearConversationHistory({ integrationId: "your-integration-id" });

Notes​

  • History is stored unencrypted in localStorage. It is readable by JavaScript running on the page and persists until removed or evicted.
  • History is kept per browser and per storage key. Users do not see their conversations on another device or in another browser.
  • The provider keeps up to maxThreads conversations (50 by default) and about 1 MB of data, and drops the oldest conversations first when either limit is reached.

Requirements​

The useConversationHistory hook must be used within a KapaProvider component.