diff --git a/backend/open_webui/models/chats.py b/backend/open_webui/models/chats.py index bcf6951e49..4dc00e9f84 100644 --- a/backend/open_webui/models/chats.py +++ b/backend/open_webui/models/chats.py @@ -366,20 +366,20 @@ class ChatTable: await db.commit() # Dual-write messages to chat_message table - try: - for form_data, chat_obj in zip(chat_import_forms, chats): - history = form_data.chat.get('history', {}) - messages = history.get('messages', {}) - for message_id, message in messages.items(): - if isinstance(message, dict) and message.get('role'): + for form_data, chat_obj in zip(chat_import_forms, chats): + history = form_data.chat.get('history', {}) + messages = history.get('messages', {}) + for message_id, message in messages.items(): + if isinstance(message, dict) and message.get('role'): + try: await ChatMessages.upsert_message( message_id=message_id, chat_id=chat_obj.id, user_id=user_id, data=message, ) - except Exception as e: - log.warning(f'Failed to write imported messages to chat_message table: {e}') + except Exception as e: + log.warning(f'Failed to write imported message {message_id} for chat {chat_obj.id}: {e}') return [ChatModel.model_validate(chat) for chat in chats] diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 1820e70481..5432c3b211 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -729,6 +729,7 @@ const convertOpenAIMessages = (convo) => { const messages = []; let currentId = ''; let lastId = null; + const uniqueModels = new Set(); for (const message_id in mapping) { const message = mapping[message_id]; @@ -749,16 +750,28 @@ const convertOpenAIMessages = (convo) => { continue; } - const new_chat = { + const model = + message['message']?.['metadata']?.['model_slug'] || 'gpt-3.5-turbo'; + const timestamp = message['message']?.['create_time'] + ? Math.floor(message['message']['create_time']) + : undefined; + + const new_chat: Record = { id: message_id, parentId: lastId, childrenIds: message['children'] || [], role: role !== 'user' ? 'assistant' : 'user', content: extractOpenAIMessageContent(message['message']), - model: 'gpt-3.5-turbo', + model, done: true, - context: null + context: null, + ...(timestamp !== undefined && { timestamp }) }; + + if (role !== 'user') { + uniqueModels.add(model); + } + messages.push(new_chat); lastId = currentId; } @@ -781,7 +794,7 @@ const convertOpenAIMessages = (convo) => { currentId: currentId, messages: history // Need to convert this to not a list and instead a json object }, - models: ['gpt-3.5-turbo'], + models: uniqueModels.size > 0 ? [...uniqueModels] : ['gpt-3.5-turbo'], messages: messages, options: {}, timestamp: convo['create_time'], @@ -828,12 +841,17 @@ export const convertOpenAIChats = (_chats) => { const chat = convertOpenAIMessages(convo); if (validateChat(chat)) { + // Use created_at/updated_at keys so importChatsHandler passes them + // to the backend correctly (previously used 'timestamp' which was ignored) + const createdAt = convo['create_time'] ? Math.floor(convo['create_time']) : null; + const updatedAt = convo['update_time'] ? Math.floor(convo['update_time']) : createdAt; chats.push({ id: convo['id'], user_id: '', title: convo['title'], chat: chat, - timestamp: convo['create_time'] + created_at: createdAt, + updated_at: updatedAt }); } else { failed++;