continue-custom/core/config/handler.ts
Nate Sesti 53b61288ec
🎨 Refactor core (#1281)
* 🎨 refactor out react context

* 🎨 refactor local storage

* 🎨 refactor IdeMessenger

* 🔥 removing unused files

* 🚚 move scripts

* ✅ setup VSCode integration testing

* 🎨 fix test-related stuff

* 🚧 testing

* 🎨 tweak terminal cmd+L

* 🧑‍💻 install biome in core

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 run biome check in binary

* 🧑‍💻 global biome config

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 fix a few biome warnings

* 🚧 WIP on protocols

* 🚧 progress, organizing, about to remove webviewCore protocol

* 🚧 now running

* 🚧 move indexing code to core

* 🚧 WIP on JetBrains indexing implementation

* 🎨 finish listDir

* 🚧 gui fixes

* 🏷️ update IMessenger.on return type

* 🐛 fix jetbrains types + IDE detection

* 🧑‍💻 set up debugging for binary with TcpMessenger

* 👷 fix prepackage.js

* 🧑‍💻 turn off debug mode for intellij

* 🐛 merge fixes

* 🐛 fixes after refactor

* 🐛 merge fixes

* 💄 increase font size in JB

* 🔀 merge changes

* 🩹 small fixes

* 🐛 fix jetbrains build

* 🩹 more jetbrains fixes

* 🎨 jetbrains improvements

* 🐛 fix lancedb prob in jetbrains build

* 👷 update jetbrains build process

* ✨ intellij problems context provider

* 👷 add script utils file

* 💚 fix jetbrains build regression

* 🎨 dynamic import transformers.js

* 🩹 small jetbrains updates

* ✨ folder context provider in jetbrains

* 🎨 many more jetbrains improvements

* 🔀 merge fixes

* ⚡️ small improvements

* 🎨 tell users transformers.js not supported

* 🎨 trial updates

* 📝 update jetbrains readmej

* ⚡️ only use smaller context for local autocomplete models

* ⚡️ improve bracket filter

* ✨ global .continue/.prompts folder

* 💄 improved model setup process

* 🥅 display VSCode req errs
2024-05-24 19:38:13 -07:00

111 lines
3 KiB
TypeScript

import {
BrowserSerializedContinueConfig,
ContinueConfig,
ContinueRcJson,
IContextProvider,
IDE,
ILLM,
} from "../index.js";
import { IdeSettings } from "../protocol/ideWebview.js";
import { Telemetry } from "../util/posthog.js";
import { finalToBrowserConfig, loadFullConfigNode } from "./load.js";
export class ConfigHandler {
private savedConfig: ContinueConfig | undefined;
private savedBrowserConfig?: BrowserSerializedContinueConfig;
private additionalContextProviders: IContextProvider[] = [];
constructor(
private readonly ide: IDE,
private ideSettingsPromise: Promise<IdeSettings>,
private readonly writeLog: (text: string) => Promise<void>,
private readonly onConfigUpdate: () => void,
) {
this.ide = ide;
this.ideSettingsPromise = ideSettingsPromise;
this.writeLog = writeLog;
this.onConfigUpdate = onConfigUpdate;
try {
this.loadConfig();
} catch (e) {
console.error("Failed to load config: ", e);
}
}
updateIdeSettings(ideSettings: IdeSettings) {
this.ideSettingsPromise = Promise.resolve(ideSettings);
this.reloadConfig();
}
reloadConfig() {
this.savedConfig = undefined;
this.savedBrowserConfig = undefined;
this.loadConfig();
this.onConfigUpdate();
}
async getSerializedConfig(): Promise<BrowserSerializedContinueConfig> {
if (!this.savedBrowserConfig) {
this.savedConfig = await this.loadConfig();
this.savedBrowserConfig = finalToBrowserConfig(this.savedConfig);
}
return this.savedBrowserConfig;
}
async loadConfig(): Promise<ContinueConfig> {
if (this.savedConfig) {
return this.savedConfig;
}
let workspaceConfigs: ContinueRcJson[] = [];
try {
workspaceConfigs = await this.ide.getWorkspaceConfigs();
} catch (e) {
console.warn("Failed to load workspace configs");
}
const ideInfo = await this.ide.getIdeInfo();
const uniqueId = await this.ide.getUniqueId();
this.savedConfig = await loadFullConfigNode(
this.ide,
workspaceConfigs,
await this.ideSettingsPromise,
ideInfo.ideType,
uniqueId,
this.writeLog,
);
this.savedConfig.allowAnonymousTelemetry =
this.savedConfig.allowAnonymousTelemetry &&
(await this.ide.isTelemetryEnabled());
// Setup telemetry only after (and if) we know it is enabled
await Telemetry.setup(
this.savedConfig.allowAnonymousTelemetry ?? true,
await this.ide.getUniqueId(),
ideInfo.extensionVersion,
);
(this.savedConfig.contextProviders ?? []).push(
...this.additionalContextProviders,
);
return this.savedConfig;
}
async llmFromTitle(title?: string): Promise<ILLM> {
const config = await this.loadConfig();
const model =
config.models.find((m) => m.title === title) || config.models[0];
if (!model) {
throw new Error("No model found");
}
return model;
}
registerCustomContextProvider(contextProvider: IContextProvider) {
this.additionalContextProviders.push(contextProvider);
this.reloadConfig();
}
}