Latest fixxes
This commit is contained in:
@@ -11,6 +11,7 @@ from typing import List, Dict, Any, Optional
|
||||
from ai_service.config import config
|
||||
from ai_service.models.model_service import model_service
|
||||
from ai_service.models.model_parameters import ModelParameters
|
||||
from ai_service.openwebui_channels import openwebui_channels
|
||||
|
||||
class ChatService:
|
||||
"""Service for chat functionality."""
|
||||
@@ -61,17 +62,40 @@ class ChatService:
|
||||
# Generate a unique ID for the chat
|
||||
chat_id = str(uuid.uuid4())
|
||||
|
||||
# Default title if none provided
|
||||
chat_title = title or f"Chat {len(self.chats) + 1}"
|
||||
|
||||
# For team chats, create an OpenWebUI channel
|
||||
openwebui_channel_id = None
|
||||
if is_team_chat:
|
||||
try:
|
||||
# Create a channel in OpenWebUI
|
||||
channel_response = openwebui_channels.create_channel(
|
||||
name=chat_title,
|
||||
description=f"Team chat created by {user_id}",
|
||||
is_private=True # Team chats are private by default
|
||||
)
|
||||
|
||||
if channel_response:
|
||||
openwebui_channel_id = channel_response.get('id')
|
||||
print(f"Created OpenWebUI channel with ID: {openwebui_channel_id}")
|
||||
else:
|
||||
print("Failed to create OpenWebUI channel, continuing with local team chat only")
|
||||
except Exception as e:
|
||||
print(f"Error creating OpenWebUI channel: {str(e)}")
|
||||
|
||||
# Create chat data
|
||||
self.chats[chat_id] = {
|
||||
'id': chat_id,
|
||||
'title': title or f"Chat {len(self.chats) + 1}",
|
||||
'title': chat_title,
|
||||
'user_id': user_id,
|
||||
'model_id': model_id or config.DEFAULT_MODEL,
|
||||
'is_team_chat': is_team_chat,
|
||||
'created_at': datetime.utcnow().isoformat(),
|
||||
'updated_at': datetime.utcnow().isoformat(),
|
||||
'created_at': datetime.now().isoformat(),
|
||||
'updated_at': datetime.now().isoformat(),
|
||||
'messages': [],
|
||||
'team_members': [user_id] if is_team_chat else []
|
||||
'team_members': [user_id] if is_team_chat else [],
|
||||
'openwebui_channel_id': openwebui_channel_id
|
||||
}
|
||||
|
||||
# Save chats to file
|
||||
@@ -96,20 +120,47 @@ class ChatService:
|
||||
if chat_id not in self.chats:
|
||||
raise ValueError(f"Chat with ID {chat_id} not found")
|
||||
|
||||
chat = self.chats[chat_id]
|
||||
|
||||
# Create message data
|
||||
message = {
|
||||
'id': str(uuid.uuid4()),
|
||||
'content': content,
|
||||
'user_id': user_id if is_user_message else None,
|
||||
'is_user_message': is_user_message,
|
||||
'timestamp': datetime.utcnow().isoformat()
|
||||
'timestamp': datetime.now().isoformat()
|
||||
}
|
||||
|
||||
# Add message to chat
|
||||
self.chats[chat_id]['messages'].append(message)
|
||||
chat['messages'].append(message)
|
||||
|
||||
# Update chat timestamp
|
||||
self.chats[chat_id]['updated_at'] = datetime.utcnow().isoformat()
|
||||
chat['updated_at'] = datetime.now().isoformat()
|
||||
|
||||
# If this is a team chat with an OpenWebUI channel, send the message there too
|
||||
if chat['is_team_chat'] and 'openwebui_channel_id' in chat and chat['openwebui_channel_id']:
|
||||
try:
|
||||
# For AI responses, use a special AI user ID
|
||||
# This ensures the AI's messages are properly distinguished in OpenWebUI
|
||||
sender_id = "ai-assistant" if not is_user_message else user_id
|
||||
|
||||
# Format the message for OpenWebUI
|
||||
if is_user_message:
|
||||
# For user messages, use the regular format
|
||||
formatted_content = content
|
||||
else:
|
||||
# For AI responses, add a special prefix to make it clear it's from the AI
|
||||
# This helps visually distinguish AI responses in the channel
|
||||
formatted_content = f"🤖 {content}"
|
||||
|
||||
# Send message to OpenWebUI channel
|
||||
openwebui_channels.send_message(
|
||||
channel_id=chat['openwebui_channel_id'],
|
||||
message=formatted_content,
|
||||
user_id=sender_id
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"Error sending message to OpenWebUI channel: {str(e)}")
|
||||
|
||||
# Save chats to file
|
||||
self._save_chats()
|
||||
@@ -140,7 +191,7 @@ class ChatService:
|
||||
"""
|
||||
user_chats = []
|
||||
|
||||
for chat_id, chat in self.chats.items():
|
||||
for _, chat in self.chats.items():
|
||||
# Include private chats owned by the user
|
||||
if chat['user_id'] == user_id and not chat['is_team_chat']:
|
||||
user_chats.append(chat)
|
||||
@@ -173,6 +224,21 @@ class ChatService:
|
||||
if not chat['is_team_chat']:
|
||||
return False
|
||||
|
||||
# Add to OpenWebUI channel if available
|
||||
if 'openwebui_channel_id' in chat and chat['openwebui_channel_id']:
|
||||
try:
|
||||
# Add member to OpenWebUI channel
|
||||
openwebui_success = openwebui_channels.add_member(
|
||||
channel_id=chat['openwebui_channel_id'],
|
||||
user_id=user_id
|
||||
)
|
||||
|
||||
if not openwebui_success:
|
||||
print(f"Warning: Failed to add user {user_id} to OpenWebUI channel {chat['openwebui_channel_id']}")
|
||||
except Exception as e:
|
||||
print(f"Error adding member to OpenWebUI channel: {str(e)}")
|
||||
|
||||
# Add to local team members list
|
||||
if user_id not in chat['team_members']:
|
||||
chat['team_members'].append(user_id)
|
||||
self._save_chats()
|
||||
@@ -198,6 +264,21 @@ class ChatService:
|
||||
if not chat['is_team_chat']:
|
||||
return False
|
||||
|
||||
# Remove from OpenWebUI channel if available
|
||||
if 'openwebui_channel_id' in chat and chat['openwebui_channel_id']:
|
||||
try:
|
||||
# Remove member from OpenWebUI channel
|
||||
openwebui_success = openwebui_channels.remove_member(
|
||||
channel_id=chat['openwebui_channel_id'],
|
||||
user_id=user_id
|
||||
)
|
||||
|
||||
if not openwebui_success:
|
||||
print(f"Warning: Failed to remove user {user_id} from OpenWebUI channel {chat['openwebui_channel_id']}")
|
||||
except Exception as e:
|
||||
print(f"Error removing member from OpenWebUI channel: {str(e)}")
|
||||
|
||||
# Remove from local team members list
|
||||
if user_id in chat['team_members']:
|
||||
chat['team_members'].remove(user_id)
|
||||
self._save_chats()
|
||||
@@ -217,6 +298,22 @@ class ChatService:
|
||||
if chat_id not in self.chats:
|
||||
return False
|
||||
|
||||
chat = self.chats[chat_id]
|
||||
|
||||
# Delete OpenWebUI channel if this is a team chat
|
||||
if chat['is_team_chat'] and 'openwebui_channel_id' in chat and chat['openwebui_channel_id']:
|
||||
try:
|
||||
# Delete the OpenWebUI channel
|
||||
openwebui_success = openwebui_channels.delete_channel(
|
||||
channel_id=chat['openwebui_channel_id']
|
||||
)
|
||||
|
||||
if not openwebui_success:
|
||||
print(f"Warning: Failed to delete OpenWebUI channel {chat['openwebui_channel_id']}")
|
||||
except Exception as e:
|
||||
print(f"Error deleting OpenWebUI channel: {str(e)}")
|
||||
|
||||
# Delete the chat from local storage
|
||||
del self.chats[chat_id]
|
||||
self._save_chats()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user