diff --git a/core/config/load.ts b/core/config/load.ts index a978a3358..cdeff8db9 100644 --- a/core/config/load.ts +++ b/core/config/load.ts @@ -59,6 +59,13 @@ function resolveSerializedConfig(filepath: string): SerializedContinueConfig { return JSON.parse(content); } +const configMergeKeys = { + "models": (a: any, b: any) => a.title === b.title, + "contextProviders": (a: any, b: any) => a.name === b.name, + "slashCommands": (a: any, b: any) => a.name === b.name, + "customCommands": (a: any, b: any) => a.name === b.name, +} + function loadSerializedConfig( workspaceConfigs: ContinueRcJson[], remoteConfigServerUrl: URL | undefined, @@ -128,11 +135,11 @@ function loadSerializedConfig( const remoteConfigJson = resolveSerializedConfig( getConfigJsonPathForRemote(remoteConfigServerUrl) ); - config = mergeJson(config, remoteConfigJson, "merge"); + config = mergeJson(config, remoteConfigJson, "merge", configMergeKeys); } for (const workspaceConfig of workspaceConfigs) { - config = mergeJson(config, workspaceConfig, workspaceConfig.mergeBehavior); + config = mergeJson(config, workspaceConfig, workspaceConfig.mergeBehavior, configMergeKeys); } return config; @@ -455,5 +462,5 @@ export { intermediateToFinalConfig, loadFullConfigNode, serializedToIntermediateConfig, - type BrowserSerializedContinueConfig, + type BrowserSerializedContinueConfig }; diff --git a/core/util/merge.ts b/core/util/merge.ts index f65c047a1..2da14db47 100644 --- a/core/util/merge.ts +++ b/core/util/merge.ts @@ -5,7 +5,8 @@ type JsonObject = { [key: string]: any }; export function mergeJson( first: JsonObject, second: JsonObject, - mergeBehavior?: ConfigMergeType + mergeBehavior?: ConfigMergeType, + mergeKeys?: {[key: string]: (a: any, b: any) => boolean} ): any { first = { ...first }; @@ -20,10 +21,21 @@ export function mergeJson( } const firstValue = first[key]; - if (Array.isArray(secondValue)) { + if (Array.isArray(secondValue) && Array.isArray(firstValue)) { // Array - first[key] = [...firstValue, ...secondValue]; - } else if (typeof secondValue === "object") { + if (mergeKeys?.[key]) { + // Merge keys are used to determine whether an item form the second object should override one from the first + let keptFromFirst: any[] = []; + firstValue.forEach((item: any) => { + if (!secondValue.some((item2: any) => mergeKeys[key](item, item2))) { + keptFromFirst.push(item); + } + }) + first[key] = [...keptFromFirst, ...secondValue]; + } else { + first[key] = [...firstValue, ...secondValue]; + } + } else if (typeof secondValue === "object" && typeof firstValue === "object") { // Object first[key] = mergeJson(firstValue, secondValue, mergeBehavior); } else {