Skip to content

Commit b3ff8fa

Browse files
CopilotOhYee
andauthored
refactor(core): use generic package mapping for optional dependency checks (#25)
* Initial plan * fix(core): add 'ag_ui' to optional dependency error check Add "ag_ui" to the error message checking logic in __getattr__ to properly handle import errors when ag-ui-protocol is not installed. This ensures users get helpful error messages when trying to use AGUIProtocolHandler or other AG-UI related components without the server optional dependencies installed. Addresses feedback from PR #23 review comment. Co-authored-by: OhYee <[email protected]> * refactor(core): use generic package mapping for optional dependency checks Refactor the import error handling to use a more maintainable and scalable approach with a package mapping structure instead of hardcoded string checks. Changes: - Added _OPTIONAL_PACKAGES list with (import_name, install_name) tuples - Refactored __getattr__ to iterate through the package mapping - Now easier to add new optional dependencies in the future - All three server optional dependencies (fastapi, uvicorn, ag_ui) registered This makes the codebase more maintainable when adding new optional dependencies in the future. Addresses feedback from @OhYee on PR #23. Co-authored-by: OhYee <[email protected]> * fix(core): restore specificity in error message and add clarifying comment Address code review feedback: - Restored "server" context in error message for better clarity - Added comment clarifying that all entries are for server-related dependencies - Maintains generic approach while preserving helpful context for users This ensures users get clear, specific error messages while keeping the code maintainable for future extensions. Co-authored-by: OhYee <[email protected]> * refactor(core): use constant for server install command to reduce duplication Extract the repeated install command string into a constant _SERVER_INSTALL_CMD to make the code more maintainable and easier to update in the future. This addresses code review feedback about reducing duplication in the package mapping structure. Co-authored-by: OhYee <[email protected]> * refactor(core): group packages by install command in _OPTIONAL_PACKAGES Refactor _OPTIONAL_PACKAGES from a list of tuples to a dictionary that groups packages by their installation command. This provides better organization when multiple packages share the same installation command. Changes: - Changed from list of (package, install_cmd) tuples to dict of {install_cmd: [packages]} - Removed _SERVER_INSTALL_CMD constant (now a dict key) - Updated iteration logic to handle nested structure - All three server packages (fastapi, uvicorn, ag_ui) grouped together Benefits: - Clearer relationship: install command -> packages it provides - More compact when packages share the same install command - Easier to see all packages for a given installation - Scalable for future optional dependency groups Addresses feedback from @OhYee about merging packages with the same command. Co-authored-by: OhYee <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: OhYee <[email protected]>
1 parent 9aa51c8 commit b3ff8fa

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

agentrun/__init__.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@
289289
"MergeOptions",
290290
}
291291

292+
# 可选依赖包映射:安装命令 -> 导入错误的包名列表
293+
# Optional dependency mapping: installation command -> list of import error package names
294+
# 将使用相同安装命令的包合并到一起 / Group packages with the same installation command
295+
_OPTIONAL_PACKAGES = {
296+
"agentrun-sdk[server]": ["fastapi", "uvicorn", "ag_ui"],
297+
}
298+
292299

293300
def __getattr__(name: str):
294301
"""延迟加载 server 模块的导出,避免可选依赖导致导入失败
@@ -303,12 +310,15 @@ def __getattr__(name: str):
303310
return getattr(server, name)
304311
except ImportError as e:
305312
# 检查是否是缺少可选依赖导致的错误
306-
if "fastapi" in str(e) or "uvicorn" in str(e):
307-
raise ImportError(
308-
f"'{name}' requires the 'server' optional dependencies. "
309-
"Install with: pip install agentrun-sdk[server]\n"
310-
f"Original error: {e}"
311-
) from e
313+
error_str = str(e)
314+
for install_cmd, package_names in _OPTIONAL_PACKAGES.items():
315+
for package_name in package_names:
316+
if package_name in error_str:
317+
raise ImportError(
318+
f"'{name}' requires the 'server' optional dependencies. "
319+
f"Install with: pip install {install_cmd}\n"
320+
f"Original error: {e}"
321+
) from e
312322
# 其他导入错误继续抛出
313323
raise
314324

0 commit comments

Comments
 (0)