Skip to content

Commit 1e59225

Browse files
committed
chore(core.agent): add missing type annotations
1 parent ffc535f commit 1e59225

File tree

7 files changed

+39
-29
lines changed

7 files changed

+39
-29
lines changed

astrbot/core/agent/handoff.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Generic
1+
from typing import Any, Generic
22
from .tool import FunctionTool
33
from .agent import Agent
44
from .run_context import TContext
@@ -8,8 +8,11 @@ class HandoffTool(FunctionTool, Generic[TContext]):
88
"""Handoff tool for delegating tasks to another agent."""
99

1010
def __init__(
11-
self, agent: Agent[TContext], parameters: dict | None = None, **kwargs
12-
):
11+
self,
12+
agent: Agent[TContext],
13+
parameters: dict | None = None,
14+
**kwargs: Any, # noqa: ANN401
15+
) -> None:
1316
self.agent = agent
1417
super().__init__(
1518
name=f"transfer_to_{agent.name}",

astrbot/core/agent/hooks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88

99
@dataclass
1010
class BaseAgentRunHooks(Generic[TContext]):
11-
async def on_agent_begin(self, run_context: ContextWrapper[TContext]): ...
11+
async def on_agent_begin(self, run_context: ContextWrapper[TContext]) -> None: ...
1212
async def on_tool_start(
1313
self,
1414
run_context: ContextWrapper[TContext],
1515
tool: FunctionTool,
1616
tool_args: dict | None,
17-
): ...
17+
) -> None: ...
1818
async def on_tool_end(
1919
self,
2020
run_context: ContextWrapper[TContext],
2121
tool: FunctionTool,
2222
tool_args: dict | None,
2323
tool_result: mcp.types.CallToolResult | None,
24-
): ...
24+
) -> None: ...
2525
async def on_agent_done(
2626
self, run_context: ContextWrapper[TContext], llm_response: LLMResponse
27-
): ...
27+
) -> None: ...

astrbot/core/agent/mcp_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async def _quick_test_mcp_connection(config: dict) -> tuple[bool, str]:
9393

9494

9595
class MCPClient:
96-
def __init__(self):
96+
def __init__(self) -> None:
9797
# Initialize session and client objects
9898
self.session: mcp.ClientSession | None = None
9999
self.exit_stack = AsyncExitStack()
@@ -104,7 +104,7 @@ def __init__(self):
104104
self.server_errlogs: list[str] = []
105105
self.running_event = asyncio.Event()
106106

107-
async def connect_to_server(self, mcp_server_config: dict, name: str):
107+
async def connect_to_server(self, mcp_server_config: dict, name: str) -> None:
108108
"""连接到 MCP 服务器
109109
110110
如果 `url` 参数存在:
@@ -117,7 +117,7 @@ async def connect_to_server(self, mcp_server_config: dict, name: str):
117117
"""
118118
cfg = _prepare_config(mcp_server_config.copy())
119119

120-
def logging_callback(msg: str):
120+
def logging_callback(msg: str) -> None:
121121
# 处理 MCP 服务的错误日志
122122
print(f"MCP Server {name} Error: {msg}")
123123
self.server_errlogs.append(msg)
@@ -187,7 +187,7 @@ def logging_callback(msg: str):
187187
**cfg,
188188
)
189189

190-
def callback(msg: str):
190+
def callback(msg: str) -> None:
191191
# 处理 MCP 服务的错误日志
192192
self.server_errlogs.append(msg)
193193

@@ -217,7 +217,7 @@ async def list_tools_and_save(self) -> mcp.ListToolsResult:
217217
self.tools = response.tools
218218
return response
219219

220-
async def cleanup(self):
220+
async def cleanup(self) -> None:
221221
"""Clean up resources"""
222222
await self.exit_stack.aclose()
223223
self.running_event.set() # Set the running event to indicate cleanup is done

astrbot/core/agent/runners/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async def reset(
2626
run_context: ContextWrapper[TContext],
2727
tool_executor: BaseFunctionToolExecutor[TContext],
2828
agent_hooks: BaseAgentRunHooks[TContext],
29-
**kwargs: T.Any,
29+
**kwargs: T.Any, # noqa: ANN401
3030
) -> None:
3131
"""
3232
Reset the agent to its initial state.

astrbot/core/agent/runners/tool_loop_agent_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async def _iter_llm_responses(self) -> T.AsyncGenerator[LLMResponse, None]:
6969
yield await self.provider.text_chat(**self.req.__dict__)
7070

7171
@override
72-
async def step(self):
72+
async def step(self) -> T.AsyncGenerator[AgentResponse, None]:
7373
"""
7474
Process a single step of the agent.
7575
This method should return the result of the step.

astrbot/core/agent/tool.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from deprecated import deprecated
33
from typing import Literal, Any
44

5+
from collections.abc import Iterator
6+
57
from collections.abc import Awaitable, Callable
68
from .mcp_client import MCPClient
79

@@ -32,7 +34,7 @@ class FunctionTool:
3234
mcp_client: MCPClient | None = None
3335
"""MCP 客户端,当 origin 为 mcp 时有效"""
3436

35-
def __repr__(self):
37+
def __repr__(self) -> str:
3638
return f"FuncTool(name={self.name}, parameters={self.parameters}, description={self.description}, active={self.active}, origin={self.origin})"
3739

3840
def __dict__(self) -> dict[str, Any]:
@@ -53,14 +55,14 @@ class ToolSet:
5355
This class provides methods to add, remove, and retrieve tools, as well as
5456
convert the tools to different API formats (OpenAI, Anthropic, Google GenAI)."""
5557

56-
def __init__(self, tools: list[FunctionTool] | None = None):
58+
def __init__(self, tools: list[FunctionTool] | None = None) -> None:
5759
self.tools: list[FunctionTool] = tools or []
5860

5961
def empty(self) -> bool:
6062
"""Check if the tool set is empty."""
6163
return len(self.tools) == 0
6264

63-
def add_tool(self, tool: FunctionTool):
65+
def add_tool(self, tool: FunctionTool) -> None:
6466
"""Add a tool to the set."""
6567
# 检查是否已存在同名工具
6668
for i, existing_tool in enumerate(self.tools):
@@ -69,7 +71,7 @@ def add_tool(self, tool: FunctionTool):
6971
return
7072
self.tools.append(tool)
7173

72-
def remove_tool(self, name: str):
74+
def remove_tool(self, name: str) -> None:
7375
"""Remove a tool by its name."""
7476
self.tools = [tool for tool in self.tools if tool.name != name]
7577

@@ -87,7 +89,7 @@ def add_func(
8789
func_args: list,
8890
desc: str,
8991
handler: Callable[..., Awaitable[Any]],
90-
):
92+
) -> None:
9193
"""Add a function tool to the set."""
9294
params = {
9395
"type": "object", # hard-coded here
@@ -107,7 +109,7 @@ def add_func(
107109
self.add_tool(_func)
108110

109111
@deprecated(reason="Use remove_tool() instead", version="4.0.0")
110-
def remove_func(self, name: str):
112+
def remove_func(self, name: str) -> None:
111113
"""Remove a function tool by its name."""
112114
self.remove_tool(name)
113115

@@ -238,32 +240,34 @@ def convert_schema(schema: dict) -> dict:
238240
return declarations
239241

240242
@deprecated(reason="Use openai_schema() instead", version="4.0.0")
241-
def get_func_desc_openai_style(self, omit_empty_parameter_field: bool = False):
243+
def get_func_desc_openai_style(
244+
self, omit_empty_parameter_field: bool = False
245+
) -> list[dict]:
242246
return self.openai_schema(omit_empty_parameter_field)
243247

244248
@deprecated(reason="Use anthropic_schema() instead", version="4.0.0")
245-
def get_func_desc_anthropic_style(self):
249+
def get_func_desc_anthropic_style(self) -> list[dict]:
246250
return self.anthropic_schema()
247251

248252
@deprecated(reason="Use google_schema() instead", version="4.0.0")
249-
def get_func_desc_google_genai_style(self):
253+
def get_func_desc_google_genai_style(self) -> dict:
250254
return self.google_schema()
251255

252256
def names(self) -> list[str]:
253257
"""获取所有工具的名称列表"""
254258
return [tool.name for tool in self.tools]
255259

256-
def __len__(self):
260+
def __len__(self) -> int:
257261
return len(self.tools)
258262

259-
def __bool__(self):
263+
def __bool__(self) -> bool:
260264
return len(self.tools) > 0
261265

262-
def __iter__(self):
266+
def __iter__(self) -> Iterator:
263267
return iter(self.tools)
264268

265-
def __repr__(self):
269+
def __repr__(self) -> str:
266270
return f"ToolSet(tools={self.tools})"
267271

268-
def __str__(self):
272+
def __str__(self) -> str:
269273
return f"ToolSet(tools={self.tools})"

astrbot/core/agent/tool_executor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
class BaseFunctionToolExecutor(Generic[TContext]):
1010
@classmethod
1111
async def execute(
12-
cls, tool: FunctionTool, run_context: ContextWrapper[TContext], **tool_args
12+
cls,
13+
tool: FunctionTool,
14+
run_context: ContextWrapper[TContext],
15+
**tool_args: Any, # noqa: ANN401
1316
) -> AsyncGenerator[Any | mcp.types.CallToolResult, None]: ...

0 commit comments

Comments
 (0)