mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-15 17:30:13 +08:00
perf: improve streaming fallback strategy for streaming-unsupported platform (#3547)
* feat: 修改tool_loop_agent_runner,新增stream_to_general属性。 Co-authored-by: aider (openai/gemini-2.5-flash-preview) <aider@aider.chat> * refactor: 优化text_chat_stream,直接yield完整信息 Co-authored-by: aider (openai/gemini-2.5-flash-preview) <aider@aider.chat> * feat(core): ✨ 添加streaming_fallback选项,允许进行流式请求和非流式输出 添加了streaming_fallback配置,默认为false。在PlatformMetadata中新增字段用于标识是否支持真流式输出。在LLMRequest中添加判断是否启用Fallback。 #3431 #2793 #3014 * refactor(core): 将stream_to_general移出toolLoopAgentRunner * refactor(core.platform): 修改metadata中的属性名称 * fix: update streaming provider settings descriptions and add conditions * fix: update streaming configuration to use unsupported_streaming_strategy and adjust related logic * fix: remove support_streaming_message flag from WecomAIBotAdapter registration * fix: update hint for non-streaming platform handling in configuration * fix(core.pipeline): Update astrbot/core/pipeline/process_stage/method/llm_request.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix(core.pipeline): Update astrbot/core/pipeline/process_stage/method/llm_request.py --------- Co-authored-by: aider (openai/gemini-2.5-flash-preview) <aider@aider.chat> Co-authored-by: Soulter <37870767+Soulter@users.noreply.github.com> Co-authored-by: Soulter <905617992@qq.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -68,7 +68,7 @@ DEFAULT_CONFIG = {
|
||||
"dequeue_context_length": 1,
|
||||
"streaming_response": False,
|
||||
"show_tool_use_status": False,
|
||||
"streaming_segmented": False,
|
||||
"unsupported_streaming_strategy": "realtime_segmenting",
|
||||
"max_agent_step": 30,
|
||||
"tool_call_timeout": 60,
|
||||
},
|
||||
@@ -1993,8 +1993,8 @@ CONFIG_METADATA_2 = {
|
||||
"show_tool_use_status": {
|
||||
"type": "bool",
|
||||
},
|
||||
"streaming_segmented": {
|
||||
"type": "bool",
|
||||
"unsupported_streaming_strategy": {
|
||||
"type": "string",
|
||||
},
|
||||
"max_agent_step": {
|
||||
"description": "工具调用轮数上限",
|
||||
@@ -2299,9 +2299,15 @@ CONFIG_METADATA_3 = {
|
||||
"description": "流式回复",
|
||||
"type": "bool",
|
||||
},
|
||||
"provider_settings.streaming_segmented": {
|
||||
"description": "不支持流式回复的平台采取分段输出",
|
||||
"type": "bool",
|
||||
"provider_settings.unsupported_streaming_strategy": {
|
||||
"description": "不支持流式回复的平台",
|
||||
"type": "string",
|
||||
"options": ["realtime_segmenting", "turn_off"],
|
||||
"hint": "选择在不支持流式回复的平台上的处理方式。实时分段回复会在系统接收流式响应检测到诸如标点符号等分段点时,立即发送当前已接收的内容",
|
||||
"labels": ["实时分段回复", "关闭流式回复"],
|
||||
"condition": {
|
||||
"provider_settings.streaming_response": True,
|
||||
},
|
||||
},
|
||||
"provider_settings.max_context_length": {
|
||||
"description": "最多携带对话轮数",
|
||||
|
||||
@@ -294,6 +294,7 @@ async def run_agent(
|
||||
agent_runner: AgentRunner,
|
||||
max_step: int = 30,
|
||||
show_tool_use: bool = True,
|
||||
stream_to_general: bool = False,
|
||||
) -> AsyncGenerator[MessageChain, None]:
|
||||
step_idx = 0
|
||||
astr_event = agent_runner.run_context.context.event
|
||||
@@ -321,7 +322,10 @@ async def run_agent(
|
||||
await astr_event.send(resp.data["chain"])
|
||||
continue
|
||||
|
||||
if not agent_runner.streaming:
|
||||
if stream_to_general and resp.type == "streaming_delta":
|
||||
continue
|
||||
|
||||
if stream_to_general or not agent_runner.streaming:
|
||||
content_typ = (
|
||||
ResultContentType.LLM_RESULT
|
||||
if resp.type == "llm_result"
|
||||
@@ -363,6 +367,9 @@ class LLMRequestSubStage(Stage):
|
||||
self.max_context_length - 1,
|
||||
)
|
||||
self.streaming_response: bool = settings["streaming_response"]
|
||||
self.unsupported_streaming_strategy: str = settings[
|
||||
"unsupported_streaming_strategy"
|
||||
]
|
||||
self.max_step: int = settings.get("max_agent_step", 30)
|
||||
self.tool_call_timeout: int = settings.get("tool_call_timeout", 60)
|
||||
if isinstance(self.max_step, bool): # workaround: #2622
|
||||
@@ -540,6 +547,10 @@ class LLMRequestSubStage(Stage):
|
||||
new_tool_set.add_tool(tool)
|
||||
req.func_tool = new_tool_set
|
||||
|
||||
stream_to_general = (
|
||||
self.unsupported_streaming_strategy == "turn_off"
|
||||
and not event.platform_meta.support_streaming_message
|
||||
)
|
||||
# 备份 req.contexts
|
||||
backup_contexts = copy.deepcopy(req.contexts)
|
||||
|
||||
@@ -567,7 +578,7 @@ class LLMRequestSubStage(Stage):
|
||||
streaming=streaming_response,
|
||||
)
|
||||
|
||||
if streaming_response:
|
||||
if streaming_response and not stream_to_general:
|
||||
# 流式响应
|
||||
event.set_result(
|
||||
MessageEventResult()
|
||||
@@ -594,7 +605,9 @@ class LLMRequestSubStage(Stage):
|
||||
),
|
||||
)
|
||||
else:
|
||||
async for _ in run_agent(agent_runner, self.max_step, self.show_tool_use):
|
||||
async for _ in run_agent(
|
||||
agent_runner, self.max_step, self.show_tool_use, stream_to_general
|
||||
):
|
||||
yield
|
||||
|
||||
# 恢复备份的 contexts
|
||||
|
||||
@@ -169,12 +169,15 @@ class RespondStage(Stage):
|
||||
logger.warning("async_stream 为空,跳过发送。")
|
||||
return
|
||||
# 流式结果直接交付平台适配器处理
|
||||
use_fallback = self.config.get("provider_settings", {}).get(
|
||||
"streaming_segmented",
|
||||
False,
|
||||
realtime_segmenting = (
|
||||
self.config.get("provider_settings", {}).get(
|
||||
"unsupported_streaming_strategy",
|
||||
"realtime_segmenting",
|
||||
)
|
||||
== "realtime_segmenting"
|
||||
)
|
||||
logger.info(f"应用流式输出({event.get_platform_id()})")
|
||||
await event.send_streaming(result.async_stream, use_fallback)
|
||||
await event.send_streaming(result.async_stream, realtime_segmenting)
|
||||
return
|
||||
if len(result.chain) > 0:
|
||||
# 检查路径映射
|
||||
|
||||
@@ -16,3 +16,6 @@ class PlatformMetadata:
|
||||
"""显示在 WebUI 配置页中的平台名称,如空则是 name"""
|
||||
logo_path: str | None = None
|
||||
"""平台适配器的 logo 文件路径(相对于插件目录)"""
|
||||
|
||||
support_streaming_message: bool = True
|
||||
"""平台是否支持真实流式传输"""
|
||||
|
||||
@@ -14,6 +14,7 @@ def register_platform_adapter(
|
||||
default_config_tmpl: dict | None = None,
|
||||
adapter_display_name: str | None = None,
|
||||
logo_path: str | None = None,
|
||||
support_streaming_message: bool = True,
|
||||
):
|
||||
"""用于注册平台适配器的带参装饰器。
|
||||
|
||||
@@ -42,6 +43,7 @@ def register_platform_adapter(
|
||||
default_config_tmpl=default_config_tmpl,
|
||||
adapter_display_name=adapter_display_name,
|
||||
logo_path=logo_path,
|
||||
support_streaming_message=support_streaming_message,
|
||||
)
|
||||
platform_registry.append(pm)
|
||||
platform_cls_map[adapter_name] = cls
|
||||
|
||||
@@ -29,6 +29,7 @@ from .aiocqhttp_message_event import AiocqhttpMessageEvent
|
||||
@register_platform_adapter(
|
||||
"aiocqhttp",
|
||||
"适用于 OneBot V11 标准的消息平台适配器,支持反向 WebSockets。",
|
||||
support_streaming_message=False,
|
||||
)
|
||||
class AiocqhttpAdapter(Platform):
|
||||
def __init__(
|
||||
@@ -49,6 +50,7 @@ class AiocqhttpAdapter(Platform):
|
||||
name="aiocqhttp",
|
||||
description="适用于 OneBot 标准的消息平台适配器,支持反向 WebSockets。",
|
||||
id=self.config.get("id"),
|
||||
support_streaming_message=False,
|
||||
)
|
||||
|
||||
self.bot = CQHttp(
|
||||
|
||||
@@ -37,7 +37,9 @@ class MyEventHandler(dingtalk_stream.EventHandler):
|
||||
return AckMessage.STATUS_OK, "OK"
|
||||
|
||||
|
||||
@register_platform_adapter("dingtalk", "钉钉机器人官方 API 适配器")
|
||||
@register_platform_adapter(
|
||||
"dingtalk", "钉钉机器人官方 API 适配器", support_streaming_message=False
|
||||
)
|
||||
class DingtalkPlatformAdapter(Platform):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -86,6 +88,7 @@ class DingtalkPlatformAdapter(Platform):
|
||||
name="dingtalk",
|
||||
description="钉钉机器人官方 API 适配器",
|
||||
id=self.config.get("id"),
|
||||
support_streaming_message=False,
|
||||
)
|
||||
|
||||
async def convert_msg(
|
||||
|
||||
@@ -34,7 +34,9 @@ else:
|
||||
|
||||
|
||||
# 注册平台适配器
|
||||
@register_platform_adapter("discord", "Discord 适配器 (基于 Pycord)")
|
||||
@register_platform_adapter(
|
||||
"discord", "Discord 适配器 (基于 Pycord)", support_streaming_message=False
|
||||
)
|
||||
class DiscordPlatformAdapter(Platform):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -111,6 +113,7 @@ class DiscordPlatformAdapter(Platform):
|
||||
"Discord 适配器",
|
||||
id=self.config.get("id"),
|
||||
default_config_tmpl=self.config,
|
||||
support_streaming_message=False,
|
||||
)
|
||||
|
||||
@override
|
||||
|
||||
@@ -23,7 +23,9 @@ from ...register import register_platform_adapter
|
||||
from .lark_event import LarkMessageEvent
|
||||
|
||||
|
||||
@register_platform_adapter("lark", "飞书机器人官方 API 适配器")
|
||||
@register_platform_adapter(
|
||||
"lark", "飞书机器人官方 API 适配器", support_streaming_message=False
|
||||
)
|
||||
class LarkPlatformAdapter(Platform):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -115,6 +117,7 @@ class LarkPlatformAdapter(Platform):
|
||||
name="lark",
|
||||
description="飞书机器人官方 API 适配器",
|
||||
id=self.config.get("id"),
|
||||
support_streaming_message=False,
|
||||
)
|
||||
|
||||
async def convert_msg(self, event: lark.im.v1.P2ImMessageReceiveV1):
|
||||
|
||||
@@ -45,7 +45,9 @@ MAX_FILE_UPLOAD_COUNT = 16
|
||||
DEFAULT_UPLOAD_CONCURRENCY = 3
|
||||
|
||||
|
||||
@register_platform_adapter("misskey", "Misskey 平台适配器")
|
||||
@register_platform_adapter(
|
||||
"misskey", "Misskey 平台适配器", support_streaming_message=False
|
||||
)
|
||||
class MisskeyPlatformAdapter(Platform):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -120,6 +122,7 @@ class MisskeyPlatformAdapter(Platform):
|
||||
description="Misskey 平台适配器",
|
||||
id=self.config.get("id", "misskey"),
|
||||
default_config_tmpl=default_config,
|
||||
support_streaming_message=False,
|
||||
)
|
||||
|
||||
async def run(self):
|
||||
|
||||
@@ -29,8 +29,7 @@ from astrbot.core.platform.astr_message_event import MessageSession
|
||||
|
||||
|
||||
@register_platform_adapter(
|
||||
"satori",
|
||||
"Satori 协议适配器",
|
||||
"satori", "Satori 协议适配器", support_streaming_message=False
|
||||
)
|
||||
class SatoriPlatformAdapter(Platform):
|
||||
def __init__(
|
||||
@@ -60,6 +59,7 @@ class SatoriPlatformAdapter(Platform):
|
||||
name="satori",
|
||||
description="Satori 通用协议适配器",
|
||||
id=self.config["id"],
|
||||
support_streaming_message=False,
|
||||
)
|
||||
|
||||
self.ws: ClientConnection | None = None
|
||||
|
||||
@@ -30,6 +30,7 @@ from .slack_event import SlackMessageEvent
|
||||
@register_platform_adapter(
|
||||
"slack",
|
||||
"适用于 Slack 的消息平台适配器,支持 Socket Mode 和 Webhook Mode。",
|
||||
support_streaming_message=False,
|
||||
)
|
||||
class SlackAdapter(Platform):
|
||||
def __init__(
|
||||
@@ -68,6 +69,7 @@ class SlackAdapter(Platform):
|
||||
name="slack",
|
||||
description="适用于 Slack 的消息平台适配器,支持 Socket Mode 和 Webhook Mode。",
|
||||
id=self.config.get("id"),
|
||||
support_streaming_message=False,
|
||||
)
|
||||
|
||||
# 初始化 Slack Web Client
|
||||
|
||||
@@ -32,7 +32,9 @@ except ImportError as e:
|
||||
)
|
||||
|
||||
|
||||
@register_platform_adapter("wechatpadpro", "WeChatPadPro 消息平台适配器")
|
||||
@register_platform_adapter(
|
||||
"wechatpadpro", "WeChatPadPro 消息平台适配器", support_streaming_message=False
|
||||
)
|
||||
class WeChatPadProAdapter(Platform):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -51,6 +53,7 @@ class WeChatPadProAdapter(Platform):
|
||||
name="wechatpadpro",
|
||||
description="WeChatPadPro 消息平台适配器",
|
||||
id=self.config.get("id", "wechatpadpro"),
|
||||
support_streaming_message=False,
|
||||
)
|
||||
|
||||
# 保存配置信息
|
||||
|
||||
@@ -110,7 +110,7 @@ class WecomServer:
|
||||
await self.shutdown_event.wait()
|
||||
|
||||
|
||||
@register_platform_adapter("wecom", "wecom 适配器")
|
||||
@register_platform_adapter("wecom", "wecom 适配器", support_streaming_message=False)
|
||||
class WecomPlatformAdapter(Platform):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -196,6 +196,7 @@ class WecomPlatformAdapter(Platform):
|
||||
"wecom",
|
||||
"wecom 适配器",
|
||||
id=self.config.get("id", "wecom"),
|
||||
support_streaming_message=False,
|
||||
)
|
||||
|
||||
@override
|
||||
|
||||
@@ -113,7 +113,9 @@ class WecomServer:
|
||||
await self.shutdown_event.wait()
|
||||
|
||||
|
||||
@register_platform_adapter("weixin_official_account", "微信公众平台 适配器")
|
||||
@register_platform_adapter(
|
||||
"weixin_official_account", "微信公众平台 适配器", support_streaming_message=False
|
||||
)
|
||||
class WeixinOfficialAccountPlatformAdapter(Platform):
|
||||
def __init__(
|
||||
self,
|
||||
@@ -195,6 +197,7 @@ class WeixinOfficialAccountPlatformAdapter(Platform):
|
||||
"weixin_official_account",
|
||||
"微信公众平台 适配器",
|
||||
id=self.config.get("id", "weixin_official_account"),
|
||||
support_streaming_message=False,
|
||||
)
|
||||
|
||||
@override
|
||||
|
||||
@@ -154,7 +154,8 @@ function hasVisibleItemsAfter(items, currentIndex) {
|
||||
<div class="w-100" v-if="!itemMeta?._special">
|
||||
<!-- Select input for JSON selector -->
|
||||
<v-select v-if="itemMeta?.options" v-model="createSelectorModel(itemKey).value"
|
||||
:items="itemMeta?.options" :disabled="itemMeta?.readonly" density="compact" variant="outlined"
|
||||
:items="itemMeta?.labels ? itemMeta.options.map((value, index) => ({ title: itemMeta.labels[index] || value, value: value })) : itemMeta.options"
|
||||
:disabled="itemMeta?.readonly" density="compact" variant="outlined"
|
||||
class="config-field" hide-details></v-select>
|
||||
|
||||
<!-- Code Editor for JSON selector -->
|
||||
|
||||
Reference in New Issue
Block a user