You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+27-14Lines changed: 27 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,6 +77,7 @@
77
77
78
78
## 🆕 NEWS
79
79
80
+
***[2026-02]** A major architectural refactor of `AgentApp`. By adopting direct inheritance from `FastAPI` and deprecating the previous factory pattern, `AgentApp` now offers seamless integration with the full FastAPI ecosystem, significantly boosting extensibility. Furthermore, we've introduced a **Distributed Interrupt Service**, enabling manual task preemption during agent execution and allowing developers to customize state persistence and recovery logic flexibly.
80
81
***[2026-01]** Added **asynchronous sandbox** implementations (`BaseSandboxAsync`, `GuiSandboxAsync`, `BrowserSandboxAsync`, `FilesystemSandboxAsync`, `MobileSandboxAsync`) enabling non-blocking, concurrent tool execution in async program. Improved `run_ipython_cell` and `run_shell_command` methods with enhanced **concurrency and parallel execution** capabilities for more efficient sandbox operations.
81
82
***[2025-12]** We have released **AgentScope Runtime v1.0**, introducing a unified “Agent as API” white-box development experience, with enhanced multi-agent collaboration, state persistence, and cross-framework integration. This release also streamlines abstractions and modules to ensure consistency between development and production environments. Please refer to the **[CHANGELOG](https://runtime.agentscope.io/en/CHANGELOG.html)** for full update details and migration guide.
82
83
@@ -141,14 +142,15 @@ pip install -e .
141
142
142
143
This example demonstrates how to create an agent API server using agentscope `ReActAgent` and `AgentApp`. To run a minimal `AgentScope` Agent with AgentScope Runtime, you generally need to implement:
143
144
144
-
1.**`@agent_app.init`** – Initialize services/resources at startup
145
+
1.**`Define lifespan`** – Use `contextlib.asynccontextmanager` to manage resource initialization (e.g., state services) at startup and cleanup on exit.
145
146
2.**`@agent_app.query(framework="agentscope")`** – Core logic for handling requests, **must use**`stream_printing_messages` to `yield msg, last` for streaming output
146
-
3.**`@agent_app.shutdown`** – Clean up services/resources on exit
147
147
148
148
149
149
```python
150
150
import os
151
+
from contextlib import asynccontextmanager
151
152
153
+
from fastapi import FastAPI
152
154
from agentscope.agent import ReActAgent
153
155
from agentscope.model import DashScopeChatModel
154
156
from agentscope.formatter import DashScopeChatFormatter
@@ -160,23 +162,32 @@ from agentscope.session import RedisSession
160
162
from agentscope_runtime.engine import AgentApp
161
163
from agentscope_runtime.engine.schemas.agent_schemas import AgentRequest
162
164
163
-
agent_app = AgentApp(
164
-
app_name="Friday",
165
-
app_description="A helpful assistant",
166
-
)
167
-
168
-
169
-
@agent_app.init
170
-
asyncdefinit_func(self):
165
+
# 1. Define lifespan manager
166
+
@asynccontextmanager
167
+
asyncdeflifespan(app: FastAPI):
168
+
"""Manage resources during service startup and shutdown"""
Copy file name to clipboardExpand all lines: cookbook/en/CHANGELOG.md
+82-24Lines changed: 82 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,9 @@
1
1
# CHANGELOG
2
2
3
3
## v1.1.0
4
+
AgentScope Runtime v1.1.0 **simplifies persistence and session continuity** by removing Runtime-side custom Memory/Session service abstractions and **standardizing on the Agent framework’s native persistence modules**. This reduces mental overhead, avoids duplicated concepts, and ensures the persistence behavior is consistent with the underlying agent framework.
4
5
5
-
AgentScope Runtime v1.1.0 focuses on**simplifying persistence and session continuity** by removing Runtime-side custom Memory/Session service abstractions and **standardizing on the Agent framework’s native persistence modules**. This reduces mental overhead, avoids duplicated concepts, and ensures the persistence behavior is consistent with the underlying agent framework.
6
+
Additionally, this version introduces a**major refactor of the `AgentApp` core architecture**: switching to direct inheritance from `FastAPI`, introducing **standard lifespan management**, and adding **task interruption** and **concurrency conflict control** for distributed scenarios.
6
7
7
8
**Background & Necessity of the Changes**
8
9
@@ -19,11 +20,28 @@ In v1.0, Runtime provided custom **Session History** and **Long-term Memory** se
19
20
20
21
To address this, v1.1.0 **deprecates and removes** these Runtime-side services/adapters, and recommends using the **Agent framework’s own persistence modules** (e.g., `JSONSession`, built-in memory implementations) directly in the `AgentApp` lifecycle.
21
22
23
+
**Furthermore, v1.1.0 refactors the core architecture of `AgentApp`, shifting from a "factory creation pattern" to "direct inheritance from `FastAPI`".** This change stems from the following considerations:
24
+
25
+
1.**Addressing limitations of the factory pattern**
26
+
In previous versions, `AgentApp` held a FastAPI instance internally through a factory class. This "black box" approach made it difficult for developers to leverage native FastAPI features (e.g., complex middleware, custom route decorators, and dependency injection), and custom lifecycle hooks like `@app.init` deviated from standard web development practices.
27
+
28
+
2.**Embracing native FastAPI ecosystem and extensibility**
29
+
By **directly inheriting from the `FastAPI` class**, `AgentApp` is now a standard FastAPI application. This grants developers full control and seamless integration with FastAPI's community plugins. The class-based architecture also allows us to elegantly introduce advanced features like **Task Interruption** and **State Race Condition Control** via `Mixin` classes, making `AgentApp` a core component that is both standard-compliant and deeply optimized for Agent-specific scenarios.
30
+
31
+
### Added
32
+
33
+
-**Distributed Task Interruption**: Introduced `InterruptMixin` with backend support (Local/Redis), allowing manual interruption via custom interfaces and supporting interrupt signal broadcasting in distributed clusters.
34
+
-**Distributed Race Condition Control**: Introduced atomic state-machine checks (Compare-and-Swap) during task startup to effectively prevent duplicate concurrent execution of the same Session ID in distributed environments.
35
+
-**Native FastAPI Extensibility**: Since `AgentApp` now inherits from `FastAPI`, developers can use native methods like `@app.get` and `app.add_middleware` with full compatibility.
36
+
22
37
### Changed
23
38
24
-
- Recommended persistence pattern:
39
+
-**Recommended persistence pattern**:
25
40
- Use the agent framework’s **Memory** modules directly (e.g., `InMemoryMemory`, Redis-backed memory if provided by the framework).
26
41
- Use the agent framework’s **Session** modules (e.g., `JSONSession`) to load/save agent session state during `query`.
42
+
-**Architectural Refactor**: `AgentApp` has shifted from a **factory class pattern** to **direct inheritance from `FastAPI`**.
43
+
-**Lifecycle Unification**: Unified the lifecycle management of internal framework resources and user-defined resources.
44
+
27
45
28
46
### Breaking Changes
29
47
@@ -35,17 +53,25 @@ To address this, v1.1.0 **deprecates and removes** these Runtime-side services/a
must be migrated to the Agent framework’s built-in persistence approach.
56
+
2.**Removal of the Factory Pattern**
57
+
-`FastAPIAppFactory` is deprecated. Users should now instantiate `AgentApp` objects directly.
58
+
3.**Deprecation of Custom Decorator Hooks**
59
+
- The `@app.init` and `@app.shutdown` decorators are deprecated and marked for removal.
60
+
-**Migration Advice**: Please use the standard FastAPI `lifespan` asynchronous context manager (see the example in the Migration Guide below).
38
61
39
62
#### Migration Guide (v1.0 → v1.1)
40
63
41
-
##### Recommended Pattern (Use Agent framework modules for persistence)
64
+
##### Recommended Pattern (Unified Lifecycle, Interruption Handling, and Native Persistence)
42
65
43
-
Use `JSONSession` or other submodule to persist/load the agent’s session state, and use `InMemoryMemory()` (or other framework-provided memory) directly in AgentScope:
66
+
In v1.1.0, we recommend using the **lifespan asynchronous context manager** instead of the old decorators to manage resources. Additionally, catch `asyncio.CancelledError` within the `query` logic to respond to interrupt signals, and use the Agent framework’s native **Session/Memory** modules for state persistence:
44
67
45
68
```python
46
69
# -*- coding: utf-8 -*-
70
+
import asyncio
47
71
import os
72
+
from contextlib import asynccontextmanager
48
73
74
+
from fastapi import FastAPI
49
75
from agentscope.agent import ReActAgent
50
76
from agentscope.model import DashScopeChatModel
51
77
from agentscope.formatter import DashScopeChatFormatter
@@ -57,23 +83,27 @@ from agentscope.session import JSONSession
57
83
from agentscope_runtime.engine.app import AgentApp
58
84
from agentscope_runtime.engine.schemas.agent_schemas import AgentRequest
0 commit comments