Skip to content

Commit

Permalink
♻️ refactor: refactor the plugin and tool slice (lobehub#1437)
Browse files Browse the repository at this point in the history
* ♻️ refactor: refactor the plugin and tool slice

* ✅ test: update test

---------

Co-authored-by: arvinxx <arvinx@foxmail.com>
  • Loading branch information
lobehubbot and arvinxx authored Mar 1, 2024
1 parent bf29ec5 commit 003e230
Show file tree
Hide file tree
Showing 10 changed files with 1,091 additions and 964 deletions.
2 changes: 1 addition & 1 deletion src/store/chat/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { chatEnhanceSelectors } from './slices/enchance/selectors';
export { chatSelectors } from './slices/message/selectors';
export { chatToolSelectors } from './slices/tool/selectors';
export { topicSelectors } from './slices/topic/selectors';
66 changes: 1 addition & 65 deletions src/store/chat/slices/enchance/action.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { produce } from 'immer';
import pMap from 'p-map';
import { StateCreator } from 'zustand/vanilla';

import { chainLangDetect } from '@/chains/langDetect';
import { chainTranslate } from '@/chains/translate';
import { supportLocales } from '@/locales/resources';
import { chatService } from '@/services/chat';
import { fileService } from '@/services/file';
import { imageGenerationService } from '@/services/imageGeneration';
import { messageService } from '@/services/message';
import { chatSelectors } from '@/store/chat/selectors';
import { ChatStore } from '@/store/chat/store';
import { ChatTTS, ChatTranslate } from '@/types/message';
import { DallEImageItem } from '@/types/tool/dalle';
import { setNamespace } from '@/utils/storeDebug';

const n = setNamespace('enhance');
Expand All @@ -23,15 +19,11 @@ const n = setNamespace('enhance');
export interface ChatEnhanceAction {
clearTTS: (id: string) => Promise<void>;
clearTranslate: (id: string) => Promise<void>;
generateImageFromPrompts: (items: DallEImageItem[], id: string) => Promise<void>;
text2image: (id: string, data: DallEImageItem[]) => Promise<void>;
toggleDallEImageLoading: (key: string, value: boolean) => void;
translateMessage: (id: string, targetLang: string) => Promise<void>;
ttsMessage: (
id: string,
state?: { contentMd5?: string; file?: string; voice?: string },
) => Promise<void>;
updateImageItem: (id: string, updater: (data: DallEImageItem[]) => void) => Promise<void>;
updateMessageTTS: (id: string, data: Partial<ChatTTS> | false) => Promise<void>;
updateMessageTranslate: (id: string, data: Partial<ChatTranslate> | false) => Promise<void>;
}
Expand All @@ -50,53 +42,6 @@ export const chatEnhance: StateCreator<
await get().updateMessageTranslate(id, false);
},

generateImageFromPrompts: async (items, messageId) => {
const { toggleDallEImageLoading, updateImageItem } = get();
// eslint-disable-next-line unicorn/consistent-function-scoping
const getMessageById = (id: string) => chatSelectors.getMessageById(id)(get());

const message = getMessageById(messageId);
if (!message) return;

const parent = getMessageById(message!.parentId!);
const originPrompt = parent?.content;

await pMap(items, async (params, index) => {
toggleDallEImageLoading(messageId + params.prompt, true);
const url = await imageGenerationService.generateImage(params);

await updateImageItem(messageId, (draft) => {
draft[index].previewUrl = url;
});

toggleDallEImageLoading(messageId + params.prompt, false);

fileService
.uploadImageByUrl(url, {
metadata: { ...params, originPrompt: originPrompt },
name: `${originPrompt || params.prompt}_${index}.png`,
})
.then(({ id }) => {
updateImageItem(messageId, (draft) => {
draft[index].imageId = id;
draft[index].previewUrl = undefined;
});
});
});
},
text2image: async (id, data) => {
// const isAutoGen = settingsSelectors.isDalleAutoGenerating(useGlobalStore.getState());
// if (!isAutoGen) return;

await get().generateImageFromPrompts(data, id);
},
toggleDallEImageLoading: (key, value) => {
set(
{ dalleImageLoading: { ...get().dalleImageLoading, [key]: value } },
false,
n('toggleDallEImageLoading'),
);
},
translateMessage: async (id, targetLang) => {
const { toggleChatLoading, updateMessageTranslate, dispatchMessage } = get();

Expand Down Expand Up @@ -147,20 +92,11 @@ export const chatEnhance: StateCreator<
await get().updateMessageTTS(id, state);
},

updateImageItem: async (id, updater) => {
const message = chatSelectors.getMessageById(id)(get());
if (!message) return;

const data: DallEImageItem[] = JSON.parse(message.content);

const nextContent = produce(data, updater);
await get().updateMessageContent(id, JSON.stringify(nextContent));
},

updateMessageTTS: async (id, data) => {
await messageService.updateMessage(id, { tts: data as ChatTTS });
await get().refreshMessages();
},

updateMessageTranslate: async (id, data) => {
await messageService.updateMessage(id, { translate: data as ChatTranslate });
await get().refreshMessages();
Expand Down
Loading

0 comments on commit 003e230

Please sign in to comment.