refine internal middleware llm usage for streaming agents

Use a non-streaming model for middleware-only calls so internal outputs do not leak into user streams and model-based middleware stays consistent.
This commit is contained in:
jxxghp
2026-04-27 06:55:41 +08:00
parent 4208c79d72
commit 221eb21694
2 changed files with 130 additions and 3 deletions
+10 -3
View File
@@ -377,6 +377,11 @@ class MoviePilotAgent:
llm = self._initialize_llm(streaming=streaming)
self._sync_model_profile(llm)
# 为中间件内部模型调用准备非流式 LLM,避免与用户流式回复复用同一实例。
non_streaming_llm = (
llm if not streaming else self._initialize_llm(streaming=False)
)
# 工具列表
tools = self._initialize_tools()
@@ -399,8 +404,9 @@ class MoviePilotAgent:
),
# 用量统计
UsageMiddleware(on_usage=self._record_usage),
# 上下文压缩
SummarizationMiddleware(model=llm, trigger=("fraction", 0.85)),
SummarizationMiddleware(
model=non_streaming_llm, trigger=("fraction", 0.85)
),
# 错误工具调用修复
PatchToolCallsMiddleware(),
]
@@ -409,7 +415,8 @@ class MoviePilotAgent:
if settings.LLM_MAX_TOOLS > 0:
middlewares.append(
LLMToolSelectorMiddleware(
model=llm, max_tools=settings.LLM_MAX_TOOLS
model=non_streaming_llm,
max_tools=settings.LLM_MAX_TOOLS,
)
)