feat: 用户对话数据同步到backend + 用户资料修改同步
This commit is contained in:
126
backend/app.py
126
backend/app.py
@@ -166,6 +166,20 @@ def init_db():
|
||||
)
|
||||
''')
|
||||
|
||||
# 对话表(用户对话数据)
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS conversations (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
agent_id TEXT,
|
||||
messages TEXT NOT NULL DEFAULT '[]',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
)
|
||||
''')
|
||||
|
||||
# 初始化默认大模型配置
|
||||
cursor.execute('SELECT COUNT(*) FROM llm_configs')
|
||||
if cursor.fetchone()[0] == 0:
|
||||
@@ -581,6 +595,118 @@ def change_user_password(id):
|
||||
return jsonify({'success': True})
|
||||
|
||||
|
||||
# ==================== 用户对话数据同步 ====================
|
||||
|
||||
@app.route('/api/user/<int:user_id>/conversations', methods=['GET'])
|
||||
def get_user_conversations(user_id):
|
||||
"""获取用户所有对话"""
|
||||
conn = get_db()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
SELECT id, title, agent_id, messages, created_at, updated_at
|
||||
FROM conversations WHERE user_id = ? ORDER BY updated_at DESC
|
||||
''', (user_id,))
|
||||
|
||||
conversations = []
|
||||
for row in cursor.fetchall():
|
||||
conv = dict(row)
|
||||
# 解析消息JSON
|
||||
try:
|
||||
conv['messages'] = json.loads(conv['messages']) if conv['messages'] else []
|
||||
except:
|
||||
conv['messages'] = []
|
||||
# 转换为前端格式(使用字符串ID)
|
||||
conv['id'] = str(conv['id'])
|
||||
conv['createdAt'] = int(datetime.strptime(conv['created_at'], '%Y-%m-%d %H:%M:%S').timestamp() * 1000) if conv['created_at'] else 0
|
||||
conv['updatedAt'] = int(datetime.strptime(conv['updated_at'], '%Y-%m-%d %H:%M:%S').timestamp() * 1000) if conv['updated_at'] else 0
|
||||
conv['agentId'] = conv['agent_id']
|
||||
conversations.append(conv)
|
||||
|
||||
conn.close()
|
||||
return jsonify(conversations)
|
||||
|
||||
|
||||
@app.route('/api/user/<int:user_id>/conversations', methods=['POST'])
|
||||
def create_user_conversation(user_id):
|
||||
"""创建新对话"""
|
||||
data = request.json
|
||||
title = data.get('title', '新对话')
|
||||
agent_id = data.get('agentId') or data.get('agent_id')
|
||||
messages = data.get('messages', [])
|
||||
|
||||
conn = get_db()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
INSERT INTO conversations (user_id, title, agent_id, messages)
|
||||
VALUES (?, ?, ?, ?)
|
||||
''', (user_id, title, agent_id, json.dumps(messages)))
|
||||
conn.commit()
|
||||
|
||||
conv_id = cursor.lastrowid
|
||||
conn.close()
|
||||
|
||||
return jsonify({'success': True, 'id': str(conv_id)})
|
||||
|
||||
|
||||
@app.route('/api/user/<int:user_id>/conversations/<int:conv_id>', methods=['PUT'])
|
||||
def update_user_conversation(user_id, conv_id):
|
||||
"""更新对话(添加消息、修改标题等)"""
|
||||
data = request.json
|
||||
conn = get_db()
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 验证对话属于该用户
|
||||
cursor.execute('SELECT id FROM conversations WHERE id = ? AND user_id = ?', (conv_id, user_id))
|
||||
if not cursor.fetchone():
|
||||
conn.close()
|
||||
return jsonify({'error': '对话不存在'}), 404
|
||||
|
||||
# 更新字段
|
||||
updates = []
|
||||
values = []
|
||||
|
||||
if 'title' in data:
|
||||
updates.append('title = ?')
|
||||
values.append(data['title'])
|
||||
|
||||
if 'messages' in data:
|
||||
updates.append('messages = ?')
|
||||
values.append(json.dumps(data['messages']))
|
||||
|
||||
if 'agentId' in data or 'agent_id' in data:
|
||||
updates.append('agent_id = ?')
|
||||
values.append(data.get('agentId') or data.get('agent_id'))
|
||||
|
||||
if updates:
|
||||
updates.append('updated_at = CURRENT_TIMESTAMP')
|
||||
sql = f'UPDATE conversations SET {", ".join(updates)} WHERE id = ?'
|
||||
values.append(conv_id)
|
||||
cursor.execute(sql, values)
|
||||
conn.commit()
|
||||
|
||||
conn.close()
|
||||
return jsonify({'success': True})
|
||||
|
||||
|
||||
@app.route('/api/user/<int:user_id>/conversations/<int:conv_id>', methods=['DELETE'])
|
||||
def delete_user_conversation(user_id, conv_id):
|
||||
"""删除对话"""
|
||||
conn = get_db()
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 验证对话属于该用户
|
||||
cursor.execute('SELECT id FROM conversations WHERE id = ? AND user_id = ?', (conv_id, user_id))
|
||||
if not cursor.fetchone():
|
||||
conn.close()
|
||||
return jsonify({'error': '对话不存在'}), 404
|
||||
|
||||
cursor.execute('DELETE FROM conversations WHERE id = ?', (conv_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return jsonify({'success': True})
|
||||
|
||||
|
||||
# ==================== 大模型接口管理 ====================
|
||||
|
||||
@app.route('/api/admin/llm', methods=['GET'])
|
||||
|
||||
Reference in New Issue
Block a user