harnessed_reasoning/wwwroot/api/sessions_list.dspy

36 lines
1.1 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Reasoning sessions list API"""
import json
result = {'success': False, 'rows': [], 'total': 0}
try:
dbname = get_module_dbname('harnessed_reasoning')
ns = {
'page': int(params_kw.get('page', 1)),
'rows': int(params_kw.get('rows', 20)),
'sort': 'created_at desc'
}
user_id = await get_user()
sql = """SELECT id, initial_request, context_summary, status, created_at, updated_at
FROM harnessed_reasoning_sessions
WHERE user_id = ${user_id}$
ORDER BY created_at DESC"""
async with DBPools().sqlorContext(dbname) as sor:
data = await sor.sqlExe(sql, {'user_id': user_id})
if isinstance(data, dict):
result['total'] = data.get('total', 0)
result['rows'] = [dict(r) for r in data.get('rows', [])]
else:
result['rows'] = [dict(r) for r in (data or [])]
result['total'] = len(result['rows'])
result['success'] = True
except Exception as e:
result['error'] = str(e)
return json.dumps(result, ensure_ascii=False, default=str)