Skip to content

Commit

Permalink
got chat duplication routing done
Browse files Browse the repository at this point in the history
  • Loading branch information
hagen-danswer committed Nov 24, 2024
1 parent 144d6b2 commit ec73a3c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
4 changes: 2 additions & 2 deletions backend/danswer/db/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ def duplicate_chat_session_for_user_from_slack(
db_session=db_session,
user_id=user.id if user else None,
persona_id=new_persona_id,
# This will likely be empty but the frontend will force a rename
description=chat_session.description,
# Set this to empty string so the frontend will force a rename
description="",
llm_override=chat_session.llm_override,
prompt_override=chat_session.prompt_override,
# Chat sessions from Slack should put people in the chat UI, not the search
Expand Down
47 changes: 45 additions & 2 deletions web/src/app/chat/ChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ export function ChatPage({

const { user, isAdmin, isLoadingUser, refreshUser } = useUser();

const slackChatId = searchParams.get("slackChatId");

const existingChatIdRaw = searchParams.get("chatId");
const [sendOnLoad, setSendOnLoad] = useState<string | null>(
searchParams.get(SEARCH_PARAM_NAMES.SEND_ON_LOAD)
Expand Down Expand Up @@ -403,6 +405,7 @@ export function ChatPage({
}
return;
}
setIsReady(true);
const shouldScrollToBottom =
visibleRange.get(existingChatSessionId) === undefined ||
visibleRange.get(existingChatSessionId)?.end == 0;
Expand Down Expand Up @@ -453,6 +456,7 @@ export function ChatPage({
}
}
setIsFetchingChatMessages(false);
console.log("stuff", chatSession);

// if this is a seeded chat, then kick off the AI message generation
if (
Expand All @@ -468,9 +472,12 @@ export function ChatPage({
});
// force re-name if the chat session doesn't have one
if (!chatSession.description) {
await nameChatSession(existingChatSessionId, seededMessage);
await nameChatSession(existingChatSessionId);
refreshChatSessions();
}
} else if (newMessageHistory.length === 2 && !chatSession.description) {
await nameChatSession(existingChatSessionId);
refreshChatSessions();
}
}

Expand Down Expand Up @@ -1428,7 +1435,7 @@ export function ChatPage({

if (!searchParamBasedChatSessionName) {
await new Promise((resolve) => setTimeout(resolve, 200));
await nameChatSession(currChatSessionId, currMessage);
await nameChatSession(currChatSessionId);
refreshChatSessions();
}

Expand Down Expand Up @@ -1810,6 +1817,42 @@ export function ChatPage({
};
}

// Add this near the top of the file where other useEffect hooks are
useEffect(() => {
const handleSlackChatRedirect = async () => {
if (!slackChatId) return;

setIsReady(false); // Set isReady to false before starting retrieval

try {
const response = await fetch("/api/chat/seed-chat-session-from-slack", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
chat_session_id: slackChatId,
}),
});

if (!response.ok) {
throw new Error("Failed to seed chat from Slack");
}

const data = await response.json();
router.push(data.redirect_url);
} catch (error) {
console.error("Error seeding chat from Slack:", error);
setPopup({
message: "Failed to load chat from Slack",
type: "error",
});
}
};

handleSlackChatRedirect();
}, [searchParams, router]); // Add any other dependencies needed

return (
<>
<HealthCheckBanner />
Expand Down
3 changes: 1 addition & 2 deletions web/src/app/chat/lib.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export async function* sendMessage({
yield* handleSSEStream<PacketType>(response);
}

export async function nameChatSession(chatSessionId: string, message: string) {
export async function nameChatSession(chatSessionId: string) {
const response = await fetch("/api/chat/rename-chat-session", {
method: "PUT",
headers: {
Expand All @@ -212,7 +212,6 @@ export async function nameChatSession(chatSessionId: string, message: string) {
body: JSON.stringify({
chat_session_id: chatSessionId,
name: null,
first_message: message,
}),
});
return response;
Expand Down

0 comments on commit ec73a3c

Please sign in to comment.