# A2uiMessageProcessor

Artifact: [androidx.a2ui:a2ui-model](https://developer.android.com/jetpack/androidx/releases/a2ui) [View Source](https://cs.android.com/search?q=file:androidx/a2ui/model/processor/A2uiMessageProcessor.kt+class:androidx.a2ui.model.processor.A2uiMessageProcessor) Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/a2ui#1.0.0-alpha02)

*** ** * ** ***


```
interface A2uiMessageProcessor
```

<br />

Known direct subclasses [A2uiCoreMessageProcessor](https://developer.android.com/reference/kotlin/androidx/a2ui/engine/processor/A2uiCoreMessageProcessor)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/a2ui/engine/processor/A2uiCoreMessageProcessor` | The central orchestration engine for the A2UI core data layer. |

*** ** * ** ***

The A2UI message processor.

This processor is the central orchestration engine for the A2UI core data layer. It receives parsed protocol messages from the host application and exposes the outbound network events back to the host framework.

TODO(b/532001163): add an sample for this API and reference it from `collectMessages` and `processMessage`.

## Summary

| ### Public functions |
|---|---|
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#collectMessages()()` Suspends and continuously processes all incoming messages queued by `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#processMessage(androidx.a2ui.model.protocol.A2uiServerToClientMessage)`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#processError(androidx.a2ui.model.protocol.A2uiClientErrorMessage)(error: https://developer.android.com/reference/kotlin/androidx/a2ui/model/protocol/A2uiClientErrorMessage)` Enqueues a client error message to be transmitted back to the server via `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#outboundEvents()`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#processMessage(androidx.a2ui.model.protocol.A2uiServerToClientMessage)(message: https://developer.android.com/reference/kotlin/androidx/a2ui/model/protocol/A2uiServerToClientMessage)` Enqueues a parsed protocol message to be processed. |

| ### Public properties |
|---|---|
| `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiSurfaceModel>>` | `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#activeSurfaces()` A flow of the active surface models currently managed by the processor. |
| `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://developer.android.com/reference/kotlin/androidx/a2ui/model/protocol/A2uiClientToServerMessage>` | `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#outboundEvents()` A flow of user actions and error payloads to be transmitted to the server. |

| ### Extension functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor.https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#(androidx.a2ui.model.processor.A2uiMessageProcessor).processInput(androidx.a2ui.model.processor.A2uiMessageParser,kotlin.Any)( parser: https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageParser<T>, input: T )` Parses `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/package-summary#(androidx.a2ui.model.processor.A2uiMessageProcessor).processInput(androidx.a2ui.model.processor.A2uiMessageParser,kotlin.Any)` using `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/package-summary#(androidx.a2ui.model.processor.A2uiMessageProcessor).processInput(androidx.a2ui.model.processor.A2uiMessageParser,kotlin.Any)` and enqueues the resulting message into this processor. |

## Public functions

### collectMessages

Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/a2ui#1.0.0-alpha02)

```
suspend fun collectMessages(): Unit
```

Suspends and continuously processes all incoming messages queued by `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#processMessage(androidx.a2ui.model.protocol.A2uiServerToClientMessage)`.

This method acts as the engine's main processing loop. It should be launched within a coroutine scope tied to the host application's lifecycle (such as a ViewModel). If this method is not running, any messages passed to `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#processMessage(androidx.a2ui.model.protocol.A2uiServerToClientMessage)` will remain queued.

When the calling coroutine scope is cancelled, the processing loop will gracefully terminate.

### processError

Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/a2ui#1.0.0-alpha02)

```
fun processError(error: A2uiClientErrorMessage): Unit
```

Enqueues a client error message to be transmitted back to the server via `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#outboundEvents()`.

| Parameters |
|---|---|
| `error: https://developer.android.com/reference/kotlin/androidx/a2ui/model/protocol/A2uiClientErrorMessage` | The client error message to transmit. |

### processMessage

Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/a2ui#1.0.0-alpha02)

```
fun processMessage(message: A2uiServerToClientMessage): Unit
```

Enqueues a parsed protocol message to be processed.

Note that this method does not execute the processing itself. Messages are queued and will only be handled while `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#collectMessages()` is actively running.

This method is thread-safe and can be called from any thread.

## Public properties

### activeSurfaces

Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/a2ui#1.0.0-alpha02)

```
val activeSurfaces: StateFlow<List<A2uiSurfaceModel>>
```

A flow of the active surface models currently managed by the processor.

### outboundEvents

Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/a2ui#1.0.0-alpha02)

```
val outboundEvents: Flow<A2uiClientToServerMessage>
```

A flow of user actions and error payloads to be transmitted to the server.

## Extension functions

### A2uiMessageProcessor.processInput

Artifact: [androidx.a2ui:a2ui-model](https://developer.android.com/jetpack/androidx/releases/a2ui) [View Source](https://cs.android.com/search?q=file:androidx/a2ui/model/processor/A2uiMessageProcessor.kt+function:processInput)

```
fun <T : Any?> A2uiMessageProcessor.processInput(
    parser: A2uiMessageParser<T>,
    input: T
): Unit
```

Parses `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/package-summary#(androidx.a2ui.model.processor.A2uiMessageProcessor).processInput(androidx.a2ui.model.processor.A2uiMessageParser,kotlin.Any)` using `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/package-summary#(androidx.a2ui.model.processor.A2uiMessageProcessor).processInput(androidx.a2ui.model.processor.A2uiMessageParser,kotlin.Any)` and enqueues the resulting message into this processor.

If parsing fails with an `https://developer.android.com/reference/kotlin/androidx/a2ui/model/protocol/A2uiException` (such as `https://developer.android.com/reference/kotlin/androidx/a2ui/model/protocol/A2uiException.A2uiValidationException`), the exception is caught, converted into an `https://developer.android.com/reference/kotlin/androidx/a2ui/model/protocol/A2uiClientErrorMessage`, and sent to `https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageProcessor#processError(androidx.a2ui.model.protocol.A2uiClientErrorMessage)`. Unexpected runtime exceptions are not caught.

| Parameters |
|---|---|
| `parser: https://developer.android.com/reference/kotlin/androidx/a2ui/model/processor/A2uiMessageParser<T>` | The parser to use for parsing the input. |
| `input: T` | The raw input to parse and process. |