Skip to content

Commit

Permalink
fix e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
sergesha committed Aug 24, 2024
1 parent c8762c0 commit 9c9be4f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 107 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { MutableRefObject, useCallback } from 'react';

import { clearStateForMessages } from '@/src/utils/app/clear-messages-state';

import {
Conversation,
ConversationInfo,
ConversationsTemporarySettings,
LikeState,
MergedMessages,
Message,
Replay,
Role,
} from '@/src/types/chat';
import { EntityType } from '@/src/types/common';
import { DialAIEntityAddon, ModelsMap } from '@/src/types/models';

import { REPLAY_AS_IS_MODEL } from '@/src/constants/chat';
import { DEFAULT_ASSISTANT_SUBMODEL_ID } from '@/src/constants/default-ui-settings';

import { useConversationActions } from './useConversationActions';

export const useChatViewConversationHandlers = (
Expand All @@ -23,7 +30,6 @@ export const useChatViewConversationHandlers = (
>,
) => {
const {
applyChatSettings,
cancelPlayback,
deleteMessage,
rateMessage,
Expand All @@ -32,17 +38,77 @@ export const useChatViewConversationHandlers = (
stopStreamMessage,
unselectConversations,
updateConversation,
} = useConversationActions(modelsMap, addonsMap);
} = useConversationActions();

const applySelectedModel = useCallback(
(
conversation: Conversation,
modelId: string | undefined,
): Partial<Conversation> => {
if (modelId === REPLAY_AS_IS_MODEL && conversation.replay) {
return {
replay: {
...conversation.replay,
replayAsIs: true,
},
};
}
const newAiEntity = modelId ? modelsMap[modelId] : undefined;
if (!modelId || !newAiEntity) {
return {};
}

const updatedReplay: Replay | undefined = !conversation.replay?.isReplay
? conversation.replay
: {
...conversation.replay,
replayAsIs: false,
};
const updatedAddons =
conversation.replay &&
conversation.replay.isReplay &&
conversation.replay.replayAsIs &&
!updatedReplay?.replayAsIs
? conversation.selectedAddons.filter((addonId) => addonsMap[addonId])
: conversation.selectedAddons;

return {
model: { id: newAiEntity.reference },
assistantModelId:
newAiEntity.type === EntityType.Assistant
? DEFAULT_ASSISTANT_SUBMODEL_ID
: undefined,
replay: updatedReplay,
selectedAddons: updatedAddons,
};
},
[modelsMap, addonsMap],
);

const handleApplySettings = useCallback(() => {
applyChatSettings(
selectedConversations,
selectedConversationsTemporarySettings.current,
);
selectedConversations.forEach((conversation) => {
const temporarySettings =
selectedConversationsTemporarySettings.current[conversation.id];
if (temporarySettings) {
updateConversation(conversation.id, {
messages: clearStateForMessages(conversation.messages),
...applySelectedModel(conversation, temporarySettings.modelId),
prompt: temporarySettings.prompt,
temperature: temporarySettings.temperature,
assistantModelId: temporarySettings.currentAssistentModelId,
selectedAddons: temporarySettings.addonsIds.filter(
(addonId) => addonsMap[addonId],
),
isShared: temporarySettings.isShared,
});
}
});
}, [
applyChatSettings,
addonsMap,
applySelectedModel,
selectedConversations,
selectedConversationsTemporarySettings,
updateConversation,
]);

const handleLike = useCallback(
Expand Down Expand Up @@ -73,13 +139,17 @@ export const useChatViewConversationHandlers = (

const handleSelectModel = useCallback(
(conversation: Conversation, modelId: string) => {
const modelValues: Partial<Conversation> = modelsMap[modelId]
? { model: { id: modelId } }
: {}; // handle undefined modelId lookup
const newAiEntity = modelsMap[modelId];
if (!newAiEntity && modelId !== REPLAY_AS_IS_MODEL) {
return;
}

updateConversation(conversation.id, modelValues);
updateConversation(
conversation.id,
applySelectedModel(conversation, modelId),
);
},
[updateConversation, modelsMap],
[modelsMap, updateConversation, applySelectedModel],
);

const handleSelectAssistantSubModel = useCallback(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
import { useCallback } from 'react';
import { useDispatch } from 'react-redux';

import { clearStateForMessages } from '@/src/utils/app/clear-messages-state';

import {
Conversation,
ConversationInfo,
ConversationsTemporarySettings,
LikeState,
Message,
Replay,
} from '@/src/types/chat';
import { EntityType } from '@/src/types/common';
import { DialAIEntityModel } from '@/src/types/models';

import { ConversationsActions } from '@/src/store/conversations/conversations.reducers';

import { REPLAY_AS_IS_MODEL } from '@/src/constants/chat';
import { DEFAULT_ASSISTANT_SUBMODEL_ID } from '@/src/constants/default-ui-settings';

interface UseConversationActionsReturnType {
updateConversation: (
conversationId: string,
values: Partial<Conversation>,
) => void;
applyChatSettings: (
selectedConversations: Conversation[],
selectedConversationsTemporarySettings: Record<
string,
ConversationsTemporarySettings
>,
) => void;
rateMessage: (
conversationId: string,
messageIndex: number,
Expand All @@ -49,57 +33,9 @@ interface UseConversationActionsReturnType {
cancelPlayback: () => void;
}

export function useConversationActions(
modelsMap: Partial<Record<string, DialAIEntityModel>>,
addonsMap: Record<string, unknown>,
): UseConversationActionsReturnType {
export function useConversationActions(): UseConversationActionsReturnType {
const dispatch = useDispatch();

const applySelectedModel = useCallback(
(
conversation: Conversation,
modelId: string | undefined,
): Partial<Conversation> => {
if (modelId === REPLAY_AS_IS_MODEL && conversation.replay) {
return {
replay: {
...conversation.replay,
replayAsIs: true,
},
};
}
const newAiEntity = modelId ? modelsMap[modelId] : undefined;
if (!modelId || !newAiEntity) {
return {};
}

const updatedReplay: Replay | undefined = !conversation.replay?.isReplay
? conversation.replay
: {
...conversation.replay,
replayAsIs: false,
};
const updatedAddons =
conversation.replay &&
conversation.replay.isReplay &&
conversation.replay.replayAsIs &&
!updatedReplay?.replayAsIs
? conversation.selectedAddons.filter((addonId) => addonsMap[addonId])
: conversation.selectedAddons;

return {
model: { id: newAiEntity.reference },
assistantModelId:
newAiEntity.type === EntityType.Assistant
? DEFAULT_ASSISTANT_SUBMODEL_ID
: undefined,
replay: updatedReplay,
selectedAddons: updatedAddons,
};
},
[modelsMap, addonsMap],
);

const updateConversation = useCallback(
(conversationId: string, values: Partial<Conversation>) => {
dispatch(
Expand All @@ -109,35 +45,6 @@ export function useConversationActions(
[dispatch],
);

const applyChatSettings = useCallback(
(
selectedConversations: Conversation[],
selectedConversationsTemporarySettings: Record<
string,
ConversationsTemporarySettings
>,
) => {
selectedConversations.forEach((conversation) => {
const temporarySettings =
selectedConversationsTemporarySettings[conversation.id];
if (temporarySettings) {
updateConversation(conversation.id, {
messages: clearStateForMessages(conversation.messages),
...applySelectedModel(conversation, temporarySettings.modelId),
prompt: temporarySettings.prompt,
temperature: temporarySettings.temperature,
assistantModelId: temporarySettings.currentAssistentModelId,
selectedAddons: temporarySettings.addonsIds.filter(
(addonId) => addonsMap[addonId],
),
isShared: temporarySettings.isShared,
});
}
});
},
[updateConversation, applySelectedModel, addonsMap],
);

const rateMessage = useCallback(
(conversationId: string, messageIndex: number, rate: LikeState) => {
dispatch(
Expand Down Expand Up @@ -200,7 +107,6 @@ export function useConversationActions(
}, [dispatch]);

return {
applyChatSettings,
cancelPlayback,
deleteMessage,
rateMessage,
Expand Down

0 comments on commit 9c9be4f

Please sign in to comment.