mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-05 05:16:03 +00:00
style: restrict line-length
This commit is contained in:
@@ -65,9 +65,7 @@ class RouterGroup(abc.ABC):
|
||||
async def handler_error(*args, **kwargs):
|
||||
if auth_type == AuthType.USER_TOKEN:
|
||||
# 从Authorization头中获取token
|
||||
token = quart.request.headers.get('Authorization', '').replace(
|
||||
'Bearer ', ''
|
||||
)
|
||||
token = quart.request.headers.get('Authorization', '').replace('Bearer ', '')
|
||||
|
||||
if not token:
|
||||
return self.http_status(401, -1, '未提供有效的用户令牌')
|
||||
|
||||
@@ -14,10 +14,8 @@ class LogsRouterGroup(group.RouterGroup):
|
||||
start_page_number = int(quart.request.args.get('start_page_number', 0))
|
||||
start_offset = int(quart.request.args.get('start_offset', 0))
|
||||
|
||||
logs_str, end_page_number, end_offset = (
|
||||
self.ap.log_cache.get_log_by_pointer(
|
||||
start_page_number=start_page_number, start_offset=start_offset
|
||||
)
|
||||
logs_str, end_page_number, end_offset = self.ap.log_cache.get_log_by_pointer(
|
||||
start_page_number=start_page_number, start_offset=start_offset
|
||||
)
|
||||
|
||||
return self.success(
|
||||
|
||||
@@ -11,23 +11,17 @@ class PipelinesRouterGroup(group.RouterGroup):
|
||||
@self.route('', methods=['GET', 'POST'])
|
||||
async def _() -> str:
|
||||
if quart.request.method == 'GET':
|
||||
return self.success(
|
||||
data={'pipelines': await self.ap.pipeline_service.get_pipelines()}
|
||||
)
|
||||
return self.success(data={'pipelines': await self.ap.pipeline_service.get_pipelines()})
|
||||
elif quart.request.method == 'POST':
|
||||
json_data = await quart.request.json
|
||||
|
||||
pipeline_uuid = await self.ap.pipeline_service.create_pipeline(
|
||||
json_data
|
||||
)
|
||||
pipeline_uuid = await self.ap.pipeline_service.create_pipeline(json_data)
|
||||
|
||||
return self.success(data={'uuid': pipeline_uuid})
|
||||
|
||||
@self.route('/_/metadata', methods=['GET'])
|
||||
async def _() -> str:
|
||||
return self.success(
|
||||
data={'configs': await self.ap.pipeline_service.get_pipeline_metadata()}
|
||||
)
|
||||
return self.success(data={'configs': await self.ap.pipeline_service.get_pipeline_metadata()})
|
||||
|
||||
@self.route('/<pipeline_uuid>', methods=['GET', 'PUT', 'DELETE'])
|
||||
async def _(pipeline_uuid: str) -> str:
|
||||
|
||||
@@ -8,30 +8,20 @@ class AdaptersRouterGroup(group.RouterGroup):
|
||||
async def initialize(self) -> None:
|
||||
@self.route('', methods=['GET'])
|
||||
async def _() -> str:
|
||||
return self.success(
|
||||
data={'adapters': self.ap.platform_mgr.get_available_adapters_info()}
|
||||
)
|
||||
return self.success(data={'adapters': self.ap.platform_mgr.get_available_adapters_info()})
|
||||
|
||||
@self.route('/<adapter_name>', methods=['GET'])
|
||||
async def _(adapter_name: str) -> str:
|
||||
adapter_info = self.ap.platform_mgr.get_available_adapter_info_by_name(
|
||||
adapter_name
|
||||
)
|
||||
adapter_info = self.ap.platform_mgr.get_available_adapter_info_by_name(adapter_name)
|
||||
|
||||
if adapter_info is None:
|
||||
return self.http_status(404, -1, 'adapter not found')
|
||||
|
||||
return self.success(data={'adapter': adapter_info})
|
||||
|
||||
@self.route(
|
||||
'/<adapter_name>/icon', methods=['GET'], auth_type=group.AuthType.NONE
|
||||
)
|
||||
@self.route('/<adapter_name>/icon', methods=['GET'], auth_type=group.AuthType.NONE)
|
||||
async def _(adapter_name: str) -> quart.Response:
|
||||
adapter_manifest = (
|
||||
self.ap.platform_mgr.get_available_adapter_manifest_by_name(
|
||||
adapter_name
|
||||
)
|
||||
)
|
||||
adapter_manifest = self.ap.platform_mgr.get_available_adapter_manifest_by_name(adapter_name)
|
||||
|
||||
if adapter_manifest is None:
|
||||
return self.http_status(404, -1, 'adapter not found')
|
||||
|
||||
@@ -92,9 +92,7 @@ class PluginsRouterGroup(group.RouterGroup):
|
||||
await self.ap.plugin_mgr.reorder_plugins(data.get('plugins'))
|
||||
return self.success()
|
||||
|
||||
@self.route(
|
||||
'/install/github', methods=['POST'], auth_type=group.AuthType.USER_TOKEN
|
||||
)
|
||||
@self.route('/install/github', methods=['POST'], auth_type=group.AuthType.USER_TOKEN)
|
||||
async def _() -> str:
|
||||
data = await quart.request.json
|
||||
|
||||
|
||||
@@ -9,9 +9,7 @@ class LLMModelsRouterGroup(group.RouterGroup):
|
||||
@self.route('', methods=['GET', 'POST'])
|
||||
async def _() -> str:
|
||||
if quart.request.method == 'GET':
|
||||
return self.success(
|
||||
data={'models': await self.ap.model_service.get_llm_models()}
|
||||
)
|
||||
return self.success(data={'models': await self.ap.model_service.get_llm_models()})
|
||||
elif quart.request.method == 'POST':
|
||||
json_data = await quart.request.json
|
||||
|
||||
|
||||
@@ -8,30 +8,20 @@ class RequestersRouterGroup(group.RouterGroup):
|
||||
async def initialize(self) -> None:
|
||||
@self.route('', methods=['GET'])
|
||||
async def _() -> quart.Response:
|
||||
return self.success(
|
||||
data={'requesters': self.ap.model_mgr.get_available_requesters_info()}
|
||||
)
|
||||
return self.success(data={'requesters': self.ap.model_mgr.get_available_requesters_info()})
|
||||
|
||||
@self.route('/<requester_name>', methods=['GET'])
|
||||
async def _(requester_name: str) -> quart.Response:
|
||||
requester_info = self.ap.model_mgr.get_available_requester_info_by_name(
|
||||
requester_name
|
||||
)
|
||||
requester_info = self.ap.model_mgr.get_available_requester_info_by_name(requester_name)
|
||||
|
||||
if requester_info is None:
|
||||
return self.http_status(404, -1, 'requester not found')
|
||||
|
||||
return self.success(data={'requester': requester_info})
|
||||
|
||||
@self.route(
|
||||
'/<requester_name>/icon', methods=['GET'], auth_type=group.AuthType.NONE
|
||||
)
|
||||
@self.route('/<requester_name>/icon', methods=['GET'], auth_type=group.AuthType.NONE)
|
||||
async def _(requester_name: str) -> quart.Response:
|
||||
requester_manifest = (
|
||||
self.ap.model_mgr.get_available_requester_manifest_by_name(
|
||||
requester_name
|
||||
)
|
||||
)
|
||||
requester_manifest = self.ap.model_mgr.get_available_requester_manifest_by_name(requester_name)
|
||||
|
||||
if requester_manifest is None:
|
||||
return self.http_status(404, -1, 'requester not found')
|
||||
|
||||
@@ -8,9 +8,7 @@ class StatsRouterGroup(group.RouterGroup):
|
||||
async def _() -> str:
|
||||
conv_count = 0
|
||||
for session in self.ap.sess_mgr.session_list:
|
||||
conv_count += len(
|
||||
session.conversations if session.conversations is not None else []
|
||||
)
|
||||
conv_count += len(session.conversations if session.conversations is not None else [])
|
||||
|
||||
return self.success(
|
||||
data={
|
||||
|
||||
@@ -13,9 +13,7 @@ class SystemRouterGroup(group.RouterGroup):
|
||||
data={
|
||||
'version': constants.semantic_version,
|
||||
'debug': constants.debug_mode,
|
||||
'enabled_platform_count': len(
|
||||
self.ap.platform_mgr.get_running_adapters()
|
||||
),
|
||||
'enabled_platform_count': len(self.ap.platform_mgr.get_running_adapters()),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -28,9 +26,7 @@ class SystemRouterGroup(group.RouterGroup):
|
||||
|
||||
return self.success(data=self.ap.task_mgr.get_tasks_dict(task_type))
|
||||
|
||||
@self.route(
|
||||
'/tasks/<task_id>', methods=['GET'], auth_type=group.AuthType.USER_TOKEN
|
||||
)
|
||||
@self.route('/tasks/<task_id>', methods=['GET'], auth_type=group.AuthType.USER_TOKEN)
|
||||
async def _(task_id: str) -> str:
|
||||
task = self.ap.task_mgr.get_task_by_id(int(task_id))
|
||||
|
||||
@@ -48,9 +44,7 @@ class SystemRouterGroup(group.RouterGroup):
|
||||
await self.ap.reload(scope=scope)
|
||||
return self.success()
|
||||
|
||||
@self.route(
|
||||
'/_debug/exec', methods=['POST'], auth_type=group.AuthType.USER_TOKEN
|
||||
)
|
||||
@self.route('/_debug/exec', methods=['POST'], auth_type=group.AuthType.USER_TOKEN)
|
||||
async def _() -> str:
|
||||
if not constants.debug_mode:
|
||||
return self.http_status(403, 403, 'Forbidden')
|
||||
|
||||
@@ -10,9 +10,7 @@ class UserRouterGroup(group.RouterGroup):
|
||||
@self.route('/init', methods=['GET', 'POST'], auth_type=group.AuthType.NONE)
|
||||
async def _() -> str:
|
||||
if quart.request.method == 'GET':
|
||||
return self.success(
|
||||
data={'initialized': await self.ap.user_service.is_initialized()}
|
||||
)
|
||||
return self.success(data={'initialized': await self.ap.user_service.is_initialized()})
|
||||
|
||||
if await self.ap.user_service.is_initialized():
|
||||
return self.fail(1, '系统已初始化')
|
||||
@@ -31,17 +29,13 @@ class UserRouterGroup(group.RouterGroup):
|
||||
json_data = await quart.request.json
|
||||
|
||||
try:
|
||||
token = await self.ap.user_service.authenticate(
|
||||
json_data['user'], json_data['password']
|
||||
)
|
||||
token = await self.ap.user_service.authenticate(json_data['user'], json_data['password'])
|
||||
except argon2.exceptions.VerifyMismatchError:
|
||||
return self.fail(1, '用户名或密码错误')
|
||||
|
||||
return self.success(data={'token': token})
|
||||
|
||||
@self.route(
|
||||
'/check-token', methods=['GET'], auth_type=group.AuthType.USER_TOKEN
|
||||
)
|
||||
@self.route('/check-token', methods=['GET'], auth_type=group.AuthType.USER_TOKEN)
|
||||
async def _(user_email: str) -> str:
|
||||
token = await self.ap.user_service.generate_jwt_token(user_email)
|
||||
|
||||
|
||||
@@ -70,15 +70,12 @@ class HTTPController:
|
||||
|
||||
@self.quart_app.route('/')
|
||||
async def index():
|
||||
return await quart.send_from_directory(
|
||||
frontend_path, 'index.html', mimetype='text/html'
|
||||
)
|
||||
return await quart.send_from_directory(frontend_path, 'index.html', mimetype='text/html')
|
||||
|
||||
@self.quart_app.route('/<path:path>')
|
||||
async def static_file(path: str):
|
||||
if not (
|
||||
os.path.exists(os.path.join(frontend_path, path))
|
||||
and os.path.isfile(os.path.join(frontend_path, path))
|
||||
os.path.exists(os.path.join(frontend_path, path)) and os.path.isfile(os.path.join(frontend_path, path))
|
||||
):
|
||||
if os.path.exists(os.path.join(frontend_path, path + '.html')):
|
||||
path += '.html'
|
||||
@@ -110,6 +107,4 @@ class HTTPController:
|
||||
elif path.endswith('.txt'):
|
||||
mimetype = 'text/plain'
|
||||
|
||||
return await quart.send_from_directory(
|
||||
frontend_path, path, mimetype=mimetype
|
||||
)
|
||||
return await quart.send_from_directory(frontend_path, path, mimetype=mimetype)
|
||||
|
||||
@@ -18,23 +18,16 @@ class BotService:
|
||||
|
||||
async def get_bots(self) -> list[dict]:
|
||||
"""获取所有机器人"""
|
||||
result = await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.select(persistence_bot.Bot)
|
||||
)
|
||||
result = await self.ap.persistence_mgr.execute_async(sqlalchemy.select(persistence_bot.Bot))
|
||||
|
||||
bots = result.all()
|
||||
|
||||
return [
|
||||
self.ap.persistence_mgr.serialize_model(persistence_bot.Bot, bot)
|
||||
for bot in bots
|
||||
]
|
||||
return [self.ap.persistence_mgr.serialize_model(persistence_bot.Bot, bot) for bot in bots]
|
||||
|
||||
async def get_bot(self, bot_uuid: str) -> dict | None:
|
||||
"""获取机器人"""
|
||||
result = await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.select(persistence_bot.Bot).where(
|
||||
persistence_bot.Bot.uuid == bot_uuid
|
||||
)
|
||||
sqlalchemy.select(persistence_bot.Bot).where(persistence_bot.Bot.uuid == bot_uuid)
|
||||
)
|
||||
|
||||
bot = result.first()
|
||||
@@ -60,9 +53,7 @@ class BotService:
|
||||
bot_data['use_pipeline_uuid'] = pipeline.uuid
|
||||
bot_data['use_pipeline_name'] = pipeline.name
|
||||
|
||||
await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.insert(persistence_bot.Bot).values(bot_data)
|
||||
)
|
||||
await self.ap.persistence_mgr.execute_async(sqlalchemy.insert(persistence_bot.Bot).values(bot_data))
|
||||
|
||||
bot = await self.get_bot(bot_data['uuid'])
|
||||
|
||||
@@ -79,8 +70,7 @@ class BotService:
|
||||
if 'use_pipeline_uuid' in bot_data:
|
||||
result = await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.select(persistence_pipeline.LegacyPipeline).where(
|
||||
persistence_pipeline.LegacyPipeline.uuid
|
||||
== bot_data['use_pipeline_uuid']
|
||||
persistence_pipeline.LegacyPipeline.uuid == bot_data['use_pipeline_uuid']
|
||||
)
|
||||
)
|
||||
pipeline = result.first()
|
||||
@@ -90,9 +80,7 @@ class BotService:
|
||||
raise Exception('Pipeline not found')
|
||||
|
||||
await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.update(persistence_bot.Bot)
|
||||
.values(bot_data)
|
||||
.where(persistence_bot.Bot.uuid == bot_uuid)
|
||||
sqlalchemy.update(persistence_bot.Bot).values(bot_data).where(persistence_bot.Bot.uuid == bot_uuid)
|
||||
)
|
||||
await self.ap.platform_mgr.remove_bot(bot_uuid)
|
||||
|
||||
@@ -108,7 +96,5 @@ class BotService:
|
||||
"""删除机器人"""
|
||||
await self.ap.platform_mgr.remove_bot(bot_uuid)
|
||||
await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.delete(persistence_bot.Bot).where(
|
||||
persistence_bot.Bot.uuid == bot_uuid
|
||||
)
|
||||
sqlalchemy.delete(persistence_bot.Bot).where(persistence_bot.Bot.uuid == bot_uuid)
|
||||
)
|
||||
|
||||
@@ -15,22 +15,15 @@ class ModelsService:
|
||||
self.ap = ap
|
||||
|
||||
async def get_llm_models(self) -> list[dict]:
|
||||
result = await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.select(persistence_model.LLMModel)
|
||||
)
|
||||
result = await self.ap.persistence_mgr.execute_async(sqlalchemy.select(persistence_model.LLMModel))
|
||||
|
||||
models = result.all()
|
||||
return [
|
||||
self.ap.persistence_mgr.serialize_model(persistence_model.LLMModel, model)
|
||||
for model in models
|
||||
]
|
||||
return [self.ap.persistence_mgr.serialize_model(persistence_model.LLMModel, model) for model in models]
|
||||
|
||||
async def create_llm_model(self, model_data: dict) -> str:
|
||||
model_data['uuid'] = str(uuid.uuid4())
|
||||
|
||||
await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.insert(persistence_model.LLMModel).values(**model_data)
|
||||
)
|
||||
await self.ap.persistence_mgr.execute_async(sqlalchemy.insert(persistence_model.LLMModel).values(**model_data))
|
||||
|
||||
llm_model = await self.get_llm_model(model_data['uuid'])
|
||||
|
||||
@@ -53,9 +46,7 @@ class ModelsService:
|
||||
|
||||
async def get_llm_model(self, model_uuid: str) -> dict | None:
|
||||
result = await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.select(persistence_model.LLMModel).where(
|
||||
persistence_model.LLMModel.uuid == model_uuid
|
||||
)
|
||||
sqlalchemy.select(persistence_model.LLMModel).where(persistence_model.LLMModel.uuid == model_uuid)
|
||||
)
|
||||
|
||||
model = result.first()
|
||||
@@ -63,9 +54,7 @@ class ModelsService:
|
||||
if model is None:
|
||||
return None
|
||||
|
||||
return self.ap.persistence_mgr.serialize_model(
|
||||
persistence_model.LLMModel, model
|
||||
)
|
||||
return self.ap.persistence_mgr.serialize_model(persistence_model.LLMModel, model)
|
||||
|
||||
async def update_llm_model(self, model_uuid: str, model_data: dict) -> None:
|
||||
if 'uuid' in model_data:
|
||||
@@ -85,9 +74,7 @@ class ModelsService:
|
||||
|
||||
async def delete_llm_model(self, model_uuid: str) -> None:
|
||||
await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.delete(persistence_model.LLMModel).where(
|
||||
persistence_model.LLMModel.uuid == model_uuid
|
||||
)
|
||||
sqlalchemy.delete(persistence_model.LLMModel).where(persistence_model.LLMModel.uuid == model_uuid)
|
||||
)
|
||||
|
||||
await self.ap.model_mgr.remove_llm_model(model_uuid)
|
||||
|
||||
@@ -39,15 +39,11 @@ class PipelineService:
|
||||
]
|
||||
|
||||
async def get_pipelines(self) -> list[dict]:
|
||||
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()
|
||||
return [
|
||||
self.ap.persistence_mgr.serialize_model(
|
||||
persistence_pipeline.LegacyPipeline, pipeline
|
||||
)
|
||||
self.ap.persistence_mgr.serialize_model(persistence_pipeline.LegacyPipeline, pipeline)
|
||||
for pipeline in pipelines
|
||||
]
|
||||
|
||||
@@ -63,23 +59,17 @@ class PipelineService:
|
||||
if pipeline is None:
|
||||
return None
|
||||
|
||||
return self.ap.persistence_mgr.serialize_model(
|
||||
persistence_pipeline.LegacyPipeline, pipeline
|
||||
)
|
||||
return self.ap.persistence_mgr.serialize_model(persistence_pipeline.LegacyPipeline, pipeline)
|
||||
|
||||
async def create_pipeline(self, pipeline_data: dict, default: bool = False) -> str:
|
||||
pipeline_data['uuid'] = str(uuid.uuid4())
|
||||
pipeline_data['for_version'] = self.ap.ver_mgr.get_current_version()
|
||||
pipeline_data['stages'] = default_stage_order.copy()
|
||||
pipeline_data['is_default'] = default
|
||||
pipeline_data['config'] = json.load(
|
||||
open('templates/default-pipeline-config.json', 'r', encoding='utf-8')
|
||||
)
|
||||
pipeline_data['config'] = json.load(open('templates/default-pipeline-config.json', 'r', encoding='utf-8'))
|
||||
|
||||
await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.insert(persistence_pipeline.LegacyPipeline).values(
|
||||
**pipeline_data
|
||||
)
|
||||
sqlalchemy.insert(persistence_pipeline.LegacyPipeline).values(**pipeline_data)
|
||||
)
|
||||
|
||||
pipeline = await self.get_pipeline(pipeline_data['uuid'])
|
||||
|
||||
@@ -17,9 +17,7 @@ class UserService:
|
||||
self.ap = ap
|
||||
|
||||
async def is_initialized(self) -> bool:
|
||||
result = await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.select(user.User).limit(1)
|
||||
)
|
||||
result = await self.ap.persistence_mgr.execute_async(sqlalchemy.select(user.User).limit(1))
|
||||
|
||||
result_list = result.all()
|
||||
return result_list is not None and len(result_list) > 0
|
||||
@@ -30,9 +28,7 @@ class UserService:
|
||||
hashed_password = ph.hash(password)
|
||||
|
||||
await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.insert(user.User).values(
|
||||
user=user_email, password=hashed_password
|
||||
)
|
||||
sqlalchemy.insert(user.User).values(user=user_email, password=hashed_password)
|
||||
)
|
||||
|
||||
async def get_user_by_email(self, user_email: str) -> user.User | None:
|
||||
@@ -41,9 +37,7 @@ class UserService:
|
||||
)
|
||||
|
||||
result_list = result.all()
|
||||
return (
|
||||
result_list[0] if result_list is not None and len(result_list) > 0 else None
|
||||
)
|
||||
return result_list[0] if result_list is not None and len(result_list) > 0 else None
|
||||
|
||||
async def authenticate(self, user_email: str, password: str) -> str | None:
|
||||
result = await self.ap.persistence_mgr.execute_async(
|
||||
|
||||
Reference in New Issue
Block a user