Skip to content

Commit 4e63b75

Browse files
committed
Address review comments
1 parent dbeb495 commit 4e63b75

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

agent_sdks/python/src/a2ui/core/parser/streaming.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def __init__(self, catalog: "A2uiCatalog" = None):
106106
self._yielded_contents: Dict[Any, str] = {}
107107

108108
self._root_ids: Dict[str, str] = {} # The root component IDs mapped per surface
109+
self._default_root_id: Optional[str] = None # Base default root ID for the protocol
109110
self._unbound_root_id: Optional[str] = (
110111
None # Temporary holding variable for when root arrives before surfaceId
111112
)
@@ -153,8 +154,13 @@ def surface_id(self, value: Optional[str]):
153154
@property
154155
def root_id(self) -> Optional[str]:
155156
if self._surface_id:
156-
return self._root_ids.get(self._surface_id)
157-
return self._unbound_root_id
157+
return self._root_ids.get(self._surface_id, self._default_root_id)
158+
# Return unbound root ID if explicitly sniffed, otherwise use protocol default
159+
return (
160+
self._unbound_root_id
161+
if self._unbound_root_id is not None
162+
else self._default_root_id
163+
)
158164

159165
@root_id.setter
160166
def root_id(self, value: Optional[str]):

agent_sdks/python/src/a2ui/core/parser/streaming_v08.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,22 @@ def _data_model_msg_type(self) -> str:
6464

6565
def _sniff_metadata(self):
6666
"""Sniffs for v0.8 metadata in the json_buffer."""
67-
matches = re.findall(r'"surfaceId"\s*:\s*"([^"]+)"', self._json_buffer)
68-
if matches:
69-
self.surface_id = matches[-1]
7067

71-
root_matches = re.findall(r'"root"\s*:\s*"([^"]+)"', self._json_buffer)
72-
if root_matches:
73-
self.root_id = root_matches[-1]
68+
def get_latest_value(key: str) -> Optional[str]:
69+
idx = len(self._json_buffer)
70+
while True:
71+
idx = self._json_buffer.rfind(f'"{key}"', 0, idx)
72+
if idx == -1:
73+
return None
74+
match = re.match(rf'"{key}"\s*:\s*"([^"]+)"', self._json_buffer[idx:])
75+
if match:
76+
return match.group(1)
77+
78+
self.surface_id = get_latest_value('surfaceId')
79+
80+
parsed_root = get_latest_value('root')
81+
if parsed_root is not None:
82+
self.root_id = parsed_root
7483

7584
if f'"{MSG_TYPE_BEGIN_RENDERING}":' in self._json_buffer:
7685
self.add_msg_type(MSG_TYPE_BEGIN_RENDERING)

agent_sdks/python/src/a2ui/core/parser/streaming_v09.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class A2uiStreamParserV09(A2uiStreamParser):
2828
def __init__(self, catalog=None):
2929
super().__init__(catalog=catalog)
3030
# v0.9 default root is "root"
31-
self.root_id = DEFAULT_ROOT_ID
31+
self._default_root_id = DEFAULT_ROOT_ID
3232

3333
@property
3434
def _placeholder_component(self) -> Dict[str, Any]:
@@ -56,13 +56,22 @@ def is_protocol_msg(self, obj: Dict[str, Any]) -> bool:
5656

5757
def _sniff_metadata(self):
5858
"""Sniffs for v0.9 metadata in the json_buffer."""
59-
matches = re.findall(r'"surfaceId"\s*:\s*"([^"]+)"', self._json_buffer)
60-
if matches:
61-
self.surface_id = matches[-1]
6259

63-
root_matches = re.findall(r'"root"\s*:\s*"([^"]+)"', self._json_buffer)
64-
if root_matches:
65-
self.root_id = root_matches[-1]
60+
def get_latest_value(key: str) -> Optional[str]:
61+
idx = len(self._json_buffer)
62+
while True:
63+
idx = self._json_buffer.rfind(f'"{key}"', 0, idx)
64+
if idx == -1:
65+
return None
66+
match = re.match(rf'"{key}"\s*:\s*"([^"]+)"', self._json_buffer[idx:])
67+
if match:
68+
return match.group(1)
69+
70+
self.surface_id = get_latest_value('surfaceId')
71+
72+
parsed_root = get_latest_value('root')
73+
if parsed_root is not None:
74+
self.root_id = parsed_root
6675

6776
if f'"{MSG_TYPE_CREATE_SURFACE}":' in self._json_buffer:
6877
self.add_msg_type(MSG_TYPE_CREATE_SURFACE)

0 commit comments

Comments
 (0)