refac
This commit is contained in:
1 parent
3fcad2f627
commit
4d99baa292
3 files changed
+32
-13
No files matched your search
@@ -1763,6 +1763,7 @@ async def chat_completion(
|
|||||||
'chat_id': form_data.pop('chat_id', None),
|
'chat_id': form_data.pop('chat_id', None),
|
||||||
'user_message': user_message,
|
'user_message': user_message,
|
||||||
'user_message_id': user_message.get('id') if user_message else None,
|
'user_message_id': user_message.get('id') if user_message else None,
|
||||||
|
'assistant_message_id': form_data.pop('assistant_message_id', None),
|
||||||
'session_id': form_data.pop('session_id', None),
|
'session_id': form_data.pop('session_id', None),
|
||||||
'folder_id': form_data.pop('folder_id', None),
|
'folder_id': form_data.pop('folder_id', None),
|
||||||
'filter_ids': form_data.pop('filter_ids', []),
|
'filter_ids': form_data.pop('filter_ids', []),
|
||||||
|
|||||||
@@ -2274,6 +2274,16 @@ async def process_chat_payload(request, form_data, user, metadata, model):
|
|||||||
if chat_id and user_message_id and not chat_id.startswith('local:'):
|
if chat_id and user_message_id and not chat_id.startswith('local:'):
|
||||||
db_messages = await load_messages_from_db(chat_id, user_message_id)
|
db_messages = await load_messages_from_db(chat_id, user_message_id)
|
||||||
if db_messages:
|
if db_messages:
|
||||||
|
# Continue: frontend sends assistant_message_id when continuing
|
||||||
|
# an existing response. Load its content so the LLM sees prior output.
|
||||||
|
assistant_message_id = metadata.get('assistant_message_id')
|
||||||
|
if assistant_message_id:
|
||||||
|
assistant_message = await Chats.get_message_by_id_and_message_id(chat_id, assistant_message_id)
|
||||||
|
if assistant_message and (assistant_message.get('content') or assistant_message.get('output')):
|
||||||
|
db_messages.append(
|
||||||
|
{k: v for k, v in assistant_message.items() if k in ('role', 'content', 'output', 'files')}
|
||||||
|
)
|
||||||
|
|
||||||
system_message = get_system_message(form_data.get('messages', []))
|
system_message = get_system_message(form_data.get('messages', []))
|
||||||
form_data['messages'] = [system_message, *db_messages] if system_message else db_messages
|
form_data['messages'] = [system_message, *db_messages] if system_message else db_messages
|
||||||
|
|
||||||
@@ -3015,7 +3025,6 @@ async def background_tasks_handler(ctx):
|
|||||||
tasks = ctx['tasks']
|
tasks = ctx['tasks']
|
||||||
event_emitter = ctx['event_emitter']
|
event_emitter = ctx['event_emitter']
|
||||||
|
|
||||||
|
|
||||||
message = None
|
message = None
|
||||||
messages = []
|
messages = []
|
||||||
|
|
||||||
@@ -4076,9 +4085,9 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
current_response_tool_call['function']['name'] = delta_name
|
current_response_tool_call['function']['name'] = delta_name
|
||||||
|
|
||||||
if delta_arguments:
|
if delta_arguments:
|
||||||
current_response_tool_call['function']['arguments'] += (
|
current_response_tool_call['function'][
|
||||||
delta_arguments
|
'arguments'
|
||||||
)
|
] += delta_arguments
|
||||||
|
|
||||||
# Emit pending tool calls in real-time
|
# Emit pending tool calls in real-time
|
||||||
if response_tool_calls:
|
if response_tool_calls:
|
||||||
@@ -4832,8 +4841,7 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
code = sanitize_code(code)
|
code = sanitize_code(code)
|
||||||
|
|
||||||
if CODE_INTERPRETER_BLOCKED_MODULES:
|
if CODE_INTERPRETER_BLOCKED_MODULES:
|
||||||
blocking_code = textwrap.dedent(
|
blocking_code = textwrap.dedent(f"""
|
||||||
f"""
|
|
||||||
import builtins
|
import builtins
|
||||||
|
|
||||||
BLOCKED_MODULES = {CODE_INTERPRETER_BLOCKED_MODULES}
|
BLOCKED_MODULES = {CODE_INTERPRETER_BLOCKED_MODULES}
|
||||||
@@ -4849,8 +4857,7 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
return _real_import(name, globals, locals, fromlist, level)
|
return _real_import(name, globals, locals, fromlist, level)
|
||||||
|
|
||||||
builtins.__import__ = restricted_import
|
builtins.__import__ = restricted_import
|
||||||
"""
|
""")
|
||||||
)
|
|
||||||
code = blocking_code + '\n' + code
|
code = blocking_code + '\n' + code
|
||||||
|
|
||||||
if request.app.state.config.CODE_INTERPRETER_ENGINE == 'pyodide':
|
if request.app.state.config.CODE_INTERPRETER_ENGINE == 'pyodide':
|
||||||
|
|||||||
@@ -2128,8 +2128,10 @@
|
|||||||
_history,
|
_history,
|
||||||
primaryResponseMessageId,
|
primaryResponseMessageId,
|
||||||
_chatId,
|
_chatId,
|
||||||
selectedModelIds.length > 1 ? messageIdsMap : undefined,
|
{
|
||||||
regenerationPrompt
|
messageIdsMap: selectedModelIds.length > 1 ? messageIdsMap : undefined,
|
||||||
|
regenerationPrompt
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
if (chatEventEmitter) clearInterval(chatEventEmitter);
|
if (chatEventEmitter) clearInterval(chatEventEmitter);
|
||||||
@@ -2194,8 +2196,15 @@
|
|||||||
_history,
|
_history,
|
||||||
responseMessageId,
|
responseMessageId,
|
||||||
_chatId,
|
_chatId,
|
||||||
messageIdsMap?: Record<string, string>,
|
{
|
||||||
regenerationPrompt?: string | null
|
messageIdsMap,
|
||||||
|
regenerationPrompt,
|
||||||
|
continueResponse = false
|
||||||
|
}: {
|
||||||
|
messageIdsMap?: Record<string, string>;
|
||||||
|
regenerationPrompt?: string | null;
|
||||||
|
continueResponse?: boolean;
|
||||||
|
} = {}
|
||||||
) => {
|
) => {
|
||||||
const responseMessage = _history.messages[responseMessageId];
|
const responseMessage = _history.messages[responseMessageId];
|
||||||
const userMessage = _history.messages[responseMessage.parentId];
|
const userMessage = _history.messages[responseMessage.parentId];
|
||||||
@@ -2396,6 +2405,7 @@
|
|||||||
parent_id: userMessage?.parentId ?? null,
|
parent_id: userMessage?.parentId ?? null,
|
||||||
user_message: userMessage,
|
user_message: userMessage,
|
||||||
...(regenerationPrompt ? { regeneration_prompt: regenerationPrompt } : {}),
|
...(regenerationPrompt ? { regeneration_prompt: regenerationPrompt } : {}),
|
||||||
|
...(continueResponse ? { assistant_message_id: responseMessageId } : {}),
|
||||||
|
|
||||||
background_tasks: {
|
background_tasks: {
|
||||||
...(!$temporaryChatEnabled && !_chatId && (userMessage?.parentId ?? null) === null
|
...(!$temporaryChatEnabled && !_chatId && (userMessage?.parentId ?? null) === null
|
||||||
@@ -2655,7 +2665,8 @@
|
|||||||
createMessagesList(history, responseMessage.id),
|
createMessagesList(history, responseMessage.id),
|
||||||
history,
|
history,
|
||||||
responseMessage.id,
|
responseMessage.id,
|
||||||
_chatId
|
_chatId,
|
||||||
|
{ continueResponse: true }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user