> ## 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.

# Messages and Threads

> Values move, threads keep: how every component reads and writes a conversation.

A conversation in a workflow is made of two different things. **Messages are values.** A **thread is a place.** Every
component speaks messages, and one component, [`data.thread`](https://docs.blobhub.io/blob-types/workflow/workflows/component-data-thread),
turns the place into a value and back. This page is the whole story; the pages it links hold the details.

## Values and places

* **`message`** is the platform's one piece of content: a role, blocks, `attributes` (`id`, `created_at`) and
  `metadata`. Every port that carries content speaks it: a prompt, a query, a scraped page, an answer
  ([Data Types](https://docs.blobhub.io/blob-types/workflow/workflows/data-types.md#message)).
* **`messages`** is an ordered list of them: chunks, search results, tools, a few-shot prompt, a history.
* Both are **values**. A component reads them from its inputs and writes them to its outputs. A flow keeps one in a
  `data.message` or `data.messages` component, for the execution or in the session, and every write replaces it whole.
* A **thread** is not a value but a session's append-only log. Each item is a message plus what the platform records
  about its post: `id`, `source`, `created_at`, `parent_id` and a role. Many parties write to it, every post raises an
  event, and the conversation widget follows it live
  ([Thread session object](https://docs.blobhub.io/blob-types/workflow/session-objects/thread/introduction.md)).

`data.thread` is the only component that knows threads. It is also the only way a thread meets a flow:

* **Read**, a thread is a `messages` value, and each message carries its item's id as `attributes.id`.
* **Written**, a thread appends. A `message` becomes one item and a `messages` value one item per message, in order.
  A message whose `attributes.id` already names an item of that thread is skipped, so writing back what was read posts
  only what was added.

No other component has a thread port or a thread mode. A component reads a thread through any input that takes
`messages`, and posts to one through any output that writes a `message` or `messages`.

```mermaid theme={null}
flowchart LR
    thread[("data.thread<br/>a session's log<br/>appended")]
    value[("data.messages<br/>a value<br/>replaced whole")]
    any_component["any component<br/>message and messages ports"]
    thread -- "read: messages, with item ids" --> any_component
    value -- "read: messages" --> any_component
    any_component -- "write: appended, known ids skipped" --> thread
    any_component -- "write: replaced" --> value
```

## Every component, by its ports

| Component | Reads a thread through | Posts to a thread through |
| :- | :- | :- |
| [`genai.chat`](https://docs.blobhub.io/blob-types/workflow/workflows/component-genai-chat) | `history` | `output` |
| `genai.agent.basic` | `history` | `agent_output`, `history`, `event_log` |
| [`logic.code`](https://docs.blobhub.io/blob-types/workflow/workflows/component-code) | Its inputs, `thread_messages` | Its outputs, `post_message` |
| [`logic.copy`](https://docs.blobhub.io/blob-types/workflow/workflows/component-logic-copy) | `input` | `output` |
| `text.render_template` | Its inputs | |
| `database.vector.ingest` | `chunks` | |
| `web.search` | | `results` |
| `logic.insert` | `messages` | `messages` |

`genai.chat` answers what was posted. `genai.agent.basic` is an agent on a conversation: its `history` output, the
history it read plus its turn, posts only its turn, and its event log can post its steps. `logic.code` serves tool
results ([Tool Calling](https://docs.blobhub.io/blob-types/workflow/workflows/tool-calling.md)) and anything custom, reading with
`thread_messages(alias)` and posting with `post_message(alias, message)`. `logic.copy` moves messages
between places, below. `text.render_template` renders a conversation as a transcript, and `database.vector.ingest`
indexes one, one item per chunk. `web.search` posts results to a research log. `logic.insert` is not needed on a thread:
a post is the append.

A port typed `message`, such as `current`, takes no thread, as it takes no `data.messages`: on a thread the newest
request is already the end of `history`.

## Mixing them

Both sides are values, so a flow mixes them freely. A chat reads a thread and writes its answer to a `data.message`
for a later step. An agent reads a few-shot prompt from a `data.messages` and posts to a thread.

Mixing has one hazard: a component that returns the whole history with its own messages appended would post the
history again. The id rule closes it. The history the component read carries its item ids, and only what it added is
posted. A chat wired to the same thread on `history` and `output` therefore posts only its answer.

## Moving between places

A move is a [`logic.copy`](https://docs.blobhub.io/blob-types/workflow/workflows/component-logic-copy) between two data components.

| From, to | For example |
| :- | :- |
| A thread to a `data.messages` | A conversation frozen as a value: a transcript kept for review, examples for a prompt. |
| A `data.messages` to a thread | An import: a seeded conversation, or an old chat history. |
| A thread to another thread | A conversation handed on: a digest, a hand-off, a fork. |

`logic.copy` copies its `input` to its `output` unchanged, whatever the value. For a `messages` value it takes the
rules a chat applies to its history, `roles`, `skip_roles`, `anchor`, `from`, `to` and `keep`
([Selecting a History](https://docs.blobhub.io/blob-types/workflow/workflows/selecting-a-history.md)): `from: -10` keeps the last ten. Anything
finer, a date or a search, is a `logic.code`. A copy into a thread appends: messages from another thread or from a
value become new items, stamped with the run's `source`, and a thread copied into itself posts nothing.

## Which to choose

* A conversation or a log that several parties append to and someone follows is a **thread**. Ostrava Pumps' service
  desk keeps its requests in one: people post through the widget, a chat answers, a `logic.code` posts tool results.
* A value a flow computes and replaces is a **`data.message` or `data.messages`**: a plan, a table of answers,
  retrieved sources, a prompt.
* A flow that keeps its conversation in a `data.messages` keeps working. The history it holds is a value, and chat
  reads it as it reads a thread.


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