mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-20 11:26:07 +00:00
style: restrict line-length
This commit is contained in:
+15
-47
@@ -79,26 +79,20 @@ class RuntimePipeline:
|
||||
query.pipeline_config = self.pipeline_entity.config
|
||||
await self.process_query(query)
|
||||
|
||||
async def _check_output(
|
||||
self, query: entities.Query, result: pipeline_entities.StageProcessResult
|
||||
):
|
||||
async def _check_output(self, query: entities.Query, result: pipeline_entities.StageProcessResult):
|
||||
"""检查输出"""
|
||||
if result.user_notice:
|
||||
# 处理str类型
|
||||
|
||||
if isinstance(result.user_notice, str):
|
||||
result.user_notice = platform_message.MessageChain(
|
||||
platform_message.Plain(result.user_notice)
|
||||
)
|
||||
result.user_notice = platform_message.MessageChain(platform_message.Plain(result.user_notice))
|
||||
elif isinstance(result.user_notice, list):
|
||||
result.user_notice = platform_message.MessageChain(*result.user_notice)
|
||||
|
||||
if query.pipeline_config['output']['misc']['at-sender'] and isinstance(
|
||||
query.message_event, platform_events.GroupMessage
|
||||
):
|
||||
result.user_notice.insert(
|
||||
0, platform_message.At(query.message_event.sender.id)
|
||||
)
|
||||
result.user_notice.insert(0, platform_message.At(query.message_event.sender.id))
|
||||
|
||||
await query.adapter.reply_message(
|
||||
message_source=query.message_event,
|
||||
@@ -150,37 +144,25 @@ class RuntimePipeline:
|
||||
result = await result
|
||||
|
||||
if isinstance(result, pipeline_entities.StageProcessResult): # 直接返回结果
|
||||
self.ap.logger.debug(
|
||||
f'Stage {stage_container.inst_name} processed query {query} res {result}'
|
||||
)
|
||||
self.ap.logger.debug(f'Stage {stage_container.inst_name} processed query {query} res {result}')
|
||||
await self._check_output(query, result)
|
||||
|
||||
if result.result_type == pipeline_entities.ResultType.INTERRUPT:
|
||||
self.ap.logger.debug(
|
||||
f'Stage {stage_container.inst_name} interrupted query {query}'
|
||||
)
|
||||
self.ap.logger.debug(f'Stage {stage_container.inst_name} interrupted query {query}')
|
||||
break
|
||||
elif result.result_type == pipeline_entities.ResultType.CONTINUE:
|
||||
query = result.new_query
|
||||
elif isinstance(result, typing.AsyncGenerator): # 生成器
|
||||
self.ap.logger.debug(
|
||||
f'Stage {stage_container.inst_name} processed query {query} gen'
|
||||
)
|
||||
self.ap.logger.debug(f'Stage {stage_container.inst_name} processed query {query} gen')
|
||||
|
||||
async for sub_result in result:
|
||||
self.ap.logger.debug(
|
||||
f'Stage {stage_container.inst_name} processed query {query} res {sub_result}'
|
||||
)
|
||||
self.ap.logger.debug(f'Stage {stage_container.inst_name} processed query {query} res {sub_result}')
|
||||
await self._check_output(query, sub_result)
|
||||
|
||||
if sub_result.result_type == pipeline_entities.ResultType.INTERRUPT:
|
||||
self.ap.logger.debug(
|
||||
f'Stage {stage_container.inst_name} interrupted query {query}'
|
||||
)
|
||||
self.ap.logger.debug(f'Stage {stage_container.inst_name} interrupted query {query}')
|
||||
break
|
||||
elif (
|
||||
sub_result.result_type == pipeline_entities.ResultType.CONTINUE
|
||||
):
|
||||
elif sub_result.result_type == pipeline_entities.ResultType.CONTINUE:
|
||||
query = sub_result.new_query
|
||||
await self._execute_from_stage(i + 1, query)
|
||||
break
|
||||
@@ -214,12 +196,8 @@ class RuntimePipeline:
|
||||
|
||||
await self._execute_from_stage(0, query)
|
||||
except Exception as e:
|
||||
inst_name = (
|
||||
query.current_stage.inst_name if query.current_stage else 'unknown'
|
||||
)
|
||||
self.ap.logger.error(
|
||||
f'处理请求时出错 query_id={query.query_id} stage={inst_name} : {e}'
|
||||
)
|
||||
inst_name = query.current_stage.inst_name if query.current_stage else 'unknown'
|
||||
self.ap.logger.error(f'处理请求时出错 query_id={query.query_id} stage={inst_name} : {e}')
|
||||
self.ap.logger.debug(f'Traceback: {traceback.format_exc()}')
|
||||
finally:
|
||||
self.ap.logger.debug(f'Query {query} processed')
|
||||
@@ -241,18 +219,14 @@ class PipelineManager:
|
||||
self.pipelines = []
|
||||
|
||||
async def initialize(self):
|
||||
self.stage_dict = {
|
||||
name: cls for name, cls in stage.preregistered_stages.items()
|
||||
}
|
||||
self.stage_dict = {name: cls for name, cls in stage.preregistered_stages.items()}
|
||||
|
||||
await self.load_pipelines_from_db()
|
||||
|
||||
async def load_pipelines_from_db(self):
|
||||
self.ap.logger.info('Loading pipelines from db...')
|
||||
|
||||
result = await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.select(persistence_pipeline.LegacyPipeline)
|
||||
)
|
||||
result = await self.ap.persistence_mgr.execute_async(sqlalchemy.select(persistence_pipeline.LegacyPipeline))
|
||||
|
||||
pipelines = result.all()
|
||||
|
||||
@@ -267,20 +241,14 @@ class PipelineManager:
|
||||
| dict,
|
||||
):
|
||||
if isinstance(pipeline_entity, sqlalchemy.Row):
|
||||
pipeline_entity = persistence_pipeline.LegacyPipeline(
|
||||
**pipeline_entity._mapping
|
||||
)
|
||||
pipeline_entity = persistence_pipeline.LegacyPipeline(**pipeline_entity._mapping)
|
||||
elif isinstance(pipeline_entity, dict):
|
||||
pipeline_entity = persistence_pipeline.LegacyPipeline(**pipeline_entity)
|
||||
|
||||
# initialize stage containers according to pipeline_entity.stages
|
||||
stage_containers: list[StageInstContainer] = []
|
||||
for stage_name in pipeline_entity.stages:
|
||||
stage_containers.append(
|
||||
StageInstContainer(
|
||||
inst_name=stage_name, inst=self.stage_dict[stage_name](self.ap)
|
||||
)
|
||||
)
|
||||
stage_containers.append(StageInstContainer(inst_name=stage_name, inst=self.stage_dict[stage_name](self.ap)))
|
||||
|
||||
for stage_container in stage_containers:
|
||||
await stage_container.inst.initialize(pipeline_entity.config)
|
||||
|
||||
Reference in New Issue
Block a user