Skip to content

Commit f866340

Browse files
committed
Mechanical: Apply formatting to Python files
1 parent f0b1855 commit f866340

26 files changed

Lines changed: 2537 additions & 1723 deletions

File tree

agent_sdks/cpp/scripts/generate_embedded_schemas.py

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,72 @@
1515
import os
1616
import sys
1717

18+
1819
def main():
1920
if len(sys.argv) < 3:
2021
print("Usage: python generate_embedded_schemas.py <repo_root> <output_file>")
2122
sys.exit(1)
22-
23+
2324
repo_root = sys.argv[1]
2425
output_file = sys.argv[2]
25-
26+
2627
v08_s2c = os.path.join(repo_root, "specification/v0_8/json/server_to_client.json")
2728
v09_s2c = os.path.join(repo_root, "specification/v0_9/json/server_to_client.json")
2829
v09_common = os.path.join(repo_root, "specification/v0_9/json/common_types.json")
29-
v08_catalog = os.path.join(repo_root, "specification/v0_8/json/standard_catalog_definition.json")
30+
v08_catalog = os.path.join(
31+
repo_root, "specification/v0_8/json/standard_catalog_definition.json"
32+
)
3033
v09_catalog = os.path.join(repo_root, "specification/v0_9/json/basic_catalog.json")
31-
34+
3235
def read_file(path):
33-
with open(path, 'r') as f:
36+
with open(path, "r") as f:
3437
return f.read()
35-
38+
3639
v08_s2c_content = read_file(v08_s2c)
3740
v09_s2c_content = read_file(v09_s2c)
3841
v09_common_content = read_file(v09_common)
3942
v08_catalog_content = read_file(v08_catalog)
4043
v09_catalog_content = read_file(v09_catalog)
41-
44+
4245
# Ensure output directory exists
4346
os.makedirs(os.path.dirname(output_file), exist_ok=True)
44-
45-
with open(output_file, 'w') as f:
46-
f.write('// Generated by generate_embedded_schemas.py. DO NOT EDIT.\n\n')
47-
f.write('namespace a2ui {\n')
48-
f.write('namespace internal {\n\n')
49-
50-
f.write('const char* SERVER_TO_CLIENT_SCHEMA_V08 = R"schema(' + v08_s2c_content + ')schema";\n\n')
51-
f.write('const char* SERVER_TO_CLIENT_SCHEMA_V09 = R"schema(' + v09_s2c_content + ')schema";\n\n')
52-
f.write('const char* COMMON_TYPES_SCHEMA_V09 = R"schema(' + v09_common_content + ')schema";\n\n')
53-
f.write('const char* BASIC_CATALOG_V08 = R"schema(' + v08_catalog_content + ')schema";\n\n')
54-
f.write('const char* BASIC_CATALOG_V09 = R"schema(' + v09_catalog_content + ')schema";\n\n')
55-
56-
57-
f.write('} // namespace internal\n')
58-
f.write('} // namespace a2ui\n')
59-
47+
48+
with open(output_file, "w") as f:
49+
f.write("// Generated by generate_embedded_schemas.py. DO NOT EDIT.\n\n")
50+
f.write("namespace a2ui {\n")
51+
f.write("namespace internal {\n\n")
52+
53+
f.write(
54+
'const char* SERVER_TO_CLIENT_SCHEMA_V08 = R"schema('
55+
+ v08_s2c_content
56+
+ ')schema";\n\n'
57+
)
58+
f.write(
59+
'const char* SERVER_TO_CLIENT_SCHEMA_V09 = R"schema('
60+
+ v09_s2c_content
61+
+ ')schema";\n\n'
62+
)
63+
f.write(
64+
'const char* COMMON_TYPES_SCHEMA_V09 = R"schema('
65+
+ v09_common_content
66+
+ ')schema";\n\n'
67+
)
68+
f.write(
69+
'const char* BASIC_CATALOG_V08 = R"schema('
70+
+ v08_catalog_content
71+
+ ')schema";\n\n'
72+
)
73+
f.write(
74+
'const char* BASIC_CATALOG_V09 = R"schema('
75+
+ v09_catalog_content
76+
+ ')schema";\n\n'
77+
)
78+
79+
f.write("} // namespace internal\n")
80+
f.write("} // namespace a2ui\n")
81+
6082
print(f"Generated {output_file}")
6183

84+
6285
if __name__ == "__main__":
6386
main()

agent_sdks/python/pack_specs_hook.py

Lines changed: 98 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -20,103 +20,105 @@
2020

2121

2222
def load_module(project_root, rel_path, filename, module_name):
23-
"""Loads a module directly from its path in src/."""
24-
path = os.path.join(project_root, "src", rel_path.replace(".", os.sep), filename)
25-
if not os.path.exists(path):
26-
raise RuntimeError(f"Could not find module at {path}")
27-
28-
# Add src to sys.path so absolute imports work
29-
src_path = os.path.abspath(os.path.join(project_root, "src"))
30-
if src_path not in sys.path:
31-
sys.path.insert(0, src_path)
32-
33-
spec = importlib.util.spec_from_file_location(module_name, path)
34-
if spec and spec.loader:
35-
module = importlib.util.module_from_spec(spec)
36-
# Set the package context to allow relative imports if any
37-
module.__package__ = rel_path
38-
sys.modules[module_name] = module
39-
spec.loader.exec_module(module)
40-
return module
41-
raise RuntimeError(f"Could not load module from {path}")
23+
"""Loads a module directly from its path in src/."""
24+
path = os.path.join(project_root, "src", rel_path.replace(".", os.sep), filename)
25+
if not os.path.exists(path):
26+
raise RuntimeError(f"Could not find module at {path}")
27+
28+
# Add src to sys.path so absolute imports work
29+
src_path = os.path.abspath(os.path.join(project_root, "src"))
30+
if src_path not in sys.path:
31+
sys.path.insert(0, src_path)
32+
33+
spec = importlib.util.spec_from_file_location(module_name, path)
34+
if spec and spec.loader:
35+
module = importlib.util.module_from_spec(spec)
36+
# Set the package context to allow relative imports if any
37+
module.__package__ = rel_path
38+
sys.modules[module_name] = module
39+
spec.loader.exec_module(module)
40+
return module
41+
raise RuntimeError(f"Could not load module from {path}")
4242

4343

4444
class PackSpecsBuildHook(BuildHookInterface):
4545

46-
def initialize(self, version, build_data):
47-
project_root = self.root
48-
49-
# Load constants and utils dynamically from src/
50-
schema_path = "a2ui.schema"
51-
a2ui_constants = load_module(
52-
project_root, schema_path, "constants.py", "_constants_load"
53-
)
54-
a2ui_utils = load_module(project_root, schema_path, "utils.py", "_utils_load")
55-
56-
basic_catalog_constants = load_module(
57-
project_root,
58-
"a2ui.basic_catalog",
59-
"constants.py",
60-
"_basic_catalog_constants_load",
61-
)
62-
63-
spec_version_map = a2ui_constants.SPEC_VERSION_MAP
64-
a2ui_asset_package = a2ui_constants.A2UI_ASSET_PACKAGE
65-
specification_dir = a2ui_constants.SPECIFICATION_DIR
66-
67-
# Dynamically find repo root by looking for specification_dir
68-
repo_root = a2ui_utils.find_repo_root(project_root)
69-
if not repo_root:
70-
# Check for PKG-INFO which implies a packaged state (sdist).
71-
# If PKG-INFO is present, trust the bundled assets.
72-
if os.path.exists(os.path.join(project_root, "PKG-INFO")):
73-
print("Repository root not found, but PKG-INFO present (sdist). Skipping copy.")
74-
return
75-
76-
raise RuntimeError(
77-
f"Could not find repository root (looked for '{specification_dir}'"
78-
" directory)."
79-
)
80-
81-
# Target directory: src/a2ui/assets
82-
target_base = os.path.join(
83-
project_root, "src", a2ui_asset_package.replace(".", os.sep)
84-
)
85-
86-
self._pack_schemas(repo_root, spec_version_map, target_base)
87-
self._pack_basic_catalogs(
88-
repo_root, basic_catalog_constants.BASIC_CATALOG_PATHS, target_base
89-
)
90-
91-
def _pack_schemas(self, repo_root, spec_map, target_base):
92-
for ver, schema_map in spec_map.items():
93-
target_dir = os.path.join(target_base, ver)
94-
os.makedirs(target_dir, exist_ok=True)
95-
96-
for _schema_key, source_rel_path in schema_map.items():
97-
self._copy_schema(repo_root, source_rel_path, target_dir)
98-
99-
def _pack_basic_catalogs(self, repo_root, catalog_paths, target_base):
100-
for ver, path_map in catalog_paths.items():
101-
target_dir = os.path.join(target_base, ver)
102-
os.makedirs(target_dir, exist_ok=True)
103-
104-
for _key, source_rel_path in path_map.items():
105-
self._copy_schema(repo_root, source_rel_path, target_dir)
106-
107-
def _copy_schema(self, repo_root, source_rel_path, target_dir):
108-
source_path = os.path.join(repo_root, source_rel_path)
109-
110-
if not os.path.exists(source_path):
111-
print(
112-
f"WARNING: Source schema file not found at {source_path}. Build"
113-
" might produce incomplete wheel if not running from monorepo"
114-
" root."
115-
)
116-
return
117-
118-
filename = os.path.basename(source_path)
119-
dst_file = os.path.join(target_dir, filename)
120-
121-
print(f"Copying {source_path} -> {dst_file}")
122-
shutil.copy2(source_path, dst_file)
46+
def initialize(self, version, build_data):
47+
project_root = self.root
48+
49+
# Load constants and utils dynamically from src/
50+
schema_path = "a2ui.schema"
51+
a2ui_constants = load_module(
52+
project_root, schema_path, "constants.py", "_constants_load"
53+
)
54+
a2ui_utils = load_module(project_root, schema_path, "utils.py", "_utils_load")
55+
56+
basic_catalog_constants = load_module(
57+
project_root,
58+
"a2ui.basic_catalog",
59+
"constants.py",
60+
"_basic_catalog_constants_load",
61+
)
62+
63+
spec_version_map = a2ui_constants.SPEC_VERSION_MAP
64+
a2ui_asset_package = a2ui_constants.A2UI_ASSET_PACKAGE
65+
specification_dir = a2ui_constants.SPECIFICATION_DIR
66+
67+
# Dynamically find repo root by looking for specification_dir
68+
repo_root = a2ui_utils.find_repo_root(project_root)
69+
if not repo_root:
70+
# Check for PKG-INFO which implies a packaged state (sdist).
71+
# If PKG-INFO is present, trust the bundled assets.
72+
if os.path.exists(os.path.join(project_root, "PKG-INFO")):
73+
print(
74+
"Repository root not found, but PKG-INFO present (sdist). Skipping copy."
75+
)
76+
return
77+
78+
raise RuntimeError(
79+
f"Could not find repository root (looked for '{specification_dir}'"
80+
" directory)."
81+
)
82+
83+
# Target directory: src/a2ui/assets
84+
target_base = os.path.join(
85+
project_root, "src", a2ui_asset_package.replace(".", os.sep)
86+
)
87+
88+
self._pack_schemas(repo_root, spec_version_map, target_base)
89+
self._pack_basic_catalogs(
90+
repo_root, basic_catalog_constants.BASIC_CATALOG_PATHS, target_base
91+
)
92+
93+
def _pack_schemas(self, repo_root, spec_map, target_base):
94+
for ver, schema_map in spec_map.items():
95+
target_dir = os.path.join(target_base, ver)
96+
os.makedirs(target_dir, exist_ok=True)
97+
98+
for _schema_key, source_rel_path in schema_map.items():
99+
self._copy_schema(repo_root, source_rel_path, target_dir)
100+
101+
def _pack_basic_catalogs(self, repo_root, catalog_paths, target_base):
102+
for ver, path_map in catalog_paths.items():
103+
target_dir = os.path.join(target_base, ver)
104+
os.makedirs(target_dir, exist_ok=True)
105+
106+
for _key, source_rel_path in path_map.items():
107+
self._copy_schema(repo_root, source_rel_path, target_dir)
108+
109+
def _copy_schema(self, repo_root, source_rel_path, target_dir):
110+
source_path = os.path.join(repo_root, source_rel_path)
111+
112+
if not os.path.exists(source_path):
113+
print(
114+
f"WARNING: Source schema file not found at {source_path}. Build"
115+
" might produce incomplete wheel if not running from monorepo"
116+
" root."
117+
)
118+
return
119+
120+
filename = os.path.basename(source_path)
121+
dst_file = os.path.join(target_dir, filename)
122+
123+
print(f"Copying {source_path} -> {dst_file}")
124+
shutil.copy2(source_path, dst_file)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,10 @@ def traverse(obj, parent_key=None):
10461046
if not any(ec["id"] == placeholder_id for ec in extra_components):
10471047
extra_components.append(placeholder_comp)
10481048

1049-
if not valid_children and field in ("children", "explicitList"):
1049+
if not valid_children and field in (
1050+
"children",
1051+
"explicitList",
1052+
):
10501053
# If list is empty, check if it was partial in the buffer
10511054
# (meaning it's a sequence that started but hasn't yielded items yet)
10521055
term = f'"{field}"'

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ def __init__(self, catalog=None):
3333
def _placeholder_component(self) -> Dict[str, Any]:
3434
"""Returns the placeholder component."""
3535
return {
36-
'component': {
37-
'Row': {
38-
'children': {'explicitList': []},
36+
"component": {
37+
"Row": {
38+
"children": {"explicitList": []},
3939
}
4040
}
4141
}
@@ -75,9 +75,9 @@ def get_latest_value(key: str) -> Optional[str]:
7575
if match:
7676
return match.group(1)
7777

78-
self.surface_id = get_latest_value('surfaceId')
78+
self.surface_id = get_latest_value("surfaceId")
7979

80-
parsed_root = get_latest_value('root')
80+
parsed_root = get_latest_value("root")
8181
if parsed_root is not None:
8282
self.root_id = parsed_root
8383

@@ -121,7 +121,7 @@ def _handle_complete_object(
121121
surface_id = val.get(SURFACE_ID_KEY) or surface_id
122122

123123
self.surface_id = surface_id
124-
sid = self.surface_id or 'unknown'
124+
sid = self.surface_id or "unknown"
125125

126126
if MSG_TYPE_DELETE_SURFACE in obj:
127127
if sid in self._yielded_surfaces_set or self._buffered_start_message:
@@ -144,7 +144,7 @@ def _handle_complete_object(
144144
br_val = obj[MSG_TYPE_BEGIN_RENDERING]
145145
if isinstance(br_val, dict):
146146
self.surface_id = br_val.get(SURFACE_ID_KEY, self.surface_id)
147-
self.root_id = br_val.get('root', self.root_id or DEFAULT_ROOT_ID)
147+
self.root_id = br_val.get("root", self.root_id or DEFAULT_ROOT_ID)
148148
self._buffered_start_message = obj
149149

150150
# Yield beginRendering immediately when it completes
@@ -164,10 +164,10 @@ def _handle_complete_object(
164164

165165
if MSG_TYPE_SURFACE_UPDATE in obj:
166166
self.add_msg_type(MSG_TYPE_SURFACE_UPDATE)
167-
components = obj[MSG_TYPE_SURFACE_UPDATE].get('components', [])
167+
components = obj[MSG_TYPE_SURFACE_UPDATE].get("components", [])
168168
for comp in components:
169-
if isinstance(comp, dict) and 'id' in comp:
170-
self._seen_components[comp['id']] = comp
169+
if isinstance(comp, dict) and "id" in comp:
170+
self._seen_components[comp["id"]] = comp
171171
self.yield_reachable(messages, check_root=True, raise_on_orphans=False)
172172
return True
173173

@@ -209,17 +209,17 @@ def _get_active_msg_type_for_components(self) -> Optional[str]:
209209
def _deduplicate_data_model(self, m: Dict[str, Any], strict_integrity: bool) -> bool:
210210
if MSG_TYPE_DATA_MODEL_UPDATE in m:
211211
dm = m[MSG_TYPE_DATA_MODEL_UPDATE]
212-
raw_contents = dm.get('contents', {})
212+
raw_contents = dm.get("contents", {})
213213
contents_dict = {}
214214
if isinstance(raw_contents, list):
215215
for entry in raw_contents:
216-
if isinstance(entry, dict) and 'key' in entry:
217-
key = entry['key']
216+
if isinstance(entry, dict) and "key" in entry:
217+
key = entry["key"]
218218
val = (
219-
entry.get('valueString')
220-
or entry.get('valueNumber')
221-
or entry.get('valueBoolean')
222-
or entry.get('valueMap')
219+
entry.get("valueString")
220+
or entry.get("valueNumber")
221+
or entry.get("valueBoolean")
222+
or entry.get("valueMap")
223223
)
224224
if key and val is not None:
225225
contents_dict[key] = val

0 commit comments

Comments
 (0)