This commit is contained in:
liubiren 2026-07-05 13:06:20 +08:00
parent c499b16c3b
commit cc8efb7143
2 changed files with 144 additions and 143 deletions

View File

@ -98,12 +98,12 @@ class Run(BaseModel):
)
is_reasoning: bool = Field(
default=False,
description="正在推理状态True 表示正在推理False 表示非正在推理",
description="推理状态True 表示正在推理False 表示非正在推理",
)
assistant_content: str = Field(default="", description="回复正文")
is_streaming: bool = Field(
is_running: bool = Field(
default=False,
description="流式输出状态True 表示正在流式输出False 表示非正在流式输出",
description="运行状态True 表示正在运行False 表示非正在运行",
)

View File

@ -155,10 +155,10 @@ class ConversationState(rx.State):
return conversation.runs if conversation else {}
@rx.var
def get_run_streaming_status(self) -> bool:
def get_run_running_status(self) -> bool:
"""
获取当前运行流式输出状态
:return: 当前运行流式输出状态True 表示正在流式输出False 表示非正在流式输出
获取当前运行运行状态
:return: 当前运行状态True 表示正在运行False 表示非正在运行
"""
# 当前对话
conversation = self.conversations.get(self.conversation_id)
@ -167,7 +167,7 @@ class ConversationState(rx.State):
# 末位运行
run = next(reversed(conversation.runs.values()))
return run.is_streaming
return run.is_running
@rx.event
async def run(self, form_data: dict[str, Any]) -> AsyncGenerator[None]:
@ -200,15 +200,16 @@ class ConversationState(rx.State):
conversation.runs[str(uuid7())] = Run(user_prompt=user_prompt)
# 将末位运行唯一标识、运行设置为当前运行唯一标识、运行
run_id, run = next(reversed(conversation.runs.items()))
# 将流式输出状态设置为正在流式输出
run.is_streaming = True
yield # 通知前端渲染流式输出状态
# 将运行状态设置为正在运行
run.is_running = True
yield # 通知前端渲染运行状态
async for event in self.agent.run_stream_events(
async with self.agent.run_stream_events(
conversation_id=self.conversation_id,
user_prompt=user_prompt,
message_history=message_history,
):
) as events:
async for event in events:
match event:
# ========== 分片开始事件 ==========
case PartStartEvent(
@ -219,9 +220,9 @@ class ConversationState(rx.State):
match part:
# 思考分片开始事件
case ThinkingPart(
part_kind=part_kind, content=content
content=content
):
# 若上一分片种类为空则将正在推理状态设置为正在推理
# 若上一分片种类为空则将推理状态设置为正在推理
if not previous_part_kind:
run.is_reasoning = True
@ -241,7 +242,7 @@ class ConversationState(rx.State):
# 文本分片开始事件
case TextPart(
part_kind=part_kind, content=content
content=content
):
# 初始化回复正文
run.assistant_content = content
@ -284,7 +285,7 @@ class ConversationState(rx.State):
case ThinkingPart(
part_kind=part_kind, content=content
):
# 若下一分片种类为文本则将正在推理状态设置为非正在推理
# 若下一分片种类为文本则将推理状态设置为非正在推理
if next_part_kind == Kind.TEXT:
run.is_reasoning = False