Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
conversation_variables.py61 linesDownload Raw Back to app
1from flask_restful import Resource, marshal_with, reqparse2from sqlalchemy import select3from sqlalchemy.orm import Session4 5from controllers.console import api6from controllers.console.app.wraps import get_app_model7from controllers.console.wraps import account_initialization_required, setup_required8from extensions.ext_database import db9from fields.conversation_variable_fields import paginated_conversation_variable_fields10from libs.login import login_required11from models import ConversationVariable12from models.model import AppMode13 14 15class ConversationVariablesApi(Resource):16    @setup_required17    @login_required18    @account_initialization_required19    @get_app_model(mode=AppMode.ADVANCED_CHAT)20    @marshal_with(paginated_conversation_variable_fields)21    def get(self, app_model):22        parser = reqparse.RequestParser()23        parser.add_argument("conversation_id", type=str, location="args")24        args = parser.parse_args()25 26        stmt = (27            select(ConversationVariable)28            .where(ConversationVariable.app_id == app_model.id)29            .order_by(ConversationVariable.created_at)30        )31        if args["conversation_id"]:32            stmt = stmt.where(ConversationVariable.conversation_id == args["conversation_id"])33        else:34            raise ValueError("conversation_id is required")35 36        # NOTE: This is a temporary solution to avoid performance issues.37        page = 138        page_size = 10039        stmt = stmt.limit(page_size).offset((page - 1) * page_size)40 41        with Session(db.engine) as session:42            rows = session.scalars(stmt).all()43 44        return {45            "page": page,46            "limit": page_size,47            "total": len(rows),48            "has_more": False,49            "data": [50                {51                    "created_at": row.created_at,52                    "updated_at": row.updated_at,53                    **row.to_variable().model_dump(),54                }55                for row in rows56            ],57        }58 59 60api.add_resource(ConversationVariablesApi, "/apps/<uuid:app_id>/conversation-variables")61