fixed team chats

This commit is contained in:
Iyeoluwa Akinrinola
2025-05-16 18:03:15 +01:00
parent 463923278a
commit 730009ae87
21 changed files with 1655 additions and 63 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
# API configuration
API_HOST=0.0.0.0
API_PORT=5251
PUBLIC_URL=http://your-public-url:5251 # Public URL for webhooks, needed for OpenWebUI to send channel messages
API_PORT=5252
PUBLIC_URL=http://your-public-url:5252 # Public URL for webhooks, needed for OpenWebUI to send channel messages
# OpenWebUI configuration
OPENWEBUI_URL=http://104.225.217.215:8080
+33 -1
View File
@@ -623,15 +623,47 @@ async def channel_message_webhook(request: ChannelMessageWebhook):
print("=" * 50)
# Find the chat associated with this OpenWebUI channel
print(f"Looking for chat with OpenWebUI channel ID: {request.channel_id}")
print(f"Number of chats in system: {len(chat_service.chats)}")
# Debug: Print all chats and their channel IDs
print("All chats in the system:")
for cid, chat in chat_service.chats.items():
is_team = chat.get('is_team_chat', False)
channel_id = chat.get('openwebui_channel_id', 'None')
print(f" Chat ID: {cid}, Is Team Chat: {is_team}, OpenWebUI Channel ID: {channel_id}")
chat_id = None
for cid, chat in chat_service.chats.items():
if chat.get('is_team_chat') and chat.get('openwebui_channel_id') == request.channel_id:
chat_id = cid
print(f"Found matching chat: {cid}")
break
if not chat_id:
print(f"No chat found for OpenWebUI channel {request.channel_id}")
return {"status": "error", "message": "No chat found for this channel"}
# If no chat exists for this channel, create one
print("Creating a new team chat for this channel...")
try:
new_chat_id = chat_service.create_chat(
user_id=request.user_id,
title=f"Channel Chat {request.channel_id}",
model_id=config.DEFAULT_MODEL,
is_team_chat=True
)
# Manually set the OpenWebUI channel ID for this chat
chat_service.chats[new_chat_id]['openwebui_channel_id'] = request.channel_id
chat_service._save_chats()
print(f"Created new chat with ID {new_chat_id} for channel {request.channel_id}")
# Use this new chat
chat_id = new_chat_id
except Exception as e:
print(f"Error creating new chat for channel: {str(e)}")
return {"status": "error", "message": "No chat found for this channel and failed to create one"}
# Skip messages from the AI assistant to avoid loops
if request.user_id == "ai-assistant":
+10
View File
@@ -69,6 +69,11 @@ class ChatService:
openwebui_channel_id = None
if is_team_chat:
try:
print("=" * 50)
print(f"Creating team chat with title: {chat_title}")
print(f"OpenWebUI URL: {openwebui_channels.openwebui_url}")
print(f"OpenWebUI API Key: {openwebui_channels.openwebui_api_key[:5]}..." if openwebui_channels.openwebui_api_key else "No API key")
# Create a channel in OpenWebUI
channel_response = openwebui_channels.create_channel(
name=chat_title,
@@ -76,13 +81,18 @@ class ChatService:
is_private=True # Team chats are private by default
)
print(f"Channel response: {json.dumps(channel_response, indent=2) if channel_response else 'None'}")
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")
print("=" * 50)
except Exception as e:
print("=" * 50)
print(f"Error creating OpenWebUI channel: {str(e)}")
print("=" * 50)
# Create chat data
self.chats[chat_id] = {