Skip to content

Commit

Permalink
fix(chat): fix reset filters after logout (Issue #2989, #2952) (#3010)
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaBondar authored Jan 24, 2025
1 parent a6f64c0 commit 41f9511
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 45 deletions.
60 changes: 37 additions & 23 deletions apps/chat/src/components/Chat/ChatHeader/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import {
getSelectedAddons,
getValidEntitiesFromIds,
} from '@/src/utils/app/conversation';
import {
doesModelAllowAddons,
doesModelAllowSystemPrompt,
doesModelAllowTemperature,
} from '@/src/utils/app/models';

import { Conversation } from '@/src/types/chat';
import { EntityType, ScreenState } from '@/src/types/common';
Expand All @@ -36,6 +41,8 @@ import { PublicationActions } from '@/src/store/publication/publication.reducers
import { SettingsSelectors } from '@/src/store/settings/settings.reducers';
import { UISelectors } from '@/src/store/ui/ui.reducers';

import { FALLBACK_TEMPERATURE } from '@/src/constants/default-ui-settings';

import { ConversationContextMenu } from '@/src/components/Chat/ConversationContextMenu';
import { ConfirmDialog } from '@/src/components/Common/ConfirmDialog';

Expand Down Expand Up @@ -267,6 +274,7 @@ export const ChatHeader = Inversify.register(
</span>
{model ? (
model.type !== EntityType.Application &&
doesModelAllowAddons(model) &&
(conversation.selectedAddons.length > 0 ||
(model.selectedAddons &&
model.selectedAddons.length > 0)) && (
Expand Down Expand Up @@ -309,21 +317,22 @@ export const ChatHeader = Inversify.register(
)
) : (
<>
{conversation.selectedAddons.length > 0 && (
<span
className="flex items-center gap-2"
data-qa="chat-addons"
>
{conversation.selectedAddons.map((addon) => (
<ModelIcon
key={addon}
entityId={addon}
size={iconSize}
entity={addonsMap[addon]}
/>
))}
</span>
)}
{doesModelAllowAddons(model) &&
conversation.selectedAddons.length > 0 && (
<span
className="flex items-center gap-2"
data-qa="chat-addons"
>
{conversation.selectedAddons.map((addon) => (
<ModelIcon
key={addon}
entityId={addon}
size={iconSize}
entity={addonsMap[addon]}
/>
))}
</span>
)}
</>
)}
</>
Expand All @@ -344,22 +353,27 @@ export const ChatHeader = Inversify.register(
: undefined
}
systemPrompt={
model?.type === EntityType.Model
model?.type === EntityType.Model &&
doesModelAllowSystemPrompt(model)
? conversation.prompt
: ''
}
temperature={
model?.type !== EntityType.Application
? conversation.temperature
? doesModelAllowTemperature(model)
? conversation.temperature
: FALLBACK_TEMPERATURE
: null
}
selectedAddons={
model
? selectedAddons
: getValidEntitiesFromIds(
conversation.selectedAddons,
addonsMap,
)
doesModelAllowAddons(model)
? model
? selectedAddons
: getValidEntitiesFromIds(
conversation.selectedAddons,
addonsMap,
)
: null
}
/>
}
Expand Down
12 changes: 4 additions & 8 deletions apps/chat/src/components/Header/User/UserDesktop.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*eslint-disable @next/next/no-img-element*/
import { IconSettings } from '@tabler/icons-react';
import { signIn, signOut, useSession } from 'next-auth/react';
import { useCallback, useState } from 'react';
import { useState } from 'react';

import { useTranslation } from 'next-i18next';

import { useLogout } from '@/src/hooks/useLogout';

import { Translation } from '@/src/types/translation';

import { useAppDispatch } from '@/src/store/hooks';
Expand All @@ -24,13 +25,8 @@ export const UserDesktop = Inversify.register('UserDesktop', () => {
const [isOpen, setIsOpen] = useState(false);
const [isLogoutConfirmationOpened, setIsLogoutConfirmationOpened] =
useState(false);
const { data: session } = useSession();
const { session, handleLogout } = useLogout();
const dispatch = useAppDispatch();
const handleLogout = useCallback(() => {
session
? signOut({ redirect: true })
: signIn('azure-ad', { redirect: true });
}, [session]);

return (
<>
Expand Down
11 changes: 4 additions & 7 deletions apps/chat/src/components/Header/User/UserMobile.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/*eslint-disable @next/next/no-img-element*/
import { IconSettings } from '@tabler/icons-react';
import { signIn, signOut, useSession } from 'next-auth/react';
import { useSession } from 'next-auth/react';
import { useCallback, useState } from 'react';

import { useTranslation } from 'next-i18next';

import classNames from 'classnames';

import { useLogout } from '@/src/hooks/useLogout';

import { Translation } from '@/src/types/translation';

import { useAppDispatch, useAppSelector } from '@/src/store/hooks';
Expand Down Expand Up @@ -70,16 +72,11 @@ const UserSettings = () => {
};

const Logout = () => {
const { data: session } = useSession();
const { session, handleLogout } = useLogout();
const { t } = useTranslation(Translation.Header);
const [isLogoutConfirmationOpened, setIsLogoutConfirmationOpened] =
useState(false);

const handleLogout = useCallback(() => {
session
? signOut({ redirect: true })
: signIn('azure-ad', { redirect: true });
}, [session]);
return (
<>
<div
Expand Down
8 changes: 5 additions & 3 deletions apps/chat/src/constants/default-ui-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const FALLBACK_ASSISTANT_SUBMODEL_ID = 'gpt-4';

export const MAX_ENTITY_LENGTH = 160;

export const DEFAULT_TEMPERATURE = parseFloat(
process.env.NEXT_PUBLIC_DEFAULT_TEMPERATURE ?? '1',
);
export const FALLBACK_TEMPERATURE = 1;

export const DEFAULT_TEMPERATURE = process.env.NEXT_PUBLIC_DEFAULT_TEMPERATURE
? parseFloat(process.env.NEXT_PUBLIC_DEFAULT_TEMPERATURE)
: FALLBACK_TEMPERATURE;
15 changes: 15 additions & 0 deletions apps/chat/src/hooks/useLogout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { signIn, signOut, useSession } from 'next-auth/react';
import { useCallback } from 'react';

export const useLogout = () => {
const { data: session } = useSession();
const handleLogout = useCallback(() => {
session
? signOut({ redirect: true, callbackUrl: '/' })
: signIn('azure-ad', { redirect: true });
}, [session]);
return {
session,
handleLogout,
};
};
7 changes: 5 additions & 2 deletions apps/chat/src/pages/api/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import { ChatBody } from '@/src/types/chat';
import { EntityType } from '@/src/types/common';

import { DEFAULT_SYSTEM_PROMPT } from '@/src/constants/default-server-settings';
import { DEFAULT_TEMPERATURE } from '@/src/constants/default-ui-settings';
import {
DEFAULT_TEMPERATURE,
FALLBACK_TEMPERATURE,
} from '@/src/constants/default-ui-settings';
import { errorsMessages } from '@/src/constants/errors';

import { authOptions } from './auth/[...nextauth]';
Expand Down Expand Up @@ -65,7 +68,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {

let temperatureToUse = temperature;
if (!doesModelAllowTemperature(model)) {
temperatureToUse = 1;
temperatureToUse = FALLBACK_TEMPERATURE;
} else if (
!temperatureToUse &&
temperatureToUse !== 0 &&
Expand Down
5 changes: 3 additions & 2 deletions apps/chat/src/store/conversations/conversations.epics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import { LOCAL_BUCKET, resetShareEntity } from '@/src/constants/chat';
import {
DEFAULT_CONVERSATION_NAME,
DEFAULT_TEMPERATURE,
FALLBACK_TEMPERATURE,
} from '@/src/constants/default-ui-settings';
import { errorsMessages } from '@/src/constants/errors';
import { MarketplaceQueryParams } from '@/src/constants/marketplace';
Expand Down Expand Up @@ -1352,7 +1353,7 @@ const streamMessageEpic: AppEpic = (action$, state$) =>
: undefined,
temperature: doesModelAllowTemperature(lastModel)
? payload.conversation.temperature
: 1,
: FALLBACK_TEMPERATURE,
selectedAddons: doesModelAllowAddons(lastModel) ? selectedAddons : [],
};
}
Expand All @@ -1361,7 +1362,7 @@ const streamMessageEpic: AppEpic = (action$, state$) =>
assistantModel: modelsMap[assistantModelId],
temperature: doesModelAllowTemperature(lastModel)
? payload.conversation.temperature
: 1,
: FALLBACK_TEMPERATURE,
selectedAddons: doesModelAllowAddons(lastModel) ? selectedAddons : [],
};
}
Expand Down

0 comments on commit 41f9511

Please sign in to comment.