refactor: convert isinstance chains to match/case in easy_ui_based_generate_task_pipeline.py (#36222)

This commit is contained in:
EvanYao 2026-05-15 21:51:49 +08:00 committed by GitHub
parent 27b084c4d4
commit c0bdd6792f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -140,42 +140,43 @@ class EasyUIBasedGenerateTaskPipeline(BasedGenerateTaskPipeline):
:return: :return:
""" """
for stream_response in generator: for stream_response in generator:
if isinstance(stream_response, ErrorStreamResponse): match stream_response:
raise stream_response.err case ErrorStreamResponse():
elif isinstance(stream_response, MessageEndStreamResponse): raise stream_response.err
extras = {"usage": self._task_state.llm_result.usage.model_dump()} case MessageEndStreamResponse():
if self._task_state.metadata: extras = {"usage": self._task_state.llm_result.usage.model_dump()}
extras["metadata"] = self._task_state.metadata.model_dump() if self._task_state.metadata:
response: ChatbotAppBlockingResponse | CompletionAppBlockingResponse extras["metadata"] = self._task_state.metadata.model_dump()
if self._conversation_mode == AppMode.COMPLETION: response: ChatbotAppBlockingResponse | CompletionAppBlockingResponse
response = CompletionAppBlockingResponse( if self._conversation_mode == AppMode.COMPLETION:
task_id=self._application_generate_entity.task_id, response = CompletionAppBlockingResponse(
data=CompletionAppBlockingResponse.Data( task_id=self._application_generate_entity.task_id,
id=self._message_id, data=CompletionAppBlockingResponse.Data(
mode=self._conversation_mode, id=self._message_id,
message_id=self._message_id, mode=self._conversation_mode,
answer=self._task_state.llm_result.message.get_text_content(), message_id=self._message_id,
created_at=self._message_created_at, answer=self._task_state.llm_result.message.get_text_content(),
**extras, created_at=self._message_created_at,
), **extras,
) ),
else: )
response = ChatbotAppBlockingResponse( else:
task_id=self._application_generate_entity.task_id, response = ChatbotAppBlockingResponse(
data=ChatbotAppBlockingResponse.Data( task_id=self._application_generate_entity.task_id,
id=self._message_id, data=ChatbotAppBlockingResponse.Data(
mode=self._conversation_mode, id=self._message_id,
conversation_id=self._conversation_id, mode=self._conversation_mode,
message_id=self._message_id, conversation_id=self._conversation_id,
answer=self._task_state.llm_result.message.get_text_content(), message_id=self._message_id,
created_at=self._message_created_at, answer=self._task_state.llm_result.message.get_text_content(),
**extras, created_at=self._message_created_at,
), **extras,
) ),
)
return response return response
else: case _:
continue continue
raise RuntimeError("queue listening stopped unexpectedly.") raise RuntimeError("queue listening stopped unexpectedly.")
@ -265,104 +266,107 @@ class EasyUIBasedGenerateTaskPipeline(BasedGenerateTaskPipeline):
publisher.publish(message) publisher.publish(message)
event = message.event event = message.event
if isinstance(event, QueueErrorEvent): match event:
with sessionmaker(bind=db.engine).begin() as session: case QueueErrorEvent():
err = self.handle_error(event=event, session=session, message_id=self._message_id) with sessionmaker(bind=db.engine).begin() as session:
yield self.error_to_stream_response(err) err = self.handle_error(event=event, session=session, message_id=self._message_id)
break yield self.error_to_stream_response(err)
elif isinstance(event, QueueStopEvent | QueueMessageEndEvent): break
if isinstance(event, QueueMessageEndEvent): case QueueStopEvent() | QueueMessageEndEvent():
if event.llm_result: if isinstance(event, QueueMessageEndEvent):
self._task_state.llm_result = event.llm_result if event.llm_result:
else: self._task_state.llm_result = event.llm_result
self._handle_stop(event) else:
self._handle_stop(event)
# handle output moderation # handle output moderation
output_moderation_answer = self.handle_output_moderation_when_task_finished( output_moderation_answer = self.handle_output_moderation_when_task_finished(
self._task_state.llm_result.message.get_text_content() self._task_state.llm_result.message.get_text_content()
)
if output_moderation_answer:
self._task_state.llm_result.message.content = output_moderation_answer
yield self._message_cycle_manager.message_replace_to_stream_response(
answer=output_moderation_answer
) )
if output_moderation_answer:
with sessionmaker(bind=db.engine).begin() as session: self._task_state.llm_result.message.content = output_moderation_answer
# Save message yield self._message_cycle_manager.message_replace_to_stream_response(
self._save_message(session=session, trace_manager=trace_manager) answer=output_moderation_answer
message_end_resp = self._message_end_to_stream_response()
yield message_end_resp
elif isinstance(event, QueueRetrieverResourcesEvent):
self._message_cycle_manager.handle_retriever_resources(event)
elif isinstance(event, QueueAnnotationReplyEvent):
annotation = self._message_cycle_manager.handle_annotation_reply(event)
if annotation:
self._task_state.llm_result.message.content = annotation.content
elif isinstance(event, QueueAgentThoughtEvent):
agent_thought_response = self._agent_thought_to_stream_response(event)
if agent_thought_response is not None:
yield agent_thought_response
elif isinstance(event, QueueMessageFileEvent):
response = self._message_cycle_manager.message_file_to_stream_response(event)
if response:
yield response
elif isinstance(event, QueueLLMChunkEvent | QueueAgentMessageEvent):
chunk = event.chunk
delta_text = chunk.delta.message.content
if delta_text is None:
continue
if isinstance(chunk.delta.message.content, list):
delta_text = ""
for content in chunk.delta.message.content:
logger.debug(
"The content type %s in LLM chunk delta message content.: %r", type(content), content
) )
if isinstance(content, TextPromptMessageContent):
delta_text += content.data with sessionmaker(bind=db.engine).begin() as session:
elif isinstance(content, str): # Save message
delta_text += content # failback to str self._save_message(session=session, trace_manager=trace_manager)
else: message_end_resp = self._message_end_to_stream_response()
logger.warning( yield message_end_resp
"Unsupported content type %s in LLM chunk delta message content.: %r", case QueueRetrieverResourcesEvent():
type(content), self._message_cycle_manager.handle_retriever_resources(event)
content, case QueueAnnotationReplyEvent():
annotation = self._message_cycle_manager.handle_annotation_reply(event)
if annotation:
self._task_state.llm_result.message.content = annotation.content
case QueueAgentThoughtEvent():
agent_thought_response = self._agent_thought_to_stream_response(event)
if agent_thought_response is not None:
yield agent_thought_response
case QueueMessageFileEvent():
response = self._message_cycle_manager.message_file_to_stream_response(event)
if response:
yield response
case QueueLLMChunkEvent() | QueueAgentMessageEvent():
chunk = event.chunk
delta_text = chunk.delta.message.content
if delta_text is None:
continue
if isinstance(chunk.delta.message.content, list):
delta_text = ""
for content in chunk.delta.message.content:
logger.debug(
"The content type %s in LLM chunk delta message content.: %r", type(content), content
) )
continue match content:
case TextPromptMessageContent():
delta_text += content.data
case str():
delta_text += content # failback to str
case _:
logger.warning(
"Unsupported content type %s in LLM chunk delta message content.: %r",
type(content),
content,
)
continue
if not self._task_state.llm_result.prompt_messages: if not self._task_state.llm_result.prompt_messages:
self._task_state.llm_result.prompt_messages = chunk.prompt_messages self._task_state.llm_result.prompt_messages = chunk.prompt_messages
# handle output moderation chunk # handle output moderation chunk
should_direct_answer = self._handle_output_moderation_chunk(cast(str, delta_text)) should_direct_answer = self._handle_output_moderation_chunk(cast(str, delta_text))
if should_direct_answer: if should_direct_answer:
continue
current_content = cast(str, self._task_state.llm_result.message.content)
current_content += cast(str, delta_text)
self._task_state.llm_result.message.content = current_content
match event:
case QueueLLMChunkEvent():
# Determine the event type once, on first LLM chunk, and reuse for subsequent chunks
if not hasattr(self, "_precomputed_event_type") or self._precomputed_event_type is None:
self._precomputed_event_type = self._message_cycle_manager.get_message_event_type(
message_id=self._message_id
)
yield self._message_cycle_manager.message_to_stream_response(
answer=cast(str, delta_text),
message_id=self._message_id,
event_type=self._precomputed_event_type,
)
case _:
yield self._agent_message_to_stream_response(
answer=cast(str, delta_text),
message_id=self._message_id,
)
case QueueMessageReplaceEvent():
yield self._message_cycle_manager.message_replace_to_stream_response(answer=event.text)
case QueuePingEvent():
yield self.ping_stream_response()
case _:
continue continue
current_content = cast(str, self._task_state.llm_result.message.content)
current_content += cast(str, delta_text)
self._task_state.llm_result.message.content = current_content
if isinstance(event, QueueLLMChunkEvent):
# Determine the event type once, on first LLM chunk, and reuse for subsequent chunks
if not hasattr(self, "_precomputed_event_type") or self._precomputed_event_type is None:
self._precomputed_event_type = self._message_cycle_manager.get_message_event_type(
message_id=self._message_id
)
yield self._message_cycle_manager.message_to_stream_response(
answer=cast(str, delta_text),
message_id=self._message_id,
event_type=self._precomputed_event_type,
)
else:
yield self._agent_message_to_stream_response(
answer=cast(str, delta_text),
message_id=self._message_id,
)
elif isinstance(event, QueueMessageReplaceEvent):
yield self._message_cycle_manager.message_replace_to_stream_response(answer=event.text)
elif isinstance(event, QueuePingEvent):
yield self.ping_stream_response()
else:
continue
if publisher: if publisher:
publisher.publish(None) publisher.publish(None)
if self._conversation_name_generate_thread: if self._conversation_name_generate_thread: