|
1 | | -# Threads |
| 1 | +# Threads and items |
2 | 2 |
|
3 | | -Threads are the core unit of ChatKit: a single conversation timeline that groups messages, tool calls, widgets, and related metadata. |
| 3 | +In ChatKit, a **thread** represents a single conversation. It is the unit that ties together everything that happens in that conversation: messages, widgets, actions, system signals, and metadata. A thread is stored as an ordered history of **thread items**, which ChatKit loads, paginates, and renders as needed. |
4 | 4 |
|
5 | | -## Lifecycle |
6 | | -- When a user submits a message and no thread exists, `ChatKitServer` creates one by calling your store's [`save_thread`](../../api/chatkit/store/#chatkit.store.Store.save_thread). |
7 | | -- As responses stream back, `ChatKitServer` automatically persists thread items as they are completed—see [Thread items](thread-items.md) and [Stream responses back to your user](../guides/stream-thread-events.md) for how events drive storage. |
8 | | -- Update titles or metadata intentionally in your integration (e.g., after summarizing a topic) by calling [`store.save_thread`](../../api/chatkit/store/#chatkit.store.Store.save_thread) with the new values. |
9 | | -- When history is enabled client-side, ChatKit retrieves past threads. The user can continue any previous thread by default. |
10 | | -- Archive or close threads according to your policies: mark them read-only (e.g., [disable new messages](../guides/add-features/disable-new-messages.md)) or delete them if you no longer want them discoverable. |
| 5 | +## What is a thread? |
11 | 6 |
|
| 7 | +A thread is an ordered timeline that contains: |
| 8 | + |
| 9 | +- Conversation history (user and assistant messages) |
| 10 | +- Structured content such as widgets and workflows |
| 11 | +- Internal signals that guide processing or model behavior |
| 12 | +- Metadata like titles or status flags |
| 13 | + |
| 14 | +Threads are persisted by your store implementation and can be updated, continued, or made read-only according to your application’s needs. |
| 15 | + |
| 16 | +## What are thread items? |
| 17 | + |
| 18 | +Thread items are the individual records that make up a thread. Each item represents one meaningful unit in the conversation history, such as: |
| 19 | + |
| 20 | +- A user message |
| 21 | +- An assistant response |
| 22 | +- A widget rendered by the assistant |
| 23 | +- A non-visible signal used only for model input |
| 24 | + |
| 25 | +ChatKit maintains the order of items, streams new ones as they are produced, and paginates them when history is loaded. |
| 26 | + |
| 27 | +## How threads are created and updated |
| 28 | + |
| 29 | +A typical thread lifecycle looks like this: |
| 30 | + |
| 31 | +- **Thread creation**: When a user submits a message and no thread exists yet, ChatKitServer creates one and persists it by calling your store’s `save_thread`. |
| 32 | +- **Appending items**: As the server streams a response, ChatKit persists thread items automatically as each item completes. Streaming events directly drive what gets stored. |
| 33 | +- **Updating metadata**: During respond, you can freely mutate the thread object (for example, to set or refine the title). ChatKit automatically persists these updates when the response completes. You can also call store.save_thread explicitly if needed. |
| 34 | +- **Loading history**: When history is enabled client-side, ChatKit retrieves past threads and their items. Users can continue an existing thread by default. |
| 35 | +- **Closing or archiving**: Threads can be marked read-only (for example, by disabling new messages) or deleted entirely if they should no longer be discoverable. |
| 36 | + |
| 37 | + |
| 38 | +## How thread items are used |
| 39 | + |
| 40 | +Thread items serve two primary purposes in ChatKit: |
| 41 | + |
| 42 | +### Model input |
| 43 | + |
| 44 | +Your server's [`respond`](../../api/chatkit/server/#chatkit.server.ChatKitServer.respond) logic reads thread items to construct input for the model input. This ensures the model sees the full conversational context both during an active response and when a user resumes a past thread. |
| 45 | + |
| 46 | +See [Respond to a user message](../guides/respond-to-user-message.md) for a full walkthrough. |
| 47 | + |
| 48 | +### UI rendering |
| 49 | + |
| 50 | +On the client, ChatKit.js renders items incrementally as they stream in for the active thread. When a past thread is loaded, the same persisted items are re-rendered to reconstruct the conversation UI. |
| 51 | + |
| 52 | +## Core item types |
| 53 | + |
| 54 | +### User messages |
| 55 | + |
| 56 | +[`UserMessageItem`](../../api/chatkit/types/#chatkit.types.UserMessageItem)s represent end-user input. They may include: |
| 57 | + |
| 58 | +- Plain text entered by the user |
| 59 | +- Optional `quoted_text` for reply-style UIs |
| 60 | +- Attachment metadata |
| 61 | + |
| 62 | + |
| 63 | +User text is not Markdown-rendered, but it may contain [@-mentions](../guides/accept-rich-user-input.md#-mentions-tag-entities-in-user-messages) if your integration enables them. |
| 64 | + |
| 65 | + |
| 66 | +### Assistant messages |
| 67 | + |
| 68 | +[`AssistantMessageItem`](../../api/chatkit/types/#chatkit.types.AssistantMessageItem)s represent assistant output. Their content can include: |
| 69 | + |
| 70 | +- Markdown-rendered text |
| 71 | +- Tool call outputs |
| 72 | +- Widgets and structured UI elements |
| 73 | +- [Inline annotations](../guides/add-annotations.md) |
| 74 | + |
| 75 | +Assistant text supports rich Markdown and is rendered progressively as it streams. |
| 76 | + |
| 77 | +#### Markdown support |
| 78 | + |
| 79 | +Assistant messages support: |
| 80 | + |
| 81 | +- GitHub-flavored Markdown (headings, lists, code blocks, links, blockquotes) |
| 82 | +- Stable list rendering during streaming (Safari-safe, no reflow) |
| 83 | +- Optional single-newline line breaks |
| 84 | +- Syntax-highlighted, copyable code blocks |
| 85 | +- LaTeX math (inline and block) |
| 86 | +- Tables with automatic sizing and horizontal scrolling |
| 87 | +- Inline annotations that create interactive affordances in the UI |
| 88 | + |
| 89 | +### Hidden context items |
| 90 | + |
| 91 | +Hidden context items are included in model input but are not rendered in the chat UI. They allow the model to react to what happened in the interface, not just what the user typed. |
| 92 | + |
| 93 | +Typical use cases include recording widget actions, selection state, or system signals. |
| 94 | + |
| 95 | +- **[`HiddenContextItem`](../../api/chatkit/types/#chatkit.types.HiddenContextItem)**: Integration-defined hidden context. You control its schema and how it is converted for the model. |
| 96 | + |
| 97 | +- **[`SDKHiddenContextItem`](../../api/chatkit/types/#chatkit.types.SDKHiddenContextItem)**: Hidden context inserted by the ChatKit Python SDK for its own internal operations. Most applications do not need to modify this unless overriding conversion behavior. |
| 98 | + |
| 99 | +## Thread item actions |
| 100 | + |
| 101 | +Thread item actions are quick action buttons associated with an assistant turn. They let users act on the output—such as retrying a response, copying content, or submitting feedback. |
| 102 | + |
| 103 | +Actions are configured client-side using the [threadItemActions option](https://openai.github.io/chatkit-js/api/openai/chatkit-react/type-aliases/threaditemactionsoption/). |
| 104 | + |
| 105 | +## Converting items to model input |
| 106 | + |
| 107 | +[`ThreadItemConverter`](../../api/chatkit/agents/#chatkit.agents.ThreadItemConverter) translates stored thread items into model-ready input items. The default converter understands common ChatKit item types such as messages, widgets, workflows, and tasks. |
| 108 | + |
| 109 | +You can override the converter when you need custom behavior. For example: |
| 110 | + |
| 111 | +- Formatting attachments for the model |
| 112 | +- Translating tags or mentions into structured input |
| 113 | +- Summarizing rich widgets into text the model can consume |
| 114 | + |
| 115 | +Custom conversion is typically paired with prompting so the model receives a coherent representation of the conversation. |
12 | 116 |
|
13 | 117 | ## Related guides |
14 | | -- [Persist ChatKit threads and messages](../guides/persist-chatkit-data.md) |
15 | | -- [Save thread titles](../guides/add-features/save-thread-titles.md) |
16 | | -- [Disable new messages for a thread](../guides/add-features/disable-new-messages.md) |
| 118 | +- [Respond to a user message](../guides/respond-to-user-message.md) |
| 119 | +- [Pass extra app context to your model](../guides/pass-extra-app-context-to-your-model.md) |
| 120 | +- [Add annotations in assistant messages](../guides/add-annotations.md) |
| 121 | +- [Accept rich user input](../guides/accept-rich-user-input.md#-mentions-tag-entities-in-user-messages) |
| 122 | +- [Handle feedback](../guides/handle-feedback.md) |
| 123 | +- [Let users browse past threads](../guides/browse-past-threads.md) |
0 commit comments