Skip to content

Commit

Permalink
fix: replay as is stops working (#69)
Browse files Browse the repository at this point in the history
* fix: forbade to send 'custom content' to the 'model'

* fix: added selected addons to the restriction for the custom_content.state

* fix: clear message state when settings have been changed

* added model selector to select model by id

* fix: created additional check for the model selector
  • Loading branch information
denys-kolomiitsev authored Nov 13, 2023
1 parent 21ba7c2 commit 15cff6e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 30 deletions.
11 changes: 1 addition & 10 deletions src/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';

import { useTranslation } from 'next-i18next';

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

import { OpenAIEntityModel, OpenAIEntityModelID } from '../../types/openai';
Expand Down Expand Up @@ -47,16 +48,6 @@ import { PlaybackEmptyInfo } from './PlaybackEmptyInfo';

const scrollThrottlingTimeout = 250;

const clearStateForMessages = (messages: Message[]): Message[] => {
return messages.map((message) => ({
...message,
custom_content: {
...message.custom_content,
state: undefined,
},
}));
};

export const Chat = memo(() => {
const { t } = useTranslation('chat');

Expand Down
63 changes: 44 additions & 19 deletions src/store/conversations/conversations.epics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ import { AnyAction } from '@reduxjs/toolkit';
import { combineEpics } from 'redux-observable';

import { cleanConversationHistory } from '@/src/utils/app/clean';
import { clearStateForMessages } from '@/src/utils/app/clear-messages-state';
import {
isSettingsChanged,
saveConversations,
saveSelectedConversationIds,
} from '@/src/utils/app/conversation';
Expand All @@ -54,6 +56,7 @@ import {
ChatBody,
Conversation,
Message,
MessageSettings,
Playback,
RateBody,
} from '@/src/types/chat';
Expand Down Expand Up @@ -572,6 +575,7 @@ const streamMessageEpic: AppEpic = (action$, state$) =>
selectedAddons,
};
}

const chatBody: ChatBody = {
modelId: payload.conversation.model.id,
messages: payload.conversation.messages
Expand Down Expand Up @@ -931,28 +935,49 @@ const replayConversationEpic: AppEpic = (action$, state$) =>
);
}
const activeMessage = messagesStack[conv.replay.activeReplayIndex];
const { prompt, temperature, selectedAddons, assistantModelId } =
activeMessage.settings ? activeMessage.settings : conv;
const convWithNewSettings: Conversation =
!conv.replay.replayAsIs || !activeMessage.model
? conv
: {
...conv,
replay: {
...conv.replay,
isError: false,
},
model: { ...conv.model, ...activeMessage.model },
prompt: prompt,
temperature: temperature,
selectedAddons: selectedAddons,
assistantModelId: assistantModelId,
};
let updatedConversation: Conversation = conv;

if (
conv.replay.replayAsIs &&
activeMessage.model &&
activeMessage.model.id
) {
const { prompt, temperature, selectedAddons, assistantModelId } =
activeMessage.settings ? activeMessage.settings : conv;

const newConversationSettings: MessageSettings = {
prompt,
temperature,
selectedAddons,
assistantModelId,
};

const model =
ModelsSelectors.selectModel(state$.value, activeMessage.model.id) ??
conv.model;

const messages =
conv.model.id !== model.id ||
isSettingsChanged(conv, newConversationSettings)
? clearStateForMessages(conv.messages)
: conv.messages;

updatedConversation = {
...conv,
model: model,
messages,
replay: {
...conv.replay,
isError: false,
},
...newConversationSettings,
};
}

return concat(
of(
ConversationsActions.sendMessage({
conversation: convWithNewSettings,
conversation: updatedConversation,
deleteCount: payload.isRestart
? (conversation?.messages.length &&
(conversation.messages[conversation.messages.length - 1]
Expand All @@ -961,7 +986,7 @@ const replayConversationEpic: AppEpic = (action$, state$) =>
: 1)) ||
0
: 0,
activeReplayIndex: convWithNewSettings.replay.activeReplayIndex,
activeReplayIndex: updatedConversation.replay.activeReplayIndex,
message: activeMessage,
}),
),
Expand Down
14 changes: 14 additions & 0 deletions src/store/models/models.reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,34 @@ const rootSelector = (state: RootState): ModelsState => state.models;
const selectModelsIsLoading = createSelector([rootSelector], (state) => {
return state.isLoading;
});

const selectModelsError = createSelector([rootSelector], (state) => {
return state.error;
});

const selectModels = createSelector([rootSelector], (state) => {
return state.models;
});

const selectModelsMap = createSelector([rootSelector], (state) => {
return state.modelsMap;
});

const selectModel = createSelector(
[selectModelsMap, (_state, modelId: string) => modelId],
(modelsMap, modelId) => {
return modelsMap[modelId];
},
);

const selectDefaultModelId = createSelector([rootSelector], (state) => {
return state.defaultModelId;
});

const selectRecentModelsIds = createSelector([rootSelector], (state) => {
return state.recentModelsIds;
});

const selectRecentModels = createSelector(
[selectRecentModelsIds, selectModelsMap],
(recentModelsIds, modelsMap) => {
Expand All @@ -133,6 +146,7 @@ export const ModelsSelectors = {
selectDefaultModelId,
selectRecentModelsIds,
selectRecentModels,
selectModel,
};

export const ModelsActions = modelsSlice.actions;
11 changes: 11 additions & 0 deletions src/utils/app/clear-messages-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Message } from '@/src/types/chat';

export const clearStateForMessages = (messages: Message[]): Message[] => {
return messages.map((message) => ({
...message,
custom_content: {
...message.custom_content,
state: undefined,
},
}));
};
33 changes: 32 additions & 1 deletion src/utils/app/conversation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import toast from 'react-hot-toast';

import { Conversation } from '@/src/types/chat';
import { Conversation, MessageSettings } from '@/src/types/chat';
import {
OpenAIEntityAddon,
OpenAIEntityApplicationType,
Expand Down Expand Up @@ -72,3 +72,34 @@ export const getSelectedAddons = (
}
return null;
};

export const isSettingsChanged = (
conversation: Conversation,
newSettings: MessageSettings,
) => {
const isChanged = Object.keys(newSettings).some((key) => {
const convSetting = conversation[key as keyof Conversation];
const newSetting = newSettings[key as keyof MessageSettings];

if (Array.isArray(convSetting) && Array.isArray(newSetting)) {
if (convSetting.length !== newSetting.length) {
return true;
}

const sortedConvSetting = [...convSetting].sort();
const sortedNewSetting = [...newSetting].sort();

const isArraysEqual: boolean = sortedConvSetting.every(
(value, index) => value === sortedNewSetting[index],
);
return !isArraysEqual;
}

return (
conversation[key as keyof Conversation] !==
newSettings[key as keyof MessageSettings]
);
});

return isChanged;
};

0 comments on commit 15cff6e

Please sign in to comment.