Skip to content

Commit

Permalink
Fix aphrodite response regeneration
Browse files Browse the repository at this point in the history
  • Loading branch information
miku448 committed Sep 29, 2023
1 parent 733f567 commit d7514ed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ export const BotDisplay = () => {
});

const lastMemoryLine = memoryLines[memoryLines.length - 2];
const lastResponse = memoryLines[memoryLines.length - 1];

event.preventDefault();
setResponseIds((_responseIds) => {
Expand All @@ -268,9 +267,12 @@ export const BotDisplay = () => {
});
const aphrodite = getAphroditeConfig();

if (aphrodite.enabled && lastMemoryLine.id && lastResponse.id) {
await platformAPI.deleteChatMessage(aphrodite.chatId, lastResponse?.id || '');
await platformAPI.deleteChatMessage(aphrodite.chatId, lastMemoryLine?.id || '');
if (aphrodite.enabled && lastMemoryLine.id) {
const lastCommandId = platformAPI.getLastMessageId();
if (lastCommandId) {
platformAPI.deleteChatMessage(aphrodite.chatId, lastCommandId || '');
platformAPI.deleteChatMessage(aphrodite.chatId, lastMemoryLine?.id || '');
}
}

const result = botFactory
Expand Down Expand Up @@ -300,10 +302,10 @@ export const BotDisplay = () => {
const text = responsesStore.get(responseId)?.text;
const aphrodite = getAphroditeConfig();
if (aphrodite.enabled && memoryLines?.length) {
const lastCommandId = platformAPI.getLastMessageId();
const newresponse = responsesStore.get(responseId);
const lastMemoryLine = memoryLines[memoryLines.length - 1];
if (lastMemoryLine?.id) {
platformAPI.editChatMessage(aphrodite.chatId, lastMemoryLine.id, newresponse?.text || '');
if (lastCommandId) {
platformAPI.editChatMessage(aphrodite.chatId, lastCommandId, newresponse?.text || '');
}
}

Expand Down
8 changes: 4 additions & 4 deletions apps/browser-chat/src/libs/botLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export function useBot(): {
subject: message.isBot ? decoratedConfig.bot_name : _botData.settings.text.name,
text: message.text,
}));
const sceneIds = chat.data.chatMessages.filter(message => message.isBot).map((message) => {
const botMessages = chat.data.chatMessages.filter(message => message.isBot).map((message) => {
const firstScenario = res.card?.data.extensions.mikugg.scenarios.find(_scenario => message.sceneId === _scenario.id);
const firstEmotionGroup = res.card?.data.extensions.mikugg.emotion_groups.find(emotion_group => firstScenario?.emotion_group === emotion_group.id);
let firstImage = firstEmotionGroup?.emotions?.find(emotion => emotion?.id === message.emotionId)?.source[0] || firstEmotionGroup?.emotions[0].source[0];
Expand All @@ -353,13 +353,13 @@ export function useBot(): {
fillResponse(message.id, "audio", '');
fillResponse(message.id, "scene", message.sceneId);

return message.sceneId;
return message;
});
const lastId = sceneIds.length ? sceneIds[sceneIds.length - 1] : null;
const lastBotMessage = botMessages.length ? botMessages[botMessages.length - 1] : null;
const emotionInterpreter = decoratedConfig.outputListeners.find(listener => listener.service === MikuExtensions.Services.ServicesNames.SBertEmotionInterpreter);
if (emotionInterpreter) {
const bot = botFactory.getInstance();
bot?.changeContext(lastId || res.card.data.extensions.mikugg.start_scenario)
bot?.changeContext(lastBotMessage?.sceneId || res.card.data.extensions.mikugg.start_scenario)
}
}
if (!isDifferentBot && memoryLines.length) {
Expand Down
12 changes: 11 additions & 1 deletion apps/browser-chat/src/libs/platformAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface ChatMessageInput {

class PlatformAPIClient {
private readonly client: AxiosInstance;
private lastMessageId: string | null = null;

constructor(token?: string) {
this.client = axios.create({ baseURL: import.meta.env.VITE_PLATFORM_API || '', withCredentials: true });
Expand All @@ -54,6 +55,9 @@ class PlatformAPIClient {

async getChat(chatId: string): Promise<APIResponse<Chat>> {
const response: AxiosResponse<Chat> = await this.client.get(`/chat/${chatId}`);
if (response.data.chatMessages.length) {
this.lastMessageId = response.data.chatMessages[response.data.chatMessages.length - 1].id;
}
return response;
}

Expand All @@ -66,7 +70,9 @@ class PlatformAPIClient {
firstMessage: ChatMessage,
secondMessage: ChatMessage,
}>> {
return await this.client.post(`/chat/${chatId}/messages`, { firstMessage, secondMessage });
const response = await this.client.post(`/chat/${chatId}/messages`, { firstMessage, secondMessage });
this.lastMessageId = response.data.secondMessage.id;
return response;
}

async editChatMessage(chatId: string, messageId: string, text: string): Promise<APIResponse<ChatMessage>> {
Expand All @@ -76,6 +82,10 @@ class PlatformAPIClient {
async deleteChatMessage(chatId: string, messageId: string): Promise<APIResponse<ChatMessage>> {
return await this.client.delete(`/chat/${chatId}/message/${messageId}`);
}

getLastMessageId(): string | null {
return this.lastMessageId;
}
}

export default new PlatformAPIClient();

0 comments on commit d7514ed

Please sign in to comment.