Skip to content

Commit

Permalink
🐛 fix: 修正数组合并逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Sep 3, 2023
1 parent a776728 commit e36e621
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@emoji-mart/data": "^1",
"@emoji-mart/react": "^1",
"@icons-pack/react-simple-icons": "^9",
"@lobehub/chat-plugin-sdk": "^1.15.0",
"@lobehub/chat-plugin-sdk": "^1.15.1",
"@lobehub/chat-plugins-gateway": "^1.5.0",
"@lobehub/ui": "latest",
"@vercel/analytics": "^1",
Expand Down
4 changes: 2 additions & 2 deletions src/features/AgentSetting/store/reducers/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { produce } from 'immer';
import { merge } from 'lodash-es';

import { DEFAULT_AGENT_CONFIG } from '@/const/settings';
import { LobeAgentConfig } from '@/types/session';
import { merge } from '@/utils/merge';

export type ConfigDispatch =
| { config: Partial<any>; type: 'update' }
Expand All @@ -13,7 +13,7 @@ export const configReducer = (state: LobeAgentConfig, payload: ConfigDispatch):
switch (payload.type) {
case 'update': {
return produce(state, (draftState) => {
return merge({}, draftState, payload.config);
return merge(draftState, payload.config);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/features/AgentSetting/store/reducers/meta.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { produce } from 'immer';
import { merge } from 'lodash-es';

import { DEFAULT_AGENT_META } from '@/const/meta';
import { MetaData } from '@/types/meta';
import { merge } from '@/utils/merge';

export type MetaDataDispatch = { type: 'update'; value: Partial<MetaData> } | { type: 'reset' };

export const metaDataReducer = (state: MetaData, payload: MetaDataDispatch): MetaData => {
switch (payload.type) {
case 'update': {
return produce(state, (draftState) => {
return merge({}, draftState, payload.value);
return merge(draftState, payload.value);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/store/session/slices/agentConfig/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { t } from 'i18next';
import { merge } from 'lodash-es';

import { DEFAULT_AVATAR, DEFAULT_BACKGROUND_COLOR } from '@/const/meta';
import { SessionStore } from '@/store/session';
import { LanguageModel } from '@/types/llm';
import { MetaData } from '@/types/meta';
import { merge } from '@/utils/merge';

import { sessionSelectors } from '../session/selectors';
import { initialLobeAgentConfig } from './initialState';
Expand All @@ -19,7 +19,7 @@ const currentAgentTitle = (s: SessionStore) => currentAgentMeta(s)?.title || t('

const currentAgentConfig = (s: SessionStore) => {
const session = sessionSelectors.currentSession(s);
return merge({}, initialLobeAgentConfig, session?.config);
return merge(initialLobeAgentConfig, session?.config);
};

const currentAgentSystemRole = (s: SessionStore) => {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { merge as _merge, mergeWith } from 'lodash-es';

/**
* 用于合并对象,如果是数组则直接替换
* @param target
* @param source
*/
export const merge: typeof _merge = <T = object>(target: T, source: T) =>
mergeWith({}, target, source, (obj, src) => {
if (Array.isArray(obj)) return src;
});

0 comments on commit e36e621

Please sign in to comment.