From d3a0c35a838ac642499bcd669d9088fef649208d Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Tue, 27 Feb 2024 12:17:15 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20de-duplicate=20when=20merging=20arr?= =?UTF-8?q?ays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/config/load.ts | 13 ++++++++++--- core/util/merge.ts | 20 ++++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) 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 {