Skip to content

Conversation

@OhYee
Copy link
Member

@OhYee OhYee commented Dec 25, 2025

Thank you for creating a pull request to contribute to Serverless Devs agentrun-sdk-python code! Before you open the request please answer the following questions to help it be more easily integrated. Please check the boxes "[ ]" with "[x]" when done too.
Please select one of the PR types below to complete


Fix bugs

Bug detail

The specific manifestation of the bug or the associated issue.

Pull request tasks

  • Add test cases for the changes
  • Passed the CI test

Update docs

Reason for update

Why do you need to update your documentation?

Pull request tasks

  • Update Chinese documentation
  • Update English documentation

Add contributor

Contributed content

  • Code
  • Document

Content detail

if content_type == 'code' || content_type == 'document':
    please tell us `PR url`,like: https://github.com/Serverless-Devs/agentrun-sdk-python/pull/1
else:
    please describe your contribution in detail

Others

Reason for update

Why do you need to update your documentation?

fix(agui_protocol): optimize import statements and event handling logic

- Move imports to local scope to avoid unnecessary dependencies
- Simplify conditional expressions and method calls
- Improve type hinting for better code clarity

修复凭据模型中的访问密钥参数拼写错误

优化 AGUI 协议处理器的导入语句和事件处理逻辑:
- 将部分导入移至函数内部以减少不必要的依赖
- 简化条件表达式与方法调用
- 改进类型提示以增强代码可读性

Change-Id: I1f4b89fa0a0ceeed09bcf1e6bb03370dfd1547de
Signed-off-by: OhYee <oyohyee@oyohyee.com>
the runstartedevent and runfinishedevent classes are now imported directly from ag_ui.core to ensure proper event handling in the sse stream.

these events are essential for signaling the start and completion of runs within the agui protocol, improving reliability and maintainability of the streaming mechanism.

修复了 agui 协议中运行事件类的导入问题,确保 SSE 流能够正确处理运行开始和结束事件。此举增强了流机制的可靠性和可维护性。

Change-Id: Ied78843c9533c640a4706b379c8bd9109acc3b0b
Signed-off-by: OhYee <oyohyee@oyohyee.com>
…tional dependencies

This change introduces a lazy loading mechanism for the server module to avoid import errors when optional dependencies are not installed. The server-related exports are now imported on-demand through __getattr__, with clear error messages when required optional dependencies are missing.

The implementation includes:
- TYPE_CHECKING import for type hints during development
- Delayed imports wrapped in TYPE_CHECKING blocks
- __getattr__ function to handle runtime imports
- Clear error messages directing users to install optional dependencies
- Server exports defined in _SERVER_EXPORTS set for tracking

This resolves issues where the package would fail to import when server optional dependencies were not available.

refactor(core): 实现服务器模块的延迟加载以处理可选依赖

此更改引入了服务器模块的延迟加载机制,以避免在未安装可选依赖项时出现导入错误。现在通过 __getattr__ 按需导入服务器相关导出,并在缺少必需的可选依赖项时提供清晰的错误消息。

实现包括:
- 用于开发期间类型提示的 TYPE_CHECKING 导入
- 包装在 TYPE_CHECKING 块中的延迟导入
- 用于处理运行时导入的 __getattr__ 函数
- 指向用户安装可选依赖项的清晰错误消息
- 定义在 _SERVER_EXPORTS 集合中用于跟踪的服务器导出

这解决了在服务器可选依赖项不可用时包导入失败的问题。

Change-Id: I9d8f7dd8e5c6d34dfd0619714ead5aea22105293
Signed-off-by: OhYee <oyohyee@oyohyee.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the import structure to make ag_ui an optional dependency, preventing import failures when the library is not installed. The changes implement lazy loading for the server module and fix a spelling error in a function parameter name.

Key Changes:

  • Converted ag_ui imports to lazy/local imports in agui_protocol.py using TYPE_CHECKING and runtime imports
  • Implemented __getattr__ mechanism in __init__.py for lazy loading of server module exports
  • Fixed typo: renamed parameter from access_key_secred to access_key_secret

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
agentrun/server/agui_protocol.py Refactored ag_ui imports to use TYPE_CHECKING for type hints and local imports within methods to avoid import errors when ag_ui is not installed
agentrun/init.py Implemented lazy loading for server module using __getattr__ and TYPE_CHECKING to handle optional dependencies gracefully
agentrun/credential/model.py Fixed spelling error in parameter name from access_key_secred to access_key_secret
agentrun/model/model.py Added blank line for improved code formatting in ProxyConfigAIGuardrailConfig class

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 202 to +256
######## Others ########
"Status",
"ResourceNotExistError",
"ResourceAlreadyExistError",
"Config",
Copy link

Copilot AI Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Status export has been removed from __all__, but the import statement from agentrun.utils.model import Status still exists on line 99. This creates an inconsistency where Status is imported but not exported, making it unavailable to users who import from this module.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Comment on lines 306 to 311
if "fastapi" in str(e) or "uvicorn" 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
Copy link

Copilot AI Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message checking logic is incomplete. It only checks for "fastapi" or "uvicorn" in the error string, but the ag_ui library imports are also optional dependencies. If ag_ui is not installed and a user tries to use AGUIProtocolHandler, they won't get a helpful error message. Consider adding "ag_ui" to the check on line 306.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Copy link
Contributor

Copilot AI commented Dec 25, 2025

@OhYee I've opened a new pull request, #24, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Contributor

Copilot AI commented Dec 25, 2025

@OhYee I've opened a new pull request, #25, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI added a commit that referenced this pull request Dec 25, 2025
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>
Copilot AI added a commit that referenced this pull request Dec 25, 2025
…hecks

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>
…hecks (#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 <13498329+OhYee@users.noreply.github.com>

* 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>

* 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>

* 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>

* 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>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: OhYee <13498329+OhYee@users.noreply.github.com>
@OhYee OhYee merged commit 613723b into main Dec 26, 2025
2 checks passed
@OhYee OhYee deleted the fix-agui branch December 26, 2025 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants