From ca3ca9bba3c6f7a2202e589b0277a75c4c565bbb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 25 Dec 2025 02:50:54 +0000 Subject: [PATCH 1/6] Initial plan From 5c01863dda2ef0697f42de282e5f8389b9022cd4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 25 Dec 2025 02:55:16 +0000 Subject: [PATCH 2/6] 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 <13498329+OhYee@users.noreply.github.com> --- agentrun/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agentrun/__init__.py b/agentrun/__init__.py index 383ef37..14c5605 100644 --- a/agentrun/__init__.py +++ b/agentrun/__init__.py @@ -303,7 +303,7 @@ def __getattr__(name: str): return getattr(server, name) except ImportError as e: # 检查是否是缺少可选依赖导致的错误 - if "fastapi" in str(e) or "uvicorn" in str(e): + if "fastapi" in str(e) or "uvicorn" in str(e) or "ag_ui" in str(e): raise ImportError( f"'{name}' requires the 'server' optional dependencies. " "Install with: pip install agentrun-sdk[server]\n" From 54bb23b1e3db9788d38867c3857cbfcb640d24e4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 25 Dec 2025 03:14:04 +0000 Subject: [PATCH 3/6] 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 <13498329+OhYee@users.noreply.github.com> --- agentrun/__init__.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/agentrun/__init__.py b/agentrun/__init__.py index 14c5605..bacf117 100644 --- a/agentrun/__init__.py +++ b/agentrun/__init__.py @@ -289,6 +289,14 @@ "MergeOptions", } +# 可选依赖包映射:导入错误的包名 -> 安装使用的包名 +# Optional dependency mapping: import error package name -> installation package name +_OPTIONAL_PACKAGES = [ + ("fastapi", "agentrun-sdk[server]"), + ("uvicorn", "agentrun-sdk[server]"), + ("ag_ui", "agentrun-sdk[server]"), +] + def __getattr__(name: str): """延迟加载 server 模块的导出,避免可选依赖导致导入失败 @@ -303,12 +311,14 @@ def __getattr__(name: str): return getattr(server, name) except ImportError as e: # 检查是否是缺少可选依赖导致的错误 - if "fastapi" in str(e) or "uvicorn" in str(e) or "ag_ui" in str(e): - raise ImportError( - f"'{name}' requires the 'server' optional dependencies. " - "Install with: pip install agentrun-sdk[server]\n" - f"Original error: {e}" - ) from e + error_str = str(e) + for package_name, install_name in _OPTIONAL_PACKAGES: + if package_name in error_str: + raise ImportError( + f"'{name}' requires optional dependencies. " + f"Install with: pip install {install_name}\n" + f"Original error: {e}" + ) from e # 其他导入错误继续抛出 raise From 7575061d6519ab0f9601feeb54b16bcb33efea32 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 25 Dec 2025 03:15:49 +0000 Subject: [PATCH 4/6] 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 <13498329+OhYee@users.noreply.github.com> --- agentrun/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/agentrun/__init__.py b/agentrun/__init__.py index bacf117..166a4b3 100644 --- a/agentrun/__init__.py +++ b/agentrun/__init__.py @@ -291,6 +291,7 @@ # 可选依赖包映射:导入错误的包名 -> 安装使用的包名 # Optional dependency mapping: import error package name -> installation package name +# 所有 server 相关的可选依赖 / All server-related optional dependencies _OPTIONAL_PACKAGES = [ ("fastapi", "agentrun-sdk[server]"), ("uvicorn", "agentrun-sdk[server]"), @@ -315,7 +316,7 @@ def __getattr__(name: str): for package_name, install_name in _OPTIONAL_PACKAGES: if package_name in error_str: raise ImportError( - f"'{name}' requires optional dependencies. " + f"'{name}' requires the 'server' optional dependencies. " f"Install with: pip install {install_name}\n" f"Original error: {e}" ) from e From 9bbe850f4764c3c7124093eb680c4077ac783318 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 25 Dec 2025 03:16:54 +0000 Subject: [PATCH 5/6] 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 <13498329+OhYee@users.noreply.github.com> --- agentrun/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/agentrun/__init__.py b/agentrun/__init__.py index 166a4b3..40cee0d 100644 --- a/agentrun/__init__.py +++ b/agentrun/__init__.py @@ -292,10 +292,11 @@ # 可选依赖包映射:导入错误的包名 -> 安装使用的包名 # Optional dependency mapping: import error package name -> installation package name # 所有 server 相关的可选依赖 / All server-related optional dependencies +_SERVER_INSTALL_CMD = "agentrun-sdk[server]" _OPTIONAL_PACKAGES = [ - ("fastapi", "agentrun-sdk[server]"), - ("uvicorn", "agentrun-sdk[server]"), - ("ag_ui", "agentrun-sdk[server]"), + ("fastapi", _SERVER_INSTALL_CMD), + ("uvicorn", _SERVER_INSTALL_CMD), + ("ag_ui", _SERVER_INSTALL_CMD), ] From 3e95d9112f11e73f5d6b5acda6a39aea362ae734 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 25 Dec 2025 03:25:31 +0000 Subject: [PATCH 6/6] 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 <13498329+OhYee@users.noreply.github.com> --- agentrun/__init__.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/agentrun/__init__.py b/agentrun/__init__.py index 40cee0d..bf93517 100644 --- a/agentrun/__init__.py +++ b/agentrun/__init__.py @@ -289,15 +289,12 @@ "MergeOptions", } -# 可选依赖包映射:导入错误的包名 -> 安装使用的包名 -# Optional dependency mapping: import error package name -> installation package name -# 所有 server 相关的可选依赖 / All server-related optional dependencies -_SERVER_INSTALL_CMD = "agentrun-sdk[server]" -_OPTIONAL_PACKAGES = [ - ("fastapi", _SERVER_INSTALL_CMD), - ("uvicorn", _SERVER_INSTALL_CMD), - ("ag_ui", _SERVER_INSTALL_CMD), -] +# 可选依赖包映射:安装命令 -> 导入错误的包名列表 +# Optional dependency mapping: installation command -> list of import error package names +# 将使用相同安装命令的包合并到一起 / Group packages with the same installation command +_OPTIONAL_PACKAGES = { + "agentrun-sdk[server]": ["fastapi", "uvicorn", "ag_ui"], +} def __getattr__(name: str): @@ -314,13 +311,14 @@ def __getattr__(name: str): except ImportError as e: # 检查是否是缺少可选依赖导致的错误 error_str = str(e) - for package_name, install_name in _OPTIONAL_PACKAGES: - if package_name in error_str: - raise ImportError( - f"'{name}' requires the 'server' optional dependencies. " - f"Install with: pip install {install_name}\n" - f"Original error: {e}" - ) from e + for install_cmd, package_names in _OPTIONAL_PACKAGES.items(): + for package_name in package_names: + if package_name in error_str: + raise ImportError( + f"'{name}' requires the 'server' optional dependencies. " + f"Install with: pip install {install_cmd}\n" + f"Original error: {e}" + ) from e # 其他导入错误继续抛出 raise