Skip to content

Commit 72991cd

Browse files
authored
refactor: update agentscope[full] to 1.0.11 (#92)
1 parent fe4b7f5 commit 72991cd

File tree

15 files changed

+312
-190
lines changed

15 files changed

+312
-190
lines changed

alias/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ The backend server will:
288288
- Create the initial superuser account (if not exists)
289289
- Start on `http://localhost:8000` (or the port specified in `.env`)
290290

291-
Verify the server is running by visiting `http://localhost:8000/api/v1/monitor/health`.
291+
Verify the server is running by visiting `http://localhost:8000/api/v1/health`.
292292

293293
#### Start the Frontend
294294

@@ -361,7 +361,7 @@ Once both servers are running:
361361
- **Frontend UI**: Open `http://localhost:5173` in your browser
362362
- **Backend API**: Available at `http://localhost:8000`
363363
- **API Documentation**: Available at `http://localhost:8000/docs` (Swagger UI) or `http://localhost:8000/api/v1/openapi.json` (OpenAPI JSON)
364-
- **Health Check**: `http://localhost:8000/api/v1/monitor/health`
364+
- **Health Check**: `http://localhost:8000/api/v1/health`
365365

366366
#### Default Login Credentials
367367

alias/README_ZH.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ python -m uvicorn alias.server.main:app --host 0.0.0.0 --port 8000 --reload
289289
- 创建初始超级用户账户(如果不存在)
290290
-`http://localhost:8000` 启动(或 `.env` 中指定的端口)
291291

292-
通过访问 `http://localhost:8000/api/v1/monitor/health` 来验证服务器是否正在运行。
292+
通过访问 `http://localhost:8000/api/v1/health` 来验证服务器是否正在运行。
293293

294294
#### 启动前端
295295

@@ -362,7 +362,7 @@ bash script/start_memory_service.sh
362362
- **前端 UI**:在浏览器中打开 `http://localhost:5173`
363363
- **后端 API**:可在 `http://localhost:8000` 访问
364364
- **API 文档**:可在 `http://localhost:8000/docs` (Swagger UI) 或 `http://localhost:8000/api/v1/openapi.json` (OpenAPI JSON) 访问
365-
- **健康检查**`http://localhost:8000/api/v1/monitor/health`
365+
- **健康检查**`http://localhost:8000/api/v1/health`
366366

367367
#### 默认登录凭据
368368

alias/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies = [
3131
"elasticsearch>=9.0.0",
3232
"jinja2>=3.1.6",
3333
"bcrypt==4.0.1",
34-
"agentscope[full]==1.0.7",
34+
"agentscope[full]==1.0.11",
3535
"tenacity>=8.5.0",
3636
"apscheduler>=3.11.0",
3737
"chardet>=5.2.0",
@@ -42,7 +42,7 @@ dependencies = [
4242
"alembic>=1.16.1",
4343
"openpyxl>=3.1.5",
4444
"sentry-sdk[fastapi]===2.30.0",
45-
"agentscope-runtime==0.2.0",
45+
"agentscope-runtime>=1.0.0",
4646
"aiosqlite>=0.21.0",
4747
"asyncpg>=0.30.0",
4848
"itsdangerous>=2.2.0"

alias/src/alias/agent/agents/_alias_agent_base.py

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from agentscope.model import ChatModelBase
1212
from agentscope.formatter import FormatterBase
1313
from agentscope.memory import MemoryBase, LongTermMemoryBase
14-
from agentscope.message import Msg, TextBlock, ToolUseBlock, ToolResultBlock
14+
from agentscope.message import Msg, ToolUseBlock, ToolResultBlock
1515

1616
from alias.agent.tools import AliasToolkit
1717
from alias.agent.utils.constants import DEFAULT_PLANNER_NAME
@@ -23,24 +23,6 @@
2323
from alias.agent.utils.constants import MODEL_MAX_RETRIES
2424

2525

26-
def alias_agent_post_reply_hook(
27-
self: "AliasAgentBase",
28-
kwargs: dict[str, Any], # pylint: disable=unused-argument
29-
output: Any,
30-
):
31-
"""
32-
This is a monkey patch to ensure that when the agent is interrupted in
33-
a tool call, the control returns to user
34-
"""
35-
if (
36-
self.tool_call_interrupt_return
37-
and isinstance(output, Msg)
38-
and output.metadata
39-
and output.metadata.get("is_interrupted", False)
40-
):
41-
raise asyncio.CancelledError()
42-
43-
4426
class AliasAgentBase(ReActAgent):
4527
def __init__(
4628
self,
@@ -53,7 +35,6 @@ def __init__(
5335
state_saving_dir: Optional[str] = None,
5436
sys_prompt: Optional[str] = None,
5537
max_iters: int = 10,
56-
tool_call_interrupt_return: bool = True,
5738
long_term_memory: Optional[LongTermMemoryBase] = None,
5839
long_term_memory_mode: Literal[
5940
"agent_control",
@@ -77,22 +58,24 @@ def __init__(
7758
self.message_sending_mapping = {}
7859
self.state_saving_dir = state_saving_dir
7960
self.agent_stop_function_names = [self.finish_function_name]
80-
self.tool_call_interrupt_return = tool_call_interrupt_return
8161

82-
# interrupted if the
83-
self.register_instance_hook(
84-
"post_reply",
85-
"alias_agent_post_reply_hook",
86-
alias_agent_post_reply_hook,
87-
)
8862
# for message output to backend
8963
self.register_instance_hook(
9064
"post_print",
9165
"alias_post_print_hook",
9266
alias_post_print_hook,
9367
)
9468

95-
async def _reasoning(self):
69+
# register finish_function_name
70+
if self.finish_function_name not in self.toolkit.tools:
71+
self.toolkit.register_tool_function(
72+
getattr(self, self.finish_function_name),
73+
)
74+
75+
async def _reasoning(
76+
self,
77+
tool_choice: Literal["auto", "none", "required"] | None = None,
78+
):
9679
"""Override _reasoning to add retry logic."""
9780

9881
# Call the parent class's _reasoning method directly to
@@ -109,7 +92,8 @@ async def call_parent_reasoning():
10992
if hasattr(original_method, "__wrapped__"):
11093
# This is the wrapped version, get the original
11194
original_method = original_method.__wrapped__
112-
return await original_method(self)
95+
96+
return await original_method(self, tool_choice=tool_choice)
11397

11498
for i in range(MODEL_MAX_RETRIES - 1):
11599
try:
@@ -132,21 +116,18 @@ async def call_parent_reasoning():
132116
# final attempt
133117
await call_parent_reasoning()
134118

135-
async def _acting(self, tool_call: ToolUseBlock) -> Msg | None:
136-
"""Perform the acting process.
137-
138-
TODO: (part 2)
139-
this is just a monkey patch for AS when not support interruption
140-
during tool call; can be remove when AS framework updated
119+
async def _acting(self, tool_call: ToolUseBlock) -> dict | None:
120+
"""Perform the acting process, and return the structured output if
121+
it's generated and verified in the finish function call.
141122
142123
Args:
143124
tool_call (`ToolUseBlock`):
144125
The tool use block to be executed.
145126
146127
Returns:
147-
`Union[Msg, None]`:
148-
Return a message to the user if the `_finish_function` is
149-
called, otherwise return `None`.
128+
`Union[dict, None]`:
129+
Return the structured output if it's verified in the finish
130+
function call.
150131
"""
151132

152133
tool_res_msg = Msg(
@@ -165,7 +146,6 @@ async def _acting(self, tool_call: ToolUseBlock) -> Msg | None:
165146
# Execute the tool call
166147
tool_res = await self.toolkit.call_tool_function(tool_call)
167148

168-
response_msg = None
169149
# Async generator handling
170150
async for chunk in tool_res:
171151
# Turn into a tool result block
@@ -191,28 +171,26 @@ async def _acting(self, tool_call: ToolUseBlock) -> Msg | None:
191171
tool_call["name"] != self.finish_function_name
192172
or (
193173
tool_call["name"] == self.finish_function_name
174+
and chunk.metadata
194175
and not chunk.metadata.get("success")
195176
)
196177
):
197178
await self.print(tool_res_msg, chunk.is_last)
198179

199180
# Return message if generate_response is called successfully
200-
if tool_call[
201-
"name"
202-
] in self.agent_stop_function_names and chunk.metadata.get(
203-
"success",
204-
True,
181+
if (
182+
tool_call["name"] in self.agent_stop_function_names
183+
and chunk.metadata
184+
and chunk.metadata.get(
185+
"success",
186+
True,
187+
)
205188
):
206-
response_msg = chunk.metadata.get("response_msg")
189+
return chunk.metadata.get("structured_output")
207190
elif chunk.is_interrupted:
208-
# TODO: monkey patch happens here
209-
response_msg = tool_res_msg
210-
if response_msg.metadata is None:
211-
response_msg.metadata = {"is_interrupted": True}
212-
else:
213-
response_msg.metadata["is_interrupted"] = True
191+
raise asyncio.CancelledError
214192

215-
return response_msg
193+
return None
216194
finally:
217195
# Record the tool result message in the memory
218196
await self.memory.add(tool_res_msg)
@@ -228,16 +206,16 @@ async def handle_interrupt(
228206
"""
229207
response_msg = Msg(
230208
self.name,
231-
content=[
232-
TextBlock(
233-
type="text",
234-
text="I got interrupted by the user. "
235-
"Pivot to handle the user's new request.",
236-
),
237-
],
238-
role="assistant",
239-
metadata={},
209+
"I noticed that you have interrupted me. What can I "
210+
"do for you?",
211+
"assistant",
212+
metadata={
213+
# Expose this field to indicate the interruption
214+
"_is_interrupted": True,
215+
},
240216
)
217+
218+
await self.print(response_msg, True)
241219
await self.memory.add(response_msg)
242220

243221
# update and save agent states

0 commit comments

Comments
 (0)