MCPcopy
hub / github.com/AstrBotDevs/AstrBot / run_agent

Function run_agent

astrbot/core/astr_agent_run_util.py:116–340  ·  view source on GitHub ↗
(
    agent_runner: AgentRunner,
    max_step: int = 30,
    show_tool_use: bool = True,
    show_tool_call_result: bool = False,
    stream_to_general: bool = False,
    show_reasoning: bool = False,
    buffer_intermediate_messages: bool = False,
)

Source from the content-addressed store, hash-verified

114
115
116async def run_agent(
117 agent_runner: AgentRunner,
118 max_step: int = 30,
119 show_tool_use: bool = True,
120 show_tool_call_result: bool = False,
121 stream_to_general: bool = False,
122 show_reasoning: bool = False,
123 buffer_intermediate_messages: bool = False,
124) -> AsyncGenerator[MessageChain | None, None]:
125 step_idx = 0
126 astr_event = agent_runner.run_context.context.event
127 tool_name_by_call_id: dict[str, str] = {}
128 buffered_llm_chains: list[MessageChain] = []
129 can_buffer_llm_result = _should_buffer_llm_result(
130 buffer_intermediate_messages,
131 stream_to_general,
132 agent_runner,
133 )
134 while step_idx < max_step + 1:
135 step_idx += 1
136
137 if step_idx == max_step + 1:
138 logger.warning(
139 f"Agent reached max steps ({max_step}), forcing a final response."
140 )
141 if not agent_runner.done():
142 # 拔掉所有工具
143 if agent_runner.req:
144 agent_runner.req.func_tool = None
145 # 注入提示词
146 agent_runner.run_context.messages.append(
147 Message(
148 role="user",
149 content="工具调用次数已达到上限,请停止使用工具,并根据已经收集到的信息,对你的任务和发现进行总结,然后直接回复用户。",
150 )
151 )
152
153 stop_watcher = asyncio.create_task(
154 _watch_agent_stop_signal(agent_runner, astr_event),
155 )
156 try:
157 async for resp in agent_runner.step():
158 if _should_stop_agent(astr_event):
159 agent_runner.request_stop()
160
161 if resp.type == "aborted":
162 if can_buffer_llm_result:
163 merged_chain = _merge_buffered_llm_chains(buffered_llm_chains)
164 if merged_chain:
165 astr_event.set_result(
166 MessageEventResult(
167 chain=merged_chain.chain,
168 result_content_type=ResultContentType.LLM_RESULT,
169 ),
170 )
171 yield merged_chain
172 astr_event.clear_result()
173 if not stop_watcher.done():

Callers 3

run_live_agentFunction · 0.85
_run_agent_feederFunction · 0.85
processMethod · 0.85

Calls 15

MessageClass · 0.90
MessageEventResultClass · 0.90
MessageChainClass · 0.90
JsonClass · 0.90
LLMResponseClass · 0.90
_watch_agent_stop_signalFunction · 0.85
_should_stop_agentFunction · 0.85
_extract_chain_json_dataFunction · 0.85

Tested by

no test coverage detected