37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Session create API"""
|
|
import json, uuid, time
|
|
|
|
result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid request', 'type': 'error'}}
|
|
|
|
try:
|
|
dbname = get_module_dbname('harnessed_agent')
|
|
user_id = await get_user()
|
|
now = time.strftime('%Y-%m-%d %H:%M:%S')
|
|
new_id = str(uuid.uuid4()).replace('-', '')
|
|
|
|
title = params_kw.get('title', '').strip()
|
|
preview = params_kw.get('preview', '').strip()
|
|
tags = params_kw.get('tags', '').strip()
|
|
is_active = params_kw.get('is_active', '').strip()
|
|
metadata_json = params_kw.get('metadata_json', '').strip()
|
|
|
|
async with DBPools().sqlorContext(dbname) as sor:
|
|
await sor.sqlExe("""INSERT INTO hermes_sessions (id, user_id, title, preview, tags, is_active, metadata_json, created_at, updated_at)
|
|
VALUES (${id}$, ${user_id}$, ${title}$, ${preview}$, ${tags}$, ${is_active}$, ${metadata_json}$, ${created_at}$, ${updated_at}$)""", {
|
|
'id': new_id, 'user_id': user_id,
|
|
'title': title,
|
|
'preview': preview,
|
|
'tags': tags,
|
|
'is_active': is_active,
|
|
'metadata_json': metadata_json,
|
|
'created_at': now, 'updated_at': now
|
|
})
|
|
result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': 'Session创建成功', 'type': 'success'}}
|
|
|
|
except Exception as e:
|
|
result['options'] = {'title': 'Error', 'message': '创建失败: ' + str(e), 'type': 'error'}
|
|
|
|
return json.dumps(result, ensure_ascii=False)
|