From cc8efb71436c58d9a2fa044782e821352e927880 Mon Sep 17 00:00:00 2001 From: liubiren Date: Sun, 5 Jul 2026 13:06:20 +0800 Subject: [PATCH] 1 --- 智能体/application/models.py | 6 +- 智能体/application/state/conversation.py | 281 ++++++++++++----------- 2 files changed, 144 insertions(+), 143 deletions(-) diff --git a/智能体/application/models.py b/智能体/application/models.py index ec5d15c..019f623 100644 --- a/智能体/application/models.py +++ b/智能体/application/models.py @@ -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 表示非正在运行", ) diff --git a/智能体/application/state/conversation.py b/智能体/application/state/conversation.py index b6b9af4..682de0d 100644 --- a/智能体/application/state/conversation.py +++ b/智能体/application/state/conversation.py @@ -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,159 +200,160 @@ 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, - ): - match event: - # ========== 分片开始事件 ========== - case PartStartEvent( - index=index, - part=part, - previous_part_kind=previous_part_kind, - ): - match part: - # 思考分片开始事件 - case ThinkingPart( - part_kind=part_kind, content=content - ): - # 若上一分片种类为空则将正在推理状态设置为正在推理 - if not previous_part_kind: - run.is_reasoning = True + ) as events: + async for event in events: + match event: + # ========== 分片开始事件 ========== + case PartStartEvent( + index=index, + part=part, + previous_part_kind=previous_part_kind, + ): + match part: + # 思考分片开始事件 + case ThinkingPart( + content=content + ): + # 若上一分片种类为空则将推理状态设置为正在推理 + if not previous_part_kind: + run.is_reasoning = True - # 初始化推理 - run.reasonings[index] = Reasoning(kind=Kind.THINKING, content=content) - yield # 通知前端渲染推理 + # 初始化推理 + run.reasonings[index] = Reasoning(kind=Kind.THINKING, content=content) + yield # 通知前端渲染推理 - case ToolSearchCallPart( - tool_call_id=tool_call_id - ): - # 创建工具调用唯一标识与片段索引映射 - tool_call_ids[index] = tool_call_id + case ToolSearchCallPart( + tool_call_id=tool_call_id + ): + # 创建工具调用唯一标识与片段索引映射 + tool_call_ids[index] = tool_call_id - # 初始化推理 - run.reasonings[index] = Reasoning(kind=Kind.TOOL_SEARCH, content="正在检索") - yield # 通知前端渲染推理 + # 初始化推理 + run.reasonings[index] = Reasoning(kind=Kind.TOOL_SEARCH, content="正在检索") + yield # 通知前端渲染推理 - # 文本分片开始事件 - case TextPart( - part_kind=part_kind, content=content - ): - # 初始化回复正文 - run.assistant_content = content - yield # 通知前端渲染回复正文 + # 文本分片开始事件 + case TextPart( + content=content + ): + # 初始化回复正文 + run.assistant_content = content + yield # 通知前端渲染回复正文 - case _: - continue + case _: + continue - # ========== 分片增量事件(前端仅就文本片段实现打字机效果) ========== - case PartDeltaEvent( - index=index, delta=delta - ): - match delta: - # 思考分片增量事件 - case ThinkingPartDelta( - content_delta=content_delta, - ): - run.reasonings[index].content += content_delta or "" - yield # 通知前端渲染推理 + # ========== 分片增量事件(前端仅就文本片段实现打字机效果) ========== + case PartDeltaEvent( + index=index, delta=delta + ): + match delta: + # 思考分片增量事件 + case ThinkingPartDelta( + content_delta=content_delta, + ): + run.reasonings[index].content += content_delta or "" + yield # 通知前端渲染推理 - # 文本分片增量事件 - case TextPartDelta( - content_delta=content_delta, - ): - run.assistant_content += content_delta - yield # 通知前端渲染回复正文 + # 文本分片增量事件 + case TextPartDelta( + content_delta=content_delta, + ): + run.assistant_content += content_delta + yield # 通知前端渲染回复正文 - case _: - continue + case _: + continue - # ========== 分片结束事件 ========== - case PartEndEvent( - event_kind=event_kind, - index=index, - part=part, - next_part_kind=next_part_kind, - ): - match part: - # 思考分片结束事件 - case ThinkingPart( - part_kind=part_kind, content=content - ): - # 若下一分片种类为文本则将正在推理状态设置为非正在推理 - if next_part_kind == Kind.TEXT: - run.is_reasoning = False + # ========== 分片结束事件 ========== + case PartEndEvent( + event_kind=event_kind, + index=index, + part=part, + next_part_kind=next_part_kind, + ): + match part: + # 思考分片结束事件 + case ThinkingPart( + part_kind=part_kind, content=content + ): + # 若下一分片种类为文本则将推理状态设置为非正在推理 + if next_part_kind == Kind.TEXT: + run.is_reasoning = False - # 检索工具分片结束事件 - case ToolSearchCallPart( - tool_call_id=tool_call_id - ): - # 创建工具调用唯一标识与片段索引映射 - tool_call_ids[index] = tool_call_id + # 检索工具分片结束事件 + case ToolSearchCallPart( + tool_call_id=tool_call_id + ): + # 创建工具调用唯一标识与片段索引映射 + tool_call_ids[index] = tool_call_id - # 加载能力分片结束事件 - case LoadCapabilityCallPart( - tool_kind=tool_kind, tool_name=tool_name - ): - yield Event( - event_kind=EventKind(event_kind), - part_index=index, - part_kind=PartKind(tool_kind), - next_part_kind=PartKind(next_part_kind), - tool_name=tool_name, - run_id=current_run_id, - ) - # 调用工具分片结束事件 - case ToolCallPart( - part_kind=part_kind, tool_name=tool_name - ): - yield Event( - event_kind=EventKind(event_kind), - part_index=index, - part_kind=PartKind(part_kind), - next_part_kind=PartKind(next_part_kind), - tool_name=tool_name, - run_id=current_run_id, - ) + # 加载能力分片结束事件 + case LoadCapabilityCallPart( + tool_kind=tool_kind, tool_name=tool_name + ): + yield Event( + event_kind=EventKind(event_kind), + part_index=index, + part_kind=PartKind(tool_kind), + next_part_kind=PartKind(next_part_kind), + tool_name=tool_name, + run_id=current_run_id, + ) + # 调用工具分片结束事件 + case ToolCallPart( + part_kind=part_kind, tool_name=tool_name + ): + yield Event( + event_kind=EventKind(event_kind), + part_index=index, + part_kind=PartKind(part_kind), + next_part_kind=PartKind(next_part_kind), + tool_name=tool_name, + run_id=current_run_id, + ) - # ========== 函数工具调用事件 ========== - case FunctionToolCallEvent( - event_kind=event_kind, - part=part, - ): - yield Event( - event_kind=EventKind(event_kind), - part_index=self.tool_call_ids.get( - part.tool_call_id - ), - part_kind=PartKind(part.part_kind), - tool_name=part.tool_name, - run_id=current_run_id, - ) + # ========== 函数工具调用事件 ========== + case FunctionToolCallEvent( + event_kind=event_kind, + part=part, + ): + yield Event( + event_kind=EventKind(event_kind), + part_index=self.tool_call_ids.get( + part.tool_call_id + ), + part_kind=PartKind(part.part_kind), + tool_name=part.tool_name, + run_id=current_run_id, + ) - # ========== 函数工具回调事件 ========== - case FunctionToolResultEvent( - event_kind=event_kind, - content=content, - part=part, - ): - yield Event( - event_kind=EventKind(event_kind), - event_content=( - content if isinstance(content, str) else "" - ), # 暂仅处理文本类型,后续处理多模态和缓存 - part_index=self.tool_call_ids.get( - part.tool_call_id - ), - part_kind=PartKind(part.part_kind), - tool_name=part.tool_name, - run_id=current_run_id, - ) + # ========== 函数工具回调事件 ========== + case FunctionToolResultEvent( + event_kind=event_kind, + content=content, + part=part, + ): + yield Event( + event_kind=EventKind(event_kind), + event_content=( + content if isinstance(content, str) else "" + ), # 暂仅处理文本类型,后续处理多模态和缓存 + part_index=self.tool_call_ids.get( + part.tool_call_id + ), + part_kind=PartKind(part.part_kind), + tool_name=part.tool_name, + run_id=current_run_id, + ) yield Event( event_kind=EventKind.RUN_END,