-
Notifications
You must be signed in to change notification settings - Fork 113
feat: refactor AgentApp to inherit from FastAPI with interrupt support #416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: refactor AgentApp to inherit from FastAPI with interrupt support #416
Conversation
…nd fix deployment
9263a4d to
1dbc76b
Compare
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request is a significant and well-executed refactoring of the AgentApp class. By making AgentApp inherit directly from FastAPI, the architecture is now much cleaner, more aligned with FastAPI best practices, and more extensible. The introduction of mixins for routing, tasks, and interrupts is a great design choice that improves modularity. The new interrupt support with configurable backends is a powerful feature. I've identified a couple of areas for improvement in the new streaming and error handling logic, but overall this is an excellent contribution.
There was a problem hiding this 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 replaces the legacy factory-based AgentApp design with a FastAPI-inheriting AgentApp, introduces unified routing and task/interrupt infrastructure, and adds support for distributed/manual interruption of running agent sessions.
Changes:
- Refactored
AgentAppto inherit directly fromFastAPI, wiring it to aRunner, new routing mixins (UnifiedRoutingMixin,CustomEndpointMixin,TaskEngineMixin), and updated deployment paths. - Introduced an interrupt subsystem (
InterruptMixinplus Redis and local backends) to track task states and allow cross-process/manual interruption of streaming executions. - Deprecated the older Celery/
FastAPIAppFactorypath, updatedLocalDeployManagerto work withAgentAppinstances directly, and removed now-obsolete tests aroundFastAPIAppFactory.
Reviewed changes
Copilot reviewed 14 out of 15 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unit/test_service_utils.py | Removes unit tests that targeted the now-deprecated FastAPIAppFactory and its deployment modes, aligning the test suite with the new AgentApp-centric architecture. |
| src/agentscope_runtime/engine/deployers/utils/service_utils/routing/unified_routing_mixin.py | Adds UnifiedRoutingMixin to centralize custom/task routing registration, task status routes, and discovery-oriented routing metadata, used by AgentApp for unified route management. |
| src/agentscope_runtime/engine/deployers/utils/service_utils/routing/task_engine_mixin.py | Introduces TaskEngineMixin that encapsulates Celery/in-memory task execution, task registration, and status querying, decoupling task processing from the old CeleryMixin/factory pattern. |
| src/agentscope_runtime/engine/deployers/utils/service_utils/routing/custom_endpoint_mixin.py | Provides CustomEndpointMixin to register custom synchronous and streaming endpoints with correct FastAPI signature handling and SSE normalization, used by UnifiedRoutingMixin/AgentApp. |
| src/agentscope_runtime/engine/deployers/utils/service_utils/interrupt/base_backend.py | Defines the abstract BaseInterruptBackend, TaskState, and InterruptSignal enums that formalize the contract for distributed interrupt backends. |
| src/agentscope_runtime/engine/deployers/utils/service_utils/interrupt/redis_backend.py | Implements RedisInterruptBackend using Redis pub/sub and key-value storage to persist task state and broadcast interrupt signals across processes. |
| src/agentscope_runtime/engine/deployers/utils/service_utils/interrupt/local_backend.py | Adds LocalInterruptBackend, an in-memory backend suitable for single-process deployments, mirroring the Redis backend’s API for task state and signal handling. |
| src/agentscope_runtime/engine/deployers/utils/service_utils/interrupt/interrupt_mixin.py | Introduces InterruptMixin with run_and_stream, stop_chat, and lifecycle wiring so AgentApp can wrap streaming generators with distributed interrupt and state tracking semantics. |
| src/agentscope_runtime/engine/deployers/utils/service_utils/interrupt/init.py | Exposes the new interrupt types (TaskState, BaseInterruptBackend, RedisInterruptBackend, InterruptMixin, LocalInterruptBackend) as a public interrupt API surface. |
| src/agentscope_runtime/engine/deployers/utils/service_utils/fastapi_factory.py | Marks FastAPIAppFactory as deprecated with guidance to construct AgentApp directly, reflecting the migration away from the factory-based app construction route. |
| src/agentscope_runtime/engine/deployers/local_deployer.py | Adjusts local daemon-thread deployment to run an existing AgentApp instance directly under uvicorn, removing the dependency on FastAPIAppFactory.create_app while still supporting detached-process packaging. |
| src/agentscope_runtime/engine/app/celery_mixin.py | Deprecates the legacy CeleryMixin in favor of the new TaskEngineMixin, signalling that task processing responsibilities have moved to the routing utils layer. |
| src/agentscope_runtime/engine/app/base_app.py | Removes the obsolete BaseApp that previously combined CeleryMixin with a pseudo-FastAPI task routing API, as its responsibilities are now handled by AgentApp plus the new mixins. |
| src/agentscope_runtime/engine/app/agent_app.py | Fully refactors AgentApp to inherit from FastAPI, integrate unified routing and interrupt support, override openapi to inject protocol schemas, manage lifecycle via FastAPI lifespan hooks, and provide run/deploy methods compatible with the new architecture. |
src/agentscope_runtime/engine/deployers/utils/service_utils/interrupt/interrupt_mixin.py
Outdated
Show resolved
Hide resolved
src/agentscope_runtime/engine/deployers/utils/service_utils/interrupt/interrupt_mixin.py
Show resolved
Hide resolved
src/agentscope_runtime/engine/deployers/utils/service_utils/interrupt/interrupt_mixin.py
Show resolved
Hide resolved
src/agentscope_runtime/engine/deployers/utils/service_utils/interrupt/interrupt_mixin.py
Show resolved
Hide resolved
38123c9 to
bcf8274
Compare
Description
This PR refactors the
AgentAppclass by migrating it from a factory-based FastAPI setup to a direct inheritance model. This completes the transition previously initiated underFastAgentApp.Key Changes:
AgentApphas been removed. The new implementation (formerlyFastAgentApp) is now the primaryAgentApp.try...finallyblocks. Users can load the state at the beginning of the execution and ensure the current state is saved within thefinallyblock when an interruption occurs.Related Issue:
Fixes #402
Fixes #432
Type of Change
Component(s) Affected
Checklist
Additional Notes
AgentApparchitecture.