refac
This commit is contained in:
1 parent
34b8844ccf
commit
f16b5c4460
3 files changed
+18
-12
No files matched your search
@@ -1760,7 +1760,7 @@ async def chat_completion(
|
|||||||
|
|
||||||
metadata = {
|
metadata = {
|
||||||
'user_id': user.id,
|
'user_id': user.id,
|
||||||
'chat_id': form_data.pop('chat_id', None),
|
'chat_id': form_data.pop('chat_id', None) or '',
|
||||||
'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),
|
'assistant_message_id': form_data.pop('assistant_message_id', None),
|
||||||
@@ -2011,7 +2011,7 @@ async def chat_completion(
|
|||||||
if metadata.get('chat_id') and metadata.get('message_id'):
|
if metadata.get('chat_id') and metadata.get('message_id'):
|
||||||
# Update the chat message with the error
|
# Update the chat message with the error
|
||||||
try:
|
try:
|
||||||
if not metadata['chat_id'].startswith('local:') and not metadata['chat_id'].startswith('channel:'):
|
if not metadata.get('chat_id', '').startswith('local:') and not metadata.get('chat_id', '').startswith('channel:'):
|
||||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
metadata['chat_id'],
|
metadata['chat_id'],
|
||||||
metadata['message_id'],
|
metadata['message_id'],
|
||||||
|
|||||||
@@ -389,6 +389,8 @@ async def _write_tts_cache(
|
|||||||
async def _tts_openai(request, payload, file_path, file_body_path, user):
|
async def _tts_openai(request, payload, file_path, file_body_path, user):
|
||||||
"""Generate speech via an OpenAI-compatible TTS endpoint."""
|
"""Generate speech via an OpenAI-compatible TTS endpoint."""
|
||||||
payload['model'] = request.app.state.config.TTS_MODEL
|
payload['model'] = request.app.state.config.TTS_MODEL
|
||||||
|
if not payload.get('voice'):
|
||||||
|
payload['voice'] = request.app.state.config.TTS_VOICE
|
||||||
payload = {**payload, **(request.app.state.config.TTS_OPENAI_PARAMS or {})}
|
payload = {**payload, **(request.app.state.config.TTS_OPENAI_PARAMS or {})}
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
|
|||||||
@@ -2292,6 +2292,10 @@ async def connect_mcp_server(
|
|||||||
|
|
||||||
|
|
||||||
async def process_chat_payload(request, form_data, user, metadata, model):
|
async def process_chat_payload(request, form_data, user, metadata, model):
|
||||||
|
# Ensure chat_id is always a string — external API clients may omit it.
|
||||||
|
if not isinstance(metadata.get('chat_id'), str):
|
||||||
|
metadata['chat_id'] = ''
|
||||||
|
|
||||||
# Pipeline Inlet -> Filter Inlet -> Chat Memory -> Chat Web Search -> Chat Image Generation
|
# Pipeline Inlet -> Filter Inlet -> Chat Memory -> Chat Web Search -> Chat Image Generation
|
||||||
# -> Chat Code Interpreter (Form Data Update) -> (Default) Chat Tools Function Calling
|
# -> Chat Code Interpreter (Form Data Update) -> (Default) Chat Tools Function Calling
|
||||||
# -> Chat Files
|
# -> Chat Files
|
||||||
@@ -3030,8 +3034,8 @@ async def background_tasks_handler(ctx):
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
'chat_id' in metadata
|
'chat_id' in metadata
|
||||||
and not metadata['chat_id'].startswith('local:')
|
and not metadata.get('chat_id', '').startswith('local:')
|
||||||
and not metadata['chat_id'].startswith('channel:')
|
and not metadata.get('chat_id', '').startswith('channel:')
|
||||||
):
|
):
|
||||||
messages_map = await Chats.get_messages_map_by_chat_id(metadata['chat_id'])
|
messages_map = await Chats.get_messages_map_by_chat_id(metadata['chat_id'])
|
||||||
message = messages_map.get(metadata['message_id']) if messages_map else None
|
message = messages_map.get(metadata['message_id']) if messages_map else None
|
||||||
@@ -3394,7 +3398,7 @@ async def non_streaming_chat_response_handler(response, ctx):
|
|||||||
|
|
||||||
log.error('Provider returned error (non-streaming): %s', error)
|
log.error('Provider returned error (non-streaming): %s', error)
|
||||||
|
|
||||||
if not metadata['chat_id'].startswith('channel:'):
|
if not metadata.get('chat_id', '').startswith('channel:'):
|
||||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
metadata['chat_id'],
|
metadata['chat_id'],
|
||||||
metadata['message_id'],
|
metadata['message_id'],
|
||||||
@@ -3410,7 +3414,7 @@ async def non_streaming_chat_response_handler(response, ctx):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
if 'selected_model_id' in response_data and not metadata['chat_id'].startswith('channel:'):
|
if 'selected_model_id' in response_data and not metadata.get('chat_id', '').startswith('channel:'):
|
||||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
metadata['chat_id'],
|
metadata['chat_id'],
|
||||||
metadata['message_id'],
|
metadata['message_id'],
|
||||||
@@ -3433,7 +3437,7 @@ async def non_streaming_chat_response_handler(response, ctx):
|
|||||||
|
|
||||||
title = (
|
title = (
|
||||||
await Chats.get_chat_title_by_id(metadata['chat_id'])
|
await Chats.get_chat_title_by_id(metadata['chat_id'])
|
||||||
if not metadata['chat_id'].startswith('channel:')
|
if not metadata.get('chat_id', '').startswith('channel:')
|
||||||
else ''
|
else ''
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -3466,7 +3470,7 @@ async def non_streaming_chat_response_handler(response, ctx):
|
|||||||
# Save message in the database
|
# Save message in the database
|
||||||
usage = normalize_usage(response_data.get('usage', {}) or {})
|
usage = normalize_usage(response_data.get('usage', {}) or {})
|
||||||
|
|
||||||
if not metadata['chat_id'].startswith('channel:'):
|
if not metadata.get('chat_id', '').startswith('channel:'):
|
||||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
metadata['chat_id'],
|
metadata['chat_id'],
|
||||||
metadata['message_id'],
|
metadata['message_id'],
|
||||||
@@ -4332,7 +4336,7 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
if end:
|
if end:
|
||||||
break
|
break
|
||||||
|
|
||||||
if ENABLE_REALTIME_CHAT_SAVE and not metadata['chat_id'].startswith('channel:'):
|
if ENABLE_REALTIME_CHAT_SAVE and not metadata.get('chat_id', '').startswith('channel:'):
|
||||||
# Save message in the database
|
# Save message in the database
|
||||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
metadata['chat_id'],
|
metadata['chat_id'],
|
||||||
@@ -5033,7 +5037,7 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
|
|
||||||
title = (
|
title = (
|
||||||
await Chats.get_chat_title_by_id(metadata['chat_id'])
|
await Chats.get_chat_title_by_id(metadata['chat_id'])
|
||||||
if not metadata['chat_id'].startswith('channel:')
|
if not metadata.get('chat_id', '').startswith('channel:')
|
||||||
else ''
|
else ''
|
||||||
)
|
)
|
||||||
data = {
|
data = {
|
||||||
@@ -5044,7 +5048,7 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
**({'usage': usage} if usage else {}),
|
**({'usage': usage} if usage else {}),
|
||||||
}
|
}
|
||||||
|
|
||||||
if not metadata['chat_id'].startswith('channel:'):
|
if not metadata.get('chat_id', '').startswith('channel:'):
|
||||||
if not ENABLE_REALTIME_CHAT_SAVE:
|
if not ENABLE_REALTIME_CHAT_SAVE:
|
||||||
# Save message in the database
|
# Save message in the database
|
||||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
@@ -5115,7 +5119,7 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
|
|
||||||
async def save_cancelled_state():
|
async def save_cancelled_state():
|
||||||
await event_emitter({'type': 'chat:tasks:cancel'})
|
await event_emitter({'type': 'chat:tasks:cancel'})
|
||||||
if not metadata['chat_id'].startswith('channel:'):
|
if not metadata.get('chat_id', '').startswith('channel:'):
|
||||||
if not ENABLE_REALTIME_CHAT_SAVE:
|
if not ENABLE_REALTIME_CHAT_SAVE:
|
||||||
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
await Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||||
metadata['chat_id'],
|
metadata['chat_id'],
|
||||||
|
|||||||
Reference in new issue
Block a user