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,9 +140,10 @@ class EasyUIBasedGenerateTaskPipeline(BasedGenerateTaskPipeline):
:return: :return:
""" """
for stream_response in generator: for stream_response in generator:
if isinstance(stream_response, ErrorStreamResponse): match stream_response:
case ErrorStreamResponse():
raise stream_response.err raise stream_response.err
elif isinstance(stream_response, MessageEndStreamResponse): case MessageEndStreamResponse():
extras = {"usage": self._task_state.llm_result.usage.model_dump()} extras = {"usage": self._task_state.llm_result.usage.model_dump()}
if self._task_state.metadata: if self._task_state.metadata:
extras["metadata"] = self._task_state.metadata.model_dump() extras["metadata"] = self._task_state.metadata.model_dump()
@ -174,7 +175,7 @@ class EasyUIBasedGenerateTaskPipeline(BasedGenerateTaskPipeline):
) )
return response return response
else: case _:
continue continue
raise RuntimeError("queue listening stopped unexpectedly.") raise RuntimeError("queue listening stopped unexpectedly.")
@ -265,12 +266,13 @@ class EasyUIBasedGenerateTaskPipeline(BasedGenerateTaskPipeline):
publisher.publish(message) publisher.publish(message)
event = message.event event = message.event
if isinstance(event, QueueErrorEvent): match event:
case QueueErrorEvent():
with sessionmaker(bind=db.engine).begin() as session: with sessionmaker(bind=db.engine).begin() as session:
err = self.handle_error(event=event, session=session, message_id=self._message_id) err = self.handle_error(event=event, session=session, message_id=self._message_id)
yield self.error_to_stream_response(err) yield self.error_to_stream_response(err)
break break
elif isinstance(event, QueueStopEvent | QueueMessageEndEvent): case QueueStopEvent() | QueueMessageEndEvent():
if isinstance(event, QueueMessageEndEvent): if isinstance(event, QueueMessageEndEvent):
if event.llm_result: if event.llm_result:
self._task_state.llm_result = event.llm_result self._task_state.llm_result = event.llm_result
@ -292,21 +294,21 @@ class EasyUIBasedGenerateTaskPipeline(BasedGenerateTaskPipeline):
self._save_message(session=session, trace_manager=trace_manager) self._save_message(session=session, trace_manager=trace_manager)
message_end_resp = self._message_end_to_stream_response() message_end_resp = self._message_end_to_stream_response()
yield message_end_resp yield message_end_resp
elif isinstance(event, QueueRetrieverResourcesEvent): case QueueRetrieverResourcesEvent():
self._message_cycle_manager.handle_retriever_resources(event) self._message_cycle_manager.handle_retriever_resources(event)
elif isinstance(event, QueueAnnotationReplyEvent): case QueueAnnotationReplyEvent():
annotation = self._message_cycle_manager.handle_annotation_reply(event) annotation = self._message_cycle_manager.handle_annotation_reply(event)
if annotation: if annotation:
self._task_state.llm_result.message.content = annotation.content self._task_state.llm_result.message.content = annotation.content
elif isinstance(event, QueueAgentThoughtEvent): case QueueAgentThoughtEvent():
agent_thought_response = self._agent_thought_to_stream_response(event) agent_thought_response = self._agent_thought_to_stream_response(event)
if agent_thought_response is not None: if agent_thought_response is not None:
yield agent_thought_response yield agent_thought_response
elif isinstance(event, QueueMessageFileEvent): case QueueMessageFileEvent():
response = self._message_cycle_manager.message_file_to_stream_response(event) response = self._message_cycle_manager.message_file_to_stream_response(event)
if response: if response:
yield response yield response
elif isinstance(event, QueueLLMChunkEvent | QueueAgentMessageEvent): case QueueLLMChunkEvent() | QueueAgentMessageEvent():
chunk = event.chunk chunk = event.chunk
delta_text = chunk.delta.message.content delta_text = chunk.delta.message.content
if delta_text is None: if delta_text is None:
@ -317,11 +319,12 @@ class EasyUIBasedGenerateTaskPipeline(BasedGenerateTaskPipeline):
logger.debug( logger.debug(
"The content type %s in LLM chunk delta message content.: %r", type(content), content "The content type %s in LLM chunk delta message content.: %r", type(content), content
) )
if isinstance(content, TextPromptMessageContent): match content:
case TextPromptMessageContent():
delta_text += content.data delta_text += content.data
elif isinstance(content, str): case str():
delta_text += content # failback to str delta_text += content # failback to str
else: case _:
logger.warning( logger.warning(
"Unsupported content type %s in LLM chunk delta message content.: %r", "Unsupported content type %s in LLM chunk delta message content.: %r",
type(content), type(content),
@ -341,7 +344,8 @@ class EasyUIBasedGenerateTaskPipeline(BasedGenerateTaskPipeline):
current_content += cast(str, delta_text) current_content += cast(str, delta_text)
self._task_state.llm_result.message.content = current_content self._task_state.llm_result.message.content = current_content
if isinstance(event, QueueLLMChunkEvent): match event:
case QueueLLMChunkEvent():
# Determine the event type once, on first LLM chunk, and reuse for subsequent chunks # 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: 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( self._precomputed_event_type = self._message_cycle_manager.get_message_event_type(
@ -352,16 +356,16 @@ class EasyUIBasedGenerateTaskPipeline(BasedGenerateTaskPipeline):
message_id=self._message_id, message_id=self._message_id,
event_type=self._precomputed_event_type, event_type=self._precomputed_event_type,
) )
else: case _:
yield self._agent_message_to_stream_response( yield self._agent_message_to_stream_response(
answer=cast(str, delta_text), answer=cast(str, delta_text),
message_id=self._message_id, message_id=self._message_id,
) )
elif isinstance(event, QueueMessageReplaceEvent): case QueueMessageReplaceEvent():
yield self._message_cycle_manager.message_replace_to_stream_response(answer=event.text) yield self._message_cycle_manager.message_replace_to_stream_response(answer=event.text)
elif isinstance(event, QueuePingEvent): case QueuePingEvent():
yield self.ping_stream_response() yield self.ping_stream_response()
else: case _:
continue continue
if publisher: if publisher:
publisher.publish(None) publisher.publish(None)