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

*** ** * ** ***


```
public interface A2uiMessageProcessor
```

<br />

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

|---|---|
| `https://developer.android.com/reference/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 methods |
|---|---|
| `abstract void` | `https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiMessageProcessor#collectMessages()()` Suspends and continuously processes all incoming messages queued by `https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiMessageProcessor#processMessage(androidx.a2ui.model.protocol.A2uiServerToClientMessage)`. |
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiSurfaceModel>>` | `https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiMessageProcessor#getActiveSurfaces()()` A flow of the active surface models currently managed by the processor. |
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/a2ui/model/protocol/A2uiClientToServerMessage>` | `https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiMessageProcessor#getOutboundEvents()()` A flow of user actions and error payloads to be transmitted to the server. |
| `abstract void` | `https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiMessageProcessor#processError(androidx.a2ui.model.protocol.A2uiClientErrorMessage)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/a2ui/model/protocol/A2uiClientErrorMessage error)` Enqueues a client error message to be transmitted back to the server via `https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiMessageProcessor#outboundEvents()`. |
| `abstract void` | `https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiMessageProcessor#processMessage(androidx.a2ui.model.protocol.A2uiServerToClientMessage)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/a2ui/model/protocol/A2uiServerToClientMessage message)` Enqueues a parsed protocol message to be processed. |

| ### Extension functions |
|---|---|
| `default final void` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiMessageProcessorKt.https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiMessageProcessor#(androidx.a2ui.model.processor.A2uiMessageProcessor).processInput(androidx.a2ui.model.processor.A2uiMessageParser,kotlin.Any)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiMessageProcessor receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiMessageParser<@https://developer.android.com/reference/androidx/annotation/NonNull T> parser, @https://developer.android.com/reference/androidx/annotation/NonNull T input )` Parses `https://developer.android.com/reference/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/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 methods

### collectMessages

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

```
abstract void collectMessages()
```

Suspends and continuously processes all incoming messages queued by `https://developer.android.com/reference/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/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.

### getActiveSurfaces

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

```
abstract @NonNull StateFlow<@NonNull List<@NonNull A2uiSurfaceModel>> getActiveSurfaces()
```

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

### getOutboundEvents

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

```
abstract @NonNull Flow<@NonNull A2uiClientToServerMessage> getOutboundEvents()
```

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

### processError

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

```
abstract void processError(@NonNull A2uiClientErrorMessage error)
```

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/a2ui/model/protocol/A2uiClientErrorMessage error` | 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)

```
abstract void processMessage(@NonNull A2uiServerToClientMessage message)
```

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/androidx/a2ui/model/processor/A2uiMessageProcessor#collectMessages()` is actively running.

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

## Extension functions

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

```
default final void <T extends Object> A2uiMessageProcessorKt.processInput(
    @NonNull A2uiMessageProcessor receiver,
    @NonNull A2uiMessageParser<@NonNull T> parser,
    @NonNull T input
)
```

Parses `https://developer.android.com/reference/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/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/androidx/a2ui/model/protocol/A2uiException` (such as `https://developer.android.com/reference/androidx/a2ui/model/protocol/A2uiException.A2uiValidationException`), the exception is caught, converted into an `https://developer.android.com/reference/androidx/a2ui/model/protocol/A2uiClientErrorMessage`, and sent to `https://developer.android.com/reference/androidx/a2ui/model/processor/A2uiMessageProcessor#processError(androidx.a2ui.model.protocol.A2uiClientErrorMessage)`. Unexpected runtime exceptions are not caught.

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