> ## Documentation Index
> Fetch the complete documentation index at: https://docs.blobhub.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Thread Component (data.thread)

> A session thread as a data component: read as messages, written by posting.

A `data.thread` names a [thread](https://docs.blobhub.io/blob-types/workflow/session-objects/thread/introduction.md) of the session by alias,
and turns it into a value and back. Read, the thread is a `messages` value. Written, it posts. It is the only component
that knows threads; every other component reads one through an input that takes `messages` and writes one through an
output that carries a `message` or `messages`
([Messages and Threads](https://docs.blobhub.io/blob-types/workflow/workflows/messages-and-threads.md)).

## Ports

A `data.thread` has one data input, `alias`, and the data reference every data component has, which other components
connect to. It has no value and no persistence port: a thread always lives in the session.

| Route | Category | Value | Notes |
| :- | :- | :- | :- |
| `alias` | Data input | `text` | The alias of the thread. Required, and may be a template over the run's input, as every data component's alias may. |

A `data.thread` without an alias fails the definition's check with `Thread component needs an alias`. It is never a
step: no flow route enters or leaves it.

## Read

A component that reads a `data.thread` gets a `messages` value, each message built from one item of the thread:

* `attributes.id` is the item's id, and `attributes.created_at` its time.
* `metadata.role` is the item's role, absent for an item posted without one.
* `role`, `content` and the rest of `metadata` follow [the mapping](#the-mapping).

**What a read sees** is the items posted before its execution started, plus the items that execution posted itself. A
message posted by someone else mid-run waits for the next run. Two branches of one run answering one thread see each
other's posts.

**A part is read as one message.** A message too long for one item is posted as consecutive parts, and a read joins
them back into the message that was written. The joined message has the first part's id and time. A part that is
not found is marked: its number is in `metadata.missing_parts`, and a text block reading
`[Part N of T of this message is missing.]` stands in its place (`[Part N of this message is missing.]` when the
parts name no usable total). A part can be missing because it was lost, or because its writer has not stored it
yet.

**Where a component selects what it reads**, the selection applies as the thread is read, newest first, so a long
thread is not read whole unless `keep` is set: `genai.chat`'s `history` and `logic.copy`'s `input` take the
[history selection](https://docs.blobhub.io/blob-types/workflow/workflows/selecting-a-history.md). Everywhere else the read is the visible
thread, whole, with the default selection: items whose role is `note` are left out.

A thread that does not exist yet reads as an empty history. An alias that holds another kind of object fails the node.

## Write

A `message` posts one item, and a `messages` value one item per message, in order. Any other value type fails the node
with `A thread takes a message or messages, not <type>`.

**A message whose `attributes.id` already names an item of that thread is skipped.** This is what lets a component hand
back the history it read with its own messages added: only what was added is posted. A `genai.chat` that reads a thread
on `history` and writes it on `output` posts only its answer.

What a post records on the item:

| Field | Value |
| :- | :- |
| `metadata.role` | The message's own `metadata.role` when it has one, otherwise from its role ([the mapping](#the-mapping)). |
| `source` | Who made the write, stamped by the platform: `{kind: workflow, user_id, execution_id, component_id, depth}`, with the `listener` that started the run when one did. A flow cannot set it ([Thread items](https://docs.blobhub.io/blob-types/workflow/session-objects/thread/introduction.md#thread-items)). |
| `parent_id` | For an answer, the newest request the component's read saw, counting items it posted earlier in the same write. For a message of tool results, the item holding the calls they answer. Otherwise none. |
| `metadata` | The message's `metadata`: the `model`, `usage` and `finish` the conversation widget shows. |
| `id` | The platform's. A retried run of the component posts under the same ids, so it cannot post an item twice, and the partial message streamed before an answer carries the id the answer is posted under ([Streaming](https://docs.blobhub.io/blob-types/workflow/workflows/streaming.md)). |

**The first post creates the thread.** A `data.thread` whose alias holds nothing creates the thread with its first
post. An alias that holds another kind of object fails the node, as a post there is refused
(`invalid_thread_envelope`), and a post never replaces an existing thread.

**A message over the item limit is posted in parts.** An item holds up to 262,144 bytes (256 KiB)
([`workflow.thread_item_bytes`](https://docs.blobhub.io/general/limits#workflow)). A longer message is cut into the fewest consecutive items
that fit, in block order, each carrying `metadata.part` with `index` (from 1), `total`, `of` (the first part's id) and
`continues` (`true` when the part ends in a piece of text that the next part carries on). A text block too long for a
part on its own is cut between characters. Any other single block over the limit fails the post with
`thread_item_too_large`. A message that fits takes no `part`.

A message of 2 to 9 parts is stored in one transaction, so a post that stops partway leaves all of its parts or
none. A longer one is stored a part at a time, in order, and a post that stops partway leaves its first parts.

**A message already stored is not stored again.** A post that finds any part of its message stored stores nothing and
returns the oldest part present, so a retry that regenerated the message keeps the stored one, and a part deleted from
it stays deleted. The one exception is a retry that regenerates the message into fewer parts after the stored one lost
its first two parts and every part the regenerated message is now cut into: those parts are posted under their ids. A
`genai.chat` retried leg finds its stored answer by any of its first 100 parts.

A split message raises one [`thread.item_posted`](https://docs.blobhub.io/blob-types/workflow/workflows/session-events#types) once its parts
are stored: its `item` block names the first part's id and lists every part's id in `parts`, in order. A retry that
finds a message of more than 9 parts without its last part stores nothing and raises the event with the oldest part
present, the present ids in `parts` and the absent numbers in `missing_parts`, again on each such retry. A message
that carries a `part` or `missing_parts` of its own is posted without it, and split afresh if it needs to be.

## The mapping

A message becomes an item with the message's `content` unchanged, and an item becomes a message by the rules below,
the same on every turn, so a stored thread renders identically each time it is read.

| Message role | Item `metadata.role` | Read back as |
| :- | :- | :- |
| any, with its own `metadata.role` (`summary`, `handover`, ...) | that role, as written | `user` |
| `assistant` | `reply` | `assistant` |
| `user` holding only `tool_result` blocks | `reply` | `user`, as tool results travel |
| `user`, `system`, `event` or none | `request` | `user` |
| none, an item posted before roles existed | none | `user` |
| any, written as a note | `note` | `user`, and left out by the default selection |

A person's post through the conversation widget is a `request`. An item whose `metadata.role` is `reply` and that holds
no `tool_result` block reads back as `assistant`; every other item reads back as `user`.

## A chat on a thread

A `genai.chat` reads and answers a thread through its ports, with no thread port and no thread mode. One `data.thread`
is wired to `history`, and `output` is wired to the same `data.thread`. The conversation widget
([`widget.thread`](https://docs.blobhub.io/blob-types/workflow/playgrounds/widgets)) shows the thread, and a post in it starts the run:

```mermaid theme={null}
flowchart LR
    widget["widget.thread<br/>servicedesk/requests"]
    thread[("data.thread<br/>servicedesk/requests")]
    chat["genai.chat"]
    widget -- "a person posts, and the widget starts the run" --> chat
    thread -- "history: the thread as messages" --> chat
    chat -- "output: the answer, posted as an item" --> thread
    thread -. "followed live" .-> widget
```

In the stored definition these are two connections between the same two components:

```json theme={null}
[
  {"source_id": "<chat>", "target_id": "<thread>", "route": "history",
   "source_port": "data_input", "target_port": "data_reference"},
  {"source_id": "<chat>", "target_id": "<thread>", "route": "output",
   "source_port": "data_output", "target_port": "data_reference"}
]
```

* **What the widget shows is what chat read and wrote.** The person's post is already in the thread when the run
  starts, so `current` is left unwired. The answer is posted to the thread, streamed there first as a partial message,
  and nothing else writes the conversation.
* **When `output` is wired to anything else**, chat writes its answer as a value, and nothing is posted.
* **A `logic.code` on the same thread** reads it with `thread_messages(alias)` and posts with `post_message(alias,
  message)`, or through ports wired to the `data.thread`
  ([Code Component](https://docs.blobhub.io/blob-types/workflow/workflows/component-code.md#threads)); the tool loop works this way
  ([Tool Calling](https://docs.blobhub.io/blob-types/workflow/workflows/tool-calling.md)).
* **Reading a thread and posting to another** is a run of
  [`logic.copy`](https://docs.blobhub.io/blob-types/workflow/workflows/component-logic-copy) between two `data.thread` components.

How chat chooses what to send is on [Selecting a History](https://docs.blobhub.io/blob-types/workflow/workflows/selecting-a-history.md), and the
component itself on [Chat Component](https://docs.blobhub.io/blob-types/workflow/workflows/component-genai-chat.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.