mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-09 07:16:04 +00:00
feat: pipeline model crud
This commit is contained in:
48
pkg/api/http/controller/groups/pipelines.py
Normal file
48
pkg/api/http/controller/groups/pipelines.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import quart
|
||||
|
||||
from .. import group
|
||||
from .....entity.persistence import pipeline
|
||||
|
||||
|
||||
@group.group_class('pipelines', '/api/v1/pipelines')
|
||||
class PipelinesRouterGroup(group.RouterGroup):
|
||||
|
||||
async def initialize(self) -> None:
|
||||
|
||||
@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()
|
||||
})
|
||||
elif quart.request.method == 'POST':
|
||||
json_data = await quart.request.json
|
||||
|
||||
await self.ap.pipeline_service.create_pipeline(json_data)
|
||||
|
||||
return self.success()
|
||||
|
||||
@self.route('/_/metadata', methods=['GET'])
|
||||
async def _() -> str:
|
||||
return self.success(data={
|
||||
'configs': await self.ap.pipeline_service.get_pipeline_metadata()
|
||||
})
|
||||
|
||||
@self.route('/<pipeline_uuid>', methods=['GET', 'DELETE'])
|
||||
async def _(pipeline_uuid: str) -> str:
|
||||
if quart.request.method == 'GET':
|
||||
pipeline = await self.ap.pipeline_service.get_pipeline(pipeline_uuid)
|
||||
|
||||
if pipeline is None:
|
||||
return self.http_status(404, -1, 'pipeline not found')
|
||||
|
||||
return self.success(data={
|
||||
'pipeline': pipeline
|
||||
})
|
||||
elif quart.request.method == 'DELETE':
|
||||
await self.ap.pipeline_service.delete_pipeline(pipeline_uuid)
|
||||
|
||||
return self.success()
|
||||
|
||||
@@ -8,7 +8,7 @@ import quart_cors
|
||||
|
||||
from ....core import app, entities as core_entities
|
||||
|
||||
from .groups import logs, system, settings, plugins, stats, user
|
||||
from .groups import logs, system, settings, plugins, stats, user, pipelines
|
||||
from .groups.provider import models, requesters
|
||||
from . import group
|
||||
|
||||
|
||||
65
pkg/api/http/service/pipeline.py
Normal file
65
pkg/api/http/service/pipeline.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
import datetime
|
||||
import sqlalchemy
|
||||
|
||||
from ....core import app
|
||||
from ....pipeline import stagemgr
|
||||
from ....entity.persistence import pipeline as persistence_pipeline
|
||||
|
||||
|
||||
class PipelineService:
|
||||
ap: app.Application
|
||||
|
||||
def __init__(self, ap: app.Application) -> None:
|
||||
self.ap = ap
|
||||
|
||||
async def get_pipeline_metadata(self) -> dict:
|
||||
return [
|
||||
self.ap.pipeline_config_meta_trigger.data,
|
||||
self.ap.pipeline_config_meta_safety.data,
|
||||
self.ap.pipeline_config_meta_ai.data,
|
||||
self.ap.pipeline_config_meta_output.data
|
||||
]
|
||||
|
||||
async def get_pipelines(self) -> list[dict]:
|
||||
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)
|
||||
for pipeline in pipelines
|
||||
]
|
||||
|
||||
async def get_pipeline(self, pipeline_uuid: str) -> dict | None:
|
||||
result = await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.select(persistence_pipeline.LegacyPipeline).where(persistence_pipeline.LegacyPipeline.uuid == pipeline_uuid)
|
||||
)
|
||||
|
||||
pipeline = result.first()
|
||||
|
||||
if pipeline is None:
|
||||
return None
|
||||
|
||||
return self.ap.persistence_mgr.serialize_model(persistence_pipeline.LegacyPipeline, pipeline)
|
||||
|
||||
async def create_pipeline(self, pipeline_data: dict) -> None:
|
||||
pipeline_data['uuid'] = str(uuid.uuid4())
|
||||
pipeline_data['for_version'] = self.ap.ver_mgr.get_current_version()
|
||||
pipeline_data['stages'] = stagemgr.stage_order.copy()
|
||||
|
||||
# TODO: 检查pipeline config是否完整
|
||||
|
||||
await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.insert(persistence_pipeline.LegacyPipeline).values(**pipeline_data)
|
||||
)
|
||||
# TODO: 更新到pipeline manager
|
||||
|
||||
async def delete_pipeline(self, pipeline_uuid: str) -> None:
|
||||
await self.ap.persistence_mgr.execute_async(
|
||||
sqlalchemy.delete(persistence_pipeline.LegacyPipeline).where(persistence_pipeline.LegacyPipeline.uuid == pipeline_uuid)
|
||||
)
|
||||
# TODO: 更新到pipeline manager
|
||||
Reference in New Issue
Block a user