refac
This commit is contained in:
1 parent
5462c02af0
commit
5ec16e76e6
2 files changed
+53
-16
No files matched your search
@@ -1547,6 +1547,7 @@ async def view_chat(
|
|||||||
async def delegate_task(
|
async def delegate_task(
|
||||||
task: str,
|
task: str,
|
||||||
context: str = '',
|
context: str = '',
|
||||||
|
file_ids: list[str] | None = None,
|
||||||
background: bool = False,
|
background: bool = False,
|
||||||
__request__: Request = None,
|
__request__: Request = None,
|
||||||
__user__: dict = None,
|
__user__: dict = None,
|
||||||
@@ -1559,6 +1560,8 @@ async def delegate_task(
|
|||||||
|
|
||||||
:param task: The specific task for the sub-agent to complete
|
:param task: The specific task for the sub-agent to complete
|
||||||
:param context: Relevant context, decisions, or file paths for the task
|
:param context: Relevant context, decisions, or file paths for the task
|
||||||
|
:param file_ids: Attached file IDs the sub-agent needs. Use this for images or files;
|
||||||
|
do not put file IDs only in context.
|
||||||
:param background: Return immediately and continue this chat when the sub-agent finishes
|
:param background: Return immediately and continue this chat when the sub-agent finishes
|
||||||
:return: Foreground result text, or a JSON dispatch handle for background work
|
:return: Foreground result text, or a JSON dispatch handle for background work
|
||||||
"""
|
"""
|
||||||
@@ -1573,6 +1576,7 @@ async def delegate_task(
|
|||||||
task,
|
task,
|
||||||
context,
|
context,
|
||||||
background,
|
background,
|
||||||
|
file_ids=file_ids,
|
||||||
request=__request__,
|
request=__request__,
|
||||||
user_data=__user__ or {},
|
user_data=__user__ or {},
|
||||||
metadata=__metadata__ or {},
|
metadata=__metadata__ or {},
|
||||||
|
|||||||
@@ -272,6 +272,7 @@ async def delegate(
|
|||||||
context: str,
|
context: str,
|
||||||
background: bool,
|
background: bool,
|
||||||
*,
|
*,
|
||||||
|
file_ids: list[str] | None = None,
|
||||||
request: Request,
|
request: Request,
|
||||||
user_data: dict,
|
user_data: dict,
|
||||||
metadata: dict,
|
metadata: dict,
|
||||||
@@ -331,6 +332,33 @@ async def delegate(
|
|||||||
return 'Error: model context is required.'
|
return 'Error: model context is required.'
|
||||||
if run.get('direct'):
|
if run.get('direct'):
|
||||||
return 'Error: sub-agents are unavailable for direct connections.'
|
return 'Error: sub-agents are unavailable for direct connections.'
|
||||||
|
if file_ids:
|
||||||
|
requested_file_ids = {str(file_id) for file_id in file_ids if file_id}
|
||||||
|
run['files'] = [
|
||||||
|
copy.deepcopy(file)
|
||||||
|
for file in metadata.get('files') or []
|
||||||
|
if str(file.get('id') or '') in requested_file_ids
|
||||||
|
or str(file.get('url') or '') in requested_file_ids
|
||||||
|
or (
|
||||||
|
isinstance(file.get('file'), dict)
|
||||||
|
and str(file.get('file', {}).get('id') or '') in requested_file_ids
|
||||||
|
)
|
||||||
|
]
|
||||||
|
found_file_ids = {
|
||||||
|
str(value)
|
||||||
|
for file in run['files']
|
||||||
|
for value in (
|
||||||
|
file.get('id'),
|
||||||
|
file.get('url'),
|
||||||
|
file.get('file', {}).get('id') if isinstance(file.get('file'), dict) else None,
|
||||||
|
)
|
||||||
|
if value
|
||||||
|
}
|
||||||
|
missing_file_ids = sorted(requested_file_ids - found_file_ids)
|
||||||
|
if missing_file_ids:
|
||||||
|
return f'Error: file_ids not attached or unavailable: {", ".join(missing_file_ids)}'
|
||||||
|
else:
|
||||||
|
run['files'] = []
|
||||||
|
|
||||||
delegation_id = f'deleg_{uuid4().hex[:8]}'
|
delegation_id = f'deleg_{uuid4().hex[:8]}'
|
||||||
foreground_semaphore = None
|
foreground_semaphore = None
|
||||||
@@ -355,6 +383,17 @@ async def delegate(
|
|||||||
user_message_id = str(uuid4())
|
user_message_id = str(uuid4())
|
||||||
assistant_message_id = str(uuid4())
|
assistant_message_id = str(uuid4())
|
||||||
prompt = f'{task}\n\n## Context\n{context}' if context else task
|
prompt = f'{task}\n\n## Context\n{context}' if context else task
|
||||||
|
prompt_files = copy.deepcopy(run.get('files') or [])
|
||||||
|
user_message = {
|
||||||
|
'id': user_message_id,
|
||||||
|
'parentId': None,
|
||||||
|
'childrenIds': [assistant_message_id],
|
||||||
|
'role': 'user',
|
||||||
|
'content': prompt,
|
||||||
|
'timestamp': int(time.time()),
|
||||||
|
'models': [run['model_id']],
|
||||||
|
**({'files': prompt_files} if prompt_files else {}),
|
||||||
|
}
|
||||||
chat = await Chats.insert_new_chat(
|
chat = await Chats.insert_new_chat(
|
||||||
chat_id,
|
chat_id,
|
||||||
user.id,
|
user.id,
|
||||||
@@ -366,15 +405,7 @@ async def delegate(
|
|||||||
'history': {
|
'history': {
|
||||||
'currentId': assistant_message_id,
|
'currentId': assistant_message_id,
|
||||||
'messages': {
|
'messages': {
|
||||||
user_message_id: {
|
user_message_id: user_message,
|
||||||
'id': user_message_id,
|
|
||||||
'parentId': None,
|
|
||||||
'childrenIds': [assistant_message_id],
|
|
||||||
'role': 'user',
|
|
||||||
'content': prompt,
|
|
||||||
'timestamp': int(time.time()),
|
|
||||||
'models': [run['model_id']],
|
|
||||||
},
|
|
||||||
assistant_message_id: {
|
assistant_message_id: {
|
||||||
'id': assistant_message_id,
|
'id': assistant_message_id,
|
||||||
'parentId': user_message_id,
|
'parentId': user_message_id,
|
||||||
@@ -387,7 +418,14 @@ async def delegate(
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'messages': [{'role': 'user', 'content': prompt}],
|
'messages': [
|
||||||
|
{
|
||||||
|
'role': 'user',
|
||||||
|
'content': prompt,
|
||||||
|
**({'files': prompt_files} if prompt_files else {}),
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'files': prompt_files,
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
internal_meta={
|
internal_meta={
|
||||||
@@ -435,12 +473,7 @@ async def delegate(
|
|||||||
'chat_id': chat_id,
|
'chat_id': chat_id,
|
||||||
'id': assistant_message_id,
|
'id': assistant_message_id,
|
||||||
'parent_id': None,
|
'parent_id': None,
|
||||||
'user_message': {
|
'user_message': user_message,
|
||||||
'id': user_message_id,
|
|
||||||
'parentId': None,
|
|
||||||
'role': 'user',
|
|
||||||
'content': prompt,
|
|
||||||
},
|
|
||||||
'session_id': run.get('session_id') or f'subagent:{chat_id}',
|
'session_id': run.get('session_id') or f'subagent:{chat_id}',
|
||||||
'background_tasks': {},
|
'background_tasks': {},
|
||||||
'tool_ids': run.get('tool_ids') or [],
|
'tool_ids': run.get('tool_ids') or [],
|
||||||
|
|||||||
Reference in new issue
Block a user