-
Notifications
You must be signed in to change notification settings - Fork 149
fix: consuming v0.9 A2UI JSONL stream #868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ class _A2uiParserStream { | |
| late final StreamController<GenerationEvent> _controller; | ||
| StreamSubscription<String>? _subscription; | ||
| String _buffer = ''; | ||
| bool _lastWasJson = false; | ||
|
|
||
| Stream<GenerationEvent> get stream => _controller.stream; | ||
|
|
||
|
|
@@ -130,22 +131,31 @@ class _A2uiParserStream { | |
| } | ||
|
|
||
| if (firstPotentialStart == -1) { | ||
| // No potential JSON start. Emit all. | ||
| // No potential JSON start. | ||
| if (_buffer.isNotEmpty) { | ||
| if (_lastWasJson && _buffer.trim().isEmpty) { | ||
| // Whitespace-only after a JSON message: treat as JSONL separator. | ||
| // Hold in buffer until more data arrives or stream ends. | ||
| break; | ||
| } | ||
|
Comment on lines
+136
to
+140
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The newly introduced logic for handling JSONL separators in the To remediate this, implement a limit on the maximum amount of whitespace that can be held in the buffer as a separator. If the whitespace-only buffer exceeds a reasonable threshold (e.g., 4KB), it should be cleared or emitted as a if (_lastWasJson && _buffer.trim().isEmpty) {
if (_buffer.length > 4096) {
_emitText(_buffer);
_buffer = '';
} else {
break;
}
}
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, the non-JSON content would be interpreted as text, so I think that it's pretty reasonable for an infinite input stream to cause an OOM eventually. We can rely on other parts of the system to fail to prevent infinite input streams. |
||
| _emitText(_buffer); | ||
| _buffer = ''; | ||
| } | ||
| break; | ||
| } else { | ||
| // Found a potential start at `firstPotentialStart`. | ||
| // Emit text BEFORE it. | ||
| if (firstPotentialStart > 0) { | ||
| _emitText(_buffer.substring(0, firstPotentialStart)); | ||
| final String prefix = _buffer.substring(0, firstPotentialStart); | ||
| if (_lastWasJson && prefix.trim().isEmpty) { | ||
| // Skip whitespace-only prefix after a JSON message | ||
| // (JSONL separator). | ||
| _buffer = _buffer.substring(firstPotentialStart); | ||
| continue; | ||
| } | ||
| _emitText(prefix); | ||
| _buffer = _buffer.substring(firstPotentialStart); | ||
| } | ||
| // Now buffer starts with potential JSON. | ||
| // Since we already tried to parse and failed (if we are here), | ||
| // we must wait for more data. | ||
| // Buffer starts with potential JSON. Wait for more data. | ||
| break; | ||
| } | ||
| } | ||
|
|
@@ -158,6 +168,7 @@ class _A2uiParserStream { | |
| } | ||
|
|
||
| void _emitText(String text) { | ||
| _lastWasJson = false; | ||
| // Clean up protocol tags that might leak into text stream | ||
| final String cleanText = text | ||
| .replaceAll('<a2ui_message>', '') | ||
|
|
@@ -172,22 +183,28 @@ class _A2uiParserStream { | |
| if (json is Map<String, Object?>) { | ||
| try { | ||
| _controller.add(A2uiMessageEvent(A2uiMessage.fromJson(json))); | ||
| _lastWasJson = true; | ||
| } on A2uiValidationException catch (e) { | ||
| _controller.addError(e); | ||
| _lastWasJson = false; | ||
| } catch (_) { | ||
| // Failed to parse A2UI message structure (e.g. invalid type | ||
| // discriminator) | ||
| _controller.add(TextEvent(jsonEncode(json))); | ||
| _lastWasJson = false; | ||
| } | ||
| } else if (json is List) { | ||
| for (final Object? item in json) { | ||
| if (item is Map<String, Object?>) { | ||
| try { | ||
| _controller.add(A2uiMessageEvent(A2uiMessage.fromJson(item))); | ||
| _lastWasJson = true; | ||
| } on A2uiValidationException catch (e) { | ||
| _controller.addError(e); | ||
| _lastWasJson = false; | ||
| } catch (_) { | ||
| _controller.add(TextEvent(jsonEncode(item))); | ||
| _lastWasJson = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add comment explaining what means true and what means false for this flag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey!
trueif the last event was anA2uiMessageEvent,falseotherwise. So the parser can tell how to treat whitespace.Would you prefer something more explicit? Maybe
_wasLastEventA2UI?