This commit is contained in:
liubiren 2026-07-01 00:00:08 +08:00
parent 1493f9d1fe
commit 11a4505db1
2 changed files with 45 additions and 33 deletions

View File

@ -17,6 +17,7 @@ class ConversationState(rx.State):
""" """
会话状态 会话状态
""" """
# 当前会话唯一标识 # 当前会话唯一标识
current_conversation_id: str = str(uuid7()) current_conversation_id: str = str(uuid7())
# 会话字典 # 会话字典
@ -133,7 +134,10 @@ class ConversationState(rx.State):
current_conversation = self.conversations[self.current_conversation_id] current_conversation = self.conversations[self.current_conversation_id]
# 若当前运行唯一标识为空或不存在则返回 False # 若当前运行唯一标识为空或不存在则返回 False
if not self.current_run_id or self.current_run_id not in current_conversation.runs: if (
not self.current_run_id
or self.current_run_id not in current_conversation.runs
):
return False return False
# 当前运行 # 当前运行
@ -153,7 +157,7 @@ class ConversationState(rx.State):
# 当前会话 # 当前会话
current_conversation = self.conversations[self.current_conversation_id] current_conversation = self.conversations[self.current_conversation_id]
# 获取用户提示词 # 获取用户提示词
user_prompt = form_data["user_prompt"].strip() user_prompt = form_data["user_prompt"].strip()
if not user_prompt: if not user_prompt:
@ -171,32 +175,38 @@ class ConversationState(rx.State):
user_prompt=user_prompt, user_prompt=user_prompt,
message_history=message_history, message_history=message_history,
): ):
# 若为运行开始事件则将事件运行唯一标识设置为当前运行唯一标识
if event.event_kind == EventKind.RUN_START: # 若事件为空或当前运行唯一标识为空或当前运行唯一标识与事件运行唯一标识不相同则跳过
self.current_run_id = event.run_id if (
continue not event
or not self.current_run_id
# 若当前运行唯一标识为空或与事件运行唯一标识不相同则跳过 or self.current_run_id != event.run_id
if not self.current_run_id or self.current_run_id != event.run_id: ):
continue continue
# 若当前运行唯一标识不存在则新增运行 # ========== 运行开始 ==========
if self.current_run_id not in current_conversation.runs: if event.event_kind == EventKind.RUN_START:
# 新增运行 # 将事件运行唯一标识设置为当前运行唯一标识
current_conversation.runs[self.current_run_id] = Run(user_prompt=user_prompt, is_streaming=True) self.current_run_id = event.run_id
# 创建运行
current_conversation.runs[self.current_run_id] = Run(
user_prompt=user_prompt, is_streaming=True
)
yield # 通知前端渲染 yield # 通知前端渲染
continue continue
# 当前运行 # 当前运行
current_run = current_conversation.runs[self.current_run_id] current_run = current_conversation.runs[self.current_run_id]
# ========== 推理 ========== # ========== 推理 ==========
if event.event_kind == EventKind.PART_START and event.part_kind == PartKind.THINKING: if (
event.event_kind == EventKind.PART_START
and event.part_kind == PartKind.THINKING
):
current_run.thinking += event.event_content current_run.thinking += event.event_content
yield # 通知前端渲染 yield # 通知前端渲染
continue continue
if part_index := event.part_index: if part_index := event.part_index:
if part_index not in current_run.reasonings: if part_index not in current_run.reasonings:
current_run.reasonings[part_index] += event.event_content current_run.reasonings[part_index] += event.event_content
@ -204,7 +214,10 @@ class ConversationState(rx.State):
continue continue
# ========== 回答 ========== # ========== 回答 ==========
if event.part_kind == PartKind.TEXT: if (
event.part_kind == PartKind.TEXT
and event.event_content != EventKind.PART_END
):
current_run.answer += event.event_content current_run.answer += event.event_content
yield # 通知前端渲染 yield # 通知前端渲染
continue continue
@ -213,24 +226,11 @@ class ConversationState(rx.State):
if event.event_kind == EventKind.RUN_END: if event.event_kind == EventKind.RUN_END:
# 保存运行新增消息 # 保存运行新增消息
await database_state.save_new_message( await database_state.save_new_message(
conversation_id=self.current_conversation_id, run_id=self.current_run_id, run_new_message=event.run_new_messages conversation_id=self.current_conversation_id,
run_id=self.current_run_id,
run_new_message=event.run_new_messages,
) )
# 设置当前运行流式输出状态非正在流式输出 # 设置当前运行流式输出状态非正在流式输出
current_run.is_streaming = False current_run.is_streaming = False
yield # 通知前端渲染 yield # 通知前端渲染
continue continue

View File

@ -305,6 +305,18 @@ class AIAgent:
tool_name=tool_name, tool_name=tool_name,
run_id=current_run_id, run_id=current_run_id,
) )
# 文本分片结束事件
case TextPart(
part_kind=part_kind, content=content
):
yield Event(
event_kind=EventKind(event_kind),
event_content=content,
part_index=index,
part_kind=PartKind(part_kind),
next_part_kind=PartKind(next_part_kind),
run_id=current_run_id,
)
# ========== 函数工具调用事件 ========== # ========== 函数工具调用事件 ==========
case FunctionToolCallEvent( case FunctionToolCallEvent(