✨ de-duplicate when merging arrays

This commit is contained in:
Nate Sesti 2024-02-27 12:17:15 -05:00
parent 19282ccf55
commit d3a0c35a83
2 changed files with 26 additions and 7 deletions

View file

@ -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
};

View file

@ -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 {