refac
This commit is contained in:
1 parent
4d27bfff92
commit
a410ca36af
1 file changed
+38
-35
@@ -523,24 +523,6 @@ async def delegate(
|
|||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
result = {'status': 'error', 'summary': '', 'error': str(exc)}
|
result = {'status': 'error', 'summary': '', 'error': str(exc)}
|
||||||
|
|
||||||
parent = await Chats.get_chat_by_id_and_user_id(parent_chat_id, user.id)
|
|
||||||
if not parent:
|
|
||||||
if cancelled:
|
|
||||||
raise asyncio.CancelledError
|
|
||||||
return result
|
|
||||||
|
|
||||||
history = copy.deepcopy(parent.chat.get('history') or {})
|
|
||||||
messages = history.setdefault('messages', {})
|
|
||||||
done_assistants = [
|
|
||||||
message
|
|
||||||
for message in messages.values()
|
|
||||||
if message.get('role') == 'assistant' and message.get('done') is not False
|
|
||||||
]
|
|
||||||
result_parent_id = (
|
|
||||||
max(done_assistants, key=lambda message: message.get('timestamp', 0)).get('id')
|
|
||||||
if done_assistants
|
|
||||||
else parent_message_id
|
|
||||||
)
|
|
||||||
duration = f'{time.time() - started_at:.1f}s'
|
duration = f'{time.time() - started_at:.1f}s'
|
||||||
lines = [
|
lines = [
|
||||||
f'[ASYNC SUBAGENT COMPLETE - {delegation_id}]',
|
f'[ASYNC SUBAGENT COMPLETE - {delegation_id}]',
|
||||||
@@ -582,7 +564,7 @@ async def delegate(
|
|||||||
}
|
}
|
||||||
pending_message = {
|
pending_message = {
|
||||||
'id': pending_message_id,
|
'id': pending_message_id,
|
||||||
'parentId': result_parent_id,
|
'parentId': None,
|
||||||
'childrenIds': [],
|
'childrenIds': [],
|
||||||
'role': 'user',
|
'role': 'user',
|
||||||
'content': '\n'.join(lines),
|
'content': '\n'.join(lines),
|
||||||
@@ -593,22 +575,43 @@ async def delegate(
|
|||||||
|
|
||||||
lock = _parent_locks.setdefault(parent_chat_id, asyncio.Lock())
|
lock = _parent_locks.setdefault(parent_chat_id, asyncio.Lock())
|
||||||
async with lock:
|
async with lock:
|
||||||
parent = await Chats.get_chat_by_id_and_user_id(parent_chat_id, user.id)
|
async with get_async_db() as db:
|
||||||
if not parent:
|
stmt = select(Chat).where(Chat.id == parent_chat_id, Chat.user_id == user.id)
|
||||||
if cancelled:
|
if db.bind.dialect.name == 'postgresql':
|
||||||
raise asyncio.CancelledError
|
stmt = stmt.with_for_update()
|
||||||
return result
|
result_row = await db.execute(stmt)
|
||||||
if await has_active_tasks(request.app.state.redis, parent_chat_id):
|
parent = result_row.scalar_one_or_none()
|
||||||
pending_message['meta']['status'] = 'pending'
|
if not parent:
|
||||||
updated_chat = copy.deepcopy(parent.chat)
|
if cancelled:
|
||||||
updated_history = updated_chat.setdefault('history', {})
|
raise asyncio.CancelledError
|
||||||
updated_messages = updated_history.setdefault('messages', {})
|
return result
|
||||||
updated_messages[pending_message_id] = pending_message
|
|
||||||
if result_parent_id and result_parent_id in updated_messages:
|
updated_chat = copy.deepcopy(parent.chat or {})
|
||||||
children = updated_messages[result_parent_id].setdefault('childrenIds', [])
|
updated_history = updated_chat.setdefault('history', {})
|
||||||
if pending_message_id not in children:
|
updated_messages = updated_history.setdefault('messages', {})
|
||||||
children.append(pending_message_id)
|
done_assistants = [
|
||||||
await Chats.update_chat_by_id(parent_chat_id, updated_chat)
|
message
|
||||||
|
for message in updated_messages.values()
|
||||||
|
if message.get('role') == 'assistant' and message.get('done') is not False
|
||||||
|
]
|
||||||
|
result_parent_id = (
|
||||||
|
max(done_assistants, key=lambda message: message.get('timestamp', 0)).get('id')
|
||||||
|
if done_assistants
|
||||||
|
else parent_message_id
|
||||||
|
)
|
||||||
|
pending_message['parentId'] = result_parent_id
|
||||||
|
if await has_active_tasks(request.app.state.redis, parent_chat_id):
|
||||||
|
pending_message['meta']['status'] = 'pending'
|
||||||
|
updated_messages[pending_message_id] = pending_message
|
||||||
|
if result_parent_id and result_parent_id in updated_messages:
|
||||||
|
children = updated_messages[result_parent_id].setdefault('childrenIds', [])
|
||||||
|
if pending_message_id not in children:
|
||||||
|
children.append(pending_message_id)
|
||||||
|
updated_history['messages'] = updated_messages
|
||||||
|
parent.chat = {**(parent.chat or {}), **updated_chat, 'history': updated_history}
|
||||||
|
parent.updated_at = int(time.time())
|
||||||
|
await db.commit()
|
||||||
|
|
||||||
await ChatMessages.upsert_message(
|
await ChatMessages.upsert_message(
|
||||||
message_id=pending_message_id,
|
message_id=pending_message_id,
|
||||||
chat_id=parent_chat_id,
|
chat_id=parent_chat_id,
|
||||||
|
|||||||
Reference in new issue
Block a user