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

# Code Component (logic.code)

> Execute custom Python code within the workflow environment.

The `logic.code` component executes custom Python code safely within the workflow execution environment. It provides a sandboxed space equipped with helpers for input/output data handling and flow control.

## Runtime Environment

The execution occurs inside a hardened **Python 3** environment.

### Available Modules

You can leverage several standard libraries and helpful built-ins:

**Standard Libraries:**

* `json`, `uuid`, `time`, `datetime`, `zoneinfo`
* `functools`, `traceback`, `copy`, `decimal`
* `textwrap`, `math`, `re`, `abc`
* `typing`, `Union`, `Literal`

**Exceptions:**

* `Exception`
* `ValueError`
* **`Exit`**: A special exception intentionally designed to exit the active code block immediately.

## Input & Output

### Reading Inputs

You can access data arriving at the component's input ports. Inputs are received as explicitly typed **Value Objects**.

* **`data_inputs`**: A list of all received input port data.
* **`get_data_input(route)`**: A helper function to grab the Value Object of a specific input port by its exact route name.

```python theme={null}
# Returns a dict structurally conforming to a Value Object.
input_val = get_data_input("user_query")

if input_val and input_val.get("type") == "message":
    pass
```

### Writing Outputs

You can emit data out of the component by pushing **Value Objects** to output routes.

* **`set_data_output(route, value, persist=False)`**: Assigns a value to a targeted output port.
  * `route`: Output port name (e.g., "result").
  * `value`: The Value Object payload.
  * `persist`: If `True`, stores the output specifically to processor data tracking logic for external visibility.

```python theme={null}
output_val = create_message(role="assistant", text="Processed Data")
set_data_output("result", output_val)
```

## Flow Control

You determine which logic route the workflow should navigate following code execution.

* **`set_result(route, terminal=False)`**: Declares the destination route.
  * `route`: Connection edge to traverse (`"success"`, `"failure"`, etc).
  * `terminal`: Set to `True` if this intentionally ends the entire workflow execution.
* **`exit()`**: Aborts the execution of the remainder of the active code script.

```python theme={null}
if user_exists:
    set_result("success")
else:
    set_result("failure")
```

## Helper Functions

### Data Helpers

* **`create_message(role, text=None, attribute_id=None)`**: Generates a standardized "message" Value Object dynamically.
* **`create_value(data_type, data)`**: Quickly wrap arbitrary matching data into a recognized Value structure.

### Session Management

Read or write semi-persistent object data retained to the execution's parent session:

* **`download_session_object(alias)`**
* **`upload_session_object(alias, value)`**
* **`delete_session_object(alias)`**

```python theme={null}
upload_session_object("run_count", {"count": 1})
```

### Logging & Events

Produce informational, warning, or failure events logged to the execution stream:

* **`post_output_event(message)`**
* **`post_warning_event(message)`**
* **`post_error_event(message)`**

### System Metadata

* **`current_time_utc_iso()`**: Gets UTC time dynamically.
* **`result`**: The immediate runtime representation of the result dictionary.

## Full Example

```python theme={null}
request_val = get_data_input("request")

if request_val and request_val.get("type") == "message":
    
    response_text = "Message received and acknowledged."
    
    msg = create_message("assistant", response_text)
    set_data_output("response", msg)
    
    post_output_event("Ack sent")
    set_result("success")
else:
    post_error_event("Request generic or missing type string")
    set_result("failure")
```
