def _escape(value): if value is None: return None return str(value).replace("'", "''") async def chat_session_list(ns={}): """当前用户的对话会话列表(左侧栏历史)""" userid = ns.get('userid') or await get_user() if not userid: return {'status': False, 'msg': '未找到用户'} page_size = int(ns.get('page_size', 50)) current_page = int(ns.get('current_page', 1)) offset = (current_page - 1) * page_size db = DBPools() async with db.sqlorContext('kboss') as sor: try: count_sql = """ SELECT COUNT(*) AS total_count FROM chat_session WHERE userid = '%s'; """ % _escape(userid) total = (await sor.sqlExe(count_sql, {}))[0]['total_count'] find_sql = """ SELECT id, model, title, created_at, updated_at FROM chat_session WHERE userid = '%s' ORDER BY updated_at DESC LIMIT %s OFFSET %s; """ % (_escape(userid), page_size, offset) sessions = await sor.sqlExe(find_sql, {}) return { 'status': True, 'msg': 'list success', 'data': { 'total_count': total, 'page_size': page_size, 'current_page': current_page, 'sessions': sessions, }, } except Exception as e: return {'status': False, 'msg': 'list failed, %s' % str(e)} ret = await chat_session_list(params_kw) return ret