Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 45 additions & 44 deletions astrbot/core/agent/runners/tool_loop_agent_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,15 @@ async def step(self):
if self.stats.time_to_first_token == 0:
self.stats.time_to_first_token = time.time() - self.stats.start_time

if llm_response.reasoning_content:
yield AgentResponse(
type="streaming_delta",
data=AgentResponseData(
chain=MessageChain(type="reasoning").message(
llm_response.reasoning_content,
),
),
)
if llm_response.result_chain:
yield AgentResponse(
type="streaming_delta",
Expand All @@ -729,15 +738,6 @@ async def step(self):
chain=MessageChain().message(llm_response.completion_text),
),
)
if llm_response.reasoning_content:
yield AgentResponse(
type="streaming_delta",
data=AgentResponseData(
chain=MessageChain(type="reasoning").message(
llm_response.reasoning_content,
),
),
)
if self._is_stop_requested():
llm_resp_result = LLMResponse(
role="assistant",
Expand Down Expand Up @@ -791,6 +791,15 @@ async def step(self):
await self._complete_with_assistant_response(llm_resp)

# 返回 LLM 结果
if llm_resp.reasoning_content:
yield AgentResponse(
type="llm_result",
data=AgentResponseData(
chain=MessageChain(type="reasoning").message(
llm_resp.reasoning_content,
),
),
)
if llm_resp.result_chain:
yield AgentResponse(
type="llm_result",
Expand All @@ -803,15 +812,6 @@ async def step(self):
chain=MessageChain().message(llm_resp.completion_text),
),
)
if llm_resp.reasoning_content:
yield AgentResponse(
type="llm_result",
data=AgentResponseData(
chain=MessageChain(type="reasoning").message(
llm_resp.reasoning_content,
),
),
)

# 如果有工具调用,还需处理工具调用
if llm_resp.tools_call_name:
Expand All @@ -821,6 +821,15 @@ async def step(self):
logger.warning(
"skills_like tool re-query returned no tool calls; fallback to assistant response."
)
if llm_resp.reasoning_content:
yield AgentResponse(
type="llm_result",
data=AgentResponseData(
chain=MessageChain(type="reasoning").message(
llm_resp.reasoning_content,
),
),
)
if llm_resp.result_chain:
yield AgentResponse(
type="llm_result",
Expand All @@ -833,15 +842,7 @@ async def step(self):
chain=MessageChain().message(llm_resp.completion_text),
),
)
if llm_resp.reasoning_content:
yield AgentResponse(
type="llm_result",
data=AgentResponseData(
chain=MessageChain(type="reasoning").message(
llm_resp.reasoning_content,
),
),
)

await self._complete_with_assistant_response(llm_resp)
return

Expand Down Expand Up @@ -988,6 +989,7 @@ def _append_tool_call_result(tool_call_id: str, content: str) -> None:
llm_response.tools_call_args,
llm_response.tools_call_ids,
):
tool_result_blocks_start = len(tool_call_result_blocks)
tool_call_streak = self._track_tool_call_streak(func_tool_name)
yield _HandleFunctionToolsResult.from_message_chain(
MessageChain(
Expand Down Expand Up @@ -1201,24 +1203,23 @@ def _append_tool_call_result(tool_call_id: str, content: str) -> None:
),
)

# yield the last tool call result
if tool_call_result_blocks:
last_tcr_content = str(tool_call_result_blocks[-1].content)
yield _HandleFunctionToolsResult.from_message_chain(
MessageChain(
type="tool_call_result",
chain=[
Json(
data={
"id": func_tool_id,
"ts": time.time(),
"result": last_tcr_content,
}
)
],
if len(tool_call_result_blocks) > tool_result_blocks_start:
tool_result_content = str(tool_call_result_blocks[-1].content)
yield _HandleFunctionToolsResult.from_message_chain(
MessageChain(
type="tool_call_result",
chain=[
Json(
data={
"id": func_tool_id,
"ts": time.time(),
"result": tool_result_content,
}
)
],
)
)
)
logger.info(f"Tool `{func_tool_name}` Result: {last_tcr_content}")
logger.info(f"Tool `{func_tool_name}` Result: {tool_result_content}")

# 处理函数调用响应
if tool_call_result_blocks:
Expand Down
6 changes: 6 additions & 0 deletions astrbot/core/astr_agent_run_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ async def run_agent(
)
await astr_event.send(chain)
continue
elif resp.type == "llm_result":
chain = resp.data["chain"]
if chain.type == "reasoning":
# For non-streaming mode, we handle reasoning in astrbot/core/astr_agent_hooks.py.
# For streaming mode, we yield content immediately when received a reasoning chunk but not in here, see below.
continue

if stream_to_general and resp.type == "streaming_delta":
continue
Expand Down
2 changes: 2 additions & 0 deletions astrbot/core/provider/sources/openai_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,8 @@ async def _query_stream(
reasoning = self._extract_reasoning_content(chunk)
_y = False
llm_response.id = chunk.id
llm_response.reasoning_content = ""
llm_response.completion_text = ""
if reasoning:
llm_response.reasoning_content = reasoning
_y = True
Expand Down
33 changes: 15 additions & 18 deletions astrbot/core/star/filter/platform_adapter_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class PlatformAdapterType(enum.Flag):
AIOCQHTTP = enum.auto()
QQOFFICIAL = enum.auto()
QQOFFICIAL_WEBHOOK = enum.auto()
TELEGRAM = enum.auto()
WECOM = enum.auto()
WECOM_AI_BOT = enum.auto()
Expand All @@ -23,29 +24,16 @@ class PlatformAdapterType(enum.Flag):
MISSKEY = enum.auto()
LINE = enum.auto()
MATRIX = enum.auto()
ALL = (
AIOCQHTTP
| QQOFFICIAL
| TELEGRAM
| WECOM
| WECOM_AI_BOT
| LARK
| DINGTALK
| DISCORD
| SLACK
| KOOK
| VOCECHAT
| WEIXIN_OFFICIAL_ACCOUNT
| SATORI
| MISSKEY
| LINE
| MATRIX
)
WEIXIN_OC = enum.auto()
MATTERMOST = enum.auto()
WEBCHAT = enum.auto()
ALL = enum.auto()


ADAPTER_NAME_2_TYPE = {
"aiocqhttp": PlatformAdapterType.AIOCQHTTP,
"qq_official": PlatformAdapterType.QQOFFICIAL,
"qq_official_webhook": PlatformAdapterType.QQOFFICIAL_WEBHOOK,
"telegram": PlatformAdapterType.TELEGRAM,
"wecom": PlatformAdapterType.WECOM,
"wecom_ai_bot": PlatformAdapterType.WECOM_AI_BOT,
Expand All @@ -60,6 +48,9 @@ class PlatformAdapterType(enum.Flag):
"misskey": PlatformAdapterType.MISSKEY,
"line": PlatformAdapterType.LINE,
"matrix": PlatformAdapterType.MATRIX,
"weixin_oc": PlatformAdapterType.WEIXIN_OC,
"mattermost": PlatformAdapterType.MATTERMOST,
"webchat": PlatformAdapterType.WEBCHAT,
}


Expand All @@ -71,6 +62,12 @@ def __init__(self, platform_adapter_type_or_str: PlatformAdapterType | str) -> N
self.platform_type = platform_adapter_type_or_str

def filter(self, event: AstrMessageEvent, cfg: AstrBotConfig) -> bool:
if (
self.platform_type is not None
and self.platform_type & PlatformAdapterType.ALL
):
return True

adapter_name = event.get_platform_name()
if adapter_name in ADAPTER_NAME_2_TYPE and self.platform_type is not None:
return bool(ADAPTER_NAME_2_TYPE[adapter_name] & self.platform_type)
Expand Down
Loading
Loading