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

# Insert Component (logic.insert)

> Places one message into a list of messages at a position and returns the list.

A `logic.insert` adds one message to a `messages` list. It places the message at the position `index` names, or at the
end, and writes the longer list to its output. Use it to build a history by hand, or to put a message in front of a
conversation before a chat reads it.

| Route | Category | Value | Notes |
| :- | :- | :- | :- |
| `success` | Flow output | — | The message is in the list. |
| `failure` | Flow output | — | `message` holds no value ([When it fails](#when-it-fails)). |
| `messages` | Data input | `messages` | Required. The list to add to. No value means an empty list. |
| `message` | Data input | `message` | Required. The message to add. |
| `index` | Data input | `integer` | Where to place it. With no value the message goes at the end. |
| `messages` | Data output | `messages` | Required. The list with the message in it. |

## Where the message goes

`index` places the message the way a Python `list.insert` does.

* **`0` puts it first,** and the messages that were there move up by one.
* **A positive `index` puts it before the message now at that position.** `2` leaves two messages ahead of it.
* **A negative `index` counts from the end.** `-1` puts the message before the last one, not after it.
* **An `index` out of range goes to the nearest end.** One past the length of the list appends, and one before the
  start puts the message first. It never fails.
* **No `index` appends.** The message goes after the last.

A message that carries no `attributes` is given an `id` and a `created_at` time as it goes in. A message that already
has `attributes` keeps them.

## When it fails

A `logic.insert` whose `message` holds no value takes `failure`, with an error event, `Cannot insert an empty or
uninitialized message.`, and writes nothing. A `message` value that holds no message inside it takes `failure` the same
way, without the event. A `messages` that holds no value does not fail: the list starts empty.

## Example

Ostrava Pumps opens each service conversation with a standing instruction. A `data.message` called `House rules` holds
the instruction, and a `data.messages` called `Request history` holds what the customer has said so far. A
`logic.insert` has `messages` wired to `Request history` as input and as output, `message` wired to `House rules`, and
`index` `0`, so the rules are the first message a chat reads, whatever the length of the history.

```json theme={null}
{
  "components": [
    {"id": "put_first", "category": "processor", "type": "logic.insert", "name": "Rules first",
     "position": {"x": 300, "y": 200},
     "ports": [{"category": "data_input", "route": "index", "value": {"type": "integer", "integer": 0}}]}
  ],
  "connections": [
    {"id": "c1", "source_id": "put_first", "target_id": "<request history>", "route": "messages",
     "source_port": "data_input", "target_port": "data_reference"},
    {"id": "c2", "source_id": "put_first", "target_id": "<house rules>", "route": "message",
     "source_port": "data_input", "target_port": "data_reference"},
    {"id": "c3", "source_id": "put_first", "target_id": "<request history>", "route": "messages",
     "source_port": "data_output", "target_port": "data_reference"},
    {"id": "c4", "source_id": "put_first", "target_id": "<next step>", "route": "success",
     "source_port": "flow_output", "target_port": "flow_input"},
    {"id": "c5", "source_id": "put_first", "target_id": "<end>", "route": "failure",
     "source_port": "flow_output", "target_port": "flow_input"}
  ]
}
```


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