Skip to content

Commit 5890b91

Browse files
authored
fix(transport): filter whitespace-only AI text responses (#759)
Modify `A2uiTransportAdapter` and `A2uiAgentConnector` to trim incoming text parts from the AI model and discard any that are empty or consist only of whitespace. This prevents the UI from rendering empty messages or trailing newlines when the model responds with whitespace chunks.
1 parent 1e221d0 commit 5890b91

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

packages/genui/lib/src/transport/a2ui_transport_adapter.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class A2uiTransportAdapter implements Transport {
6565
Stream<String> get incomingText => _pipeline
6666
.where((e) => e is TextEvent)
6767
.cast<TextEvent>()
68-
.map((e) => e.text);
68+
.map((e) => e.text.trim())
69+
.where((text) => text.isNotEmpty);
6970

7071
/// A stream of A2UI messages parsed from the input.
7172
@override

packages/genui_a2a/lib/src/a2ui_agent_connector.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ class A2uiAgentConnector {
206206
if (part is DataPart) {
207207
_processA2uiMessages(part.data);
208208
} else if (part is TextPart) {
209-
if (!_textController.isClosed) {
210-
_textController.add(part.text);
209+
final String trimmedText = part.text.trim();
210+
if (trimmedText.isNotEmpty && !_textController.isClosed) {
211+
_textController.add(trimmedText);
211212
}
212213
}
213214
}
@@ -241,8 +242,9 @@ class A2uiAgentConnector {
241242
if (part is DataPart) {
242243
_processA2uiMessages(part.data);
243244
} else if (part is TextPart) {
244-
if (!_textController.isClosed) {
245-
_textController.add(part.text);
245+
final String trimmedText = part.text.trim();
246+
if (trimmedText.isNotEmpty && !_textController.isClosed) {
247+
_textController.add(trimmedText);
246248
}
247249
}
248250
}

0 commit comments

Comments
 (0)