Add brain memory services and APIs
Introduce the backend pieces for brain memory ingestion, routing, and system telemetry so the new knowledge workflows can project data into a brain view. The supporting tests lock in the new behavior and keep the expanded backend surface stable. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -92,11 +92,12 @@ async def chat(
|
||||
):
|
||||
"""简单版对话(非流式)"""
|
||||
agent_svc = AgentService(db)
|
||||
conv_id, msg_id, content = await agent_svc.chat_simple(
|
||||
conv_id, msg_id, content, model_name = await agent_svc.chat_simple(
|
||||
user_id=current_user.id,
|
||||
message=data.message,
|
||||
conversation_id=data.conversation_id,
|
||||
file_ids=data.file_ids,
|
||||
model_name=data.model_name,
|
||||
)
|
||||
|
||||
# 更新对话消息计数
|
||||
@@ -111,6 +112,7 @@ async def chat(
|
||||
message_id=msg_id,
|
||||
content=content,
|
||||
agent_name="jarvis",
|
||||
model_name=model_name,
|
||||
)
|
||||
|
||||
|
||||
@@ -128,24 +130,24 @@ async def chat_stream(
|
||||
user_id=current_user.id,
|
||||
message=data.message,
|
||||
conversation_id=data.conversation_id,
|
||||
file_ids=data.file_ids,
|
||||
model_name=data.model_name,
|
||||
)
|
||||
|
||||
# 先发送元数据
|
||||
yield f"event: metadata\ndata: {json.dumps({'conversation_id': conv_id, 'message_id': msg_id})}\n\n"
|
||||
|
||||
# 流式发送内容
|
||||
collected = ""
|
||||
try:
|
||||
async for chunk in stream:
|
||||
if chunk:
|
||||
collected += chunk
|
||||
yield f"event: chunk\ndata: {json.dumps({'content': chunk})}\n\n"
|
||||
|
||||
# 更新数据库中的消息
|
||||
await agent_svc.save_response(msg_id, collected)
|
||||
|
||||
async for event in stream:
|
||||
event_type = event.get('type', 'progress')
|
||||
if event_type == 'chunk':
|
||||
yield f"event: chunk\ndata: {json.dumps({'content': event.get('content', '')}, ensure_ascii=False)}\n\n"
|
||||
elif event_type == 'error':
|
||||
yield f"event: error\ndata: {json.dumps({'error': event.get('error', '未知错误')}, ensure_ascii=False)}\n\n"
|
||||
else:
|
||||
payload = {k: v for k, v in event.items() if k != 'type'}
|
||||
yield f"event: progress\ndata: {json.dumps(payload, ensure_ascii=False)}\n\n"
|
||||
except Exception as e:
|
||||
yield f"event: error\ndata: {json.dumps({'error': str(e)})}\n\n"
|
||||
yield f"event: error\ndata: {json.dumps({'error': str(e)}, ensure_ascii=False)}\n\n"
|
||||
finally:
|
||||
yield f"event: done\ndata: {json.dumps({'message_id': msg_id})}\n\n"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user