-
-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto save settings #874
base: master
Are you sure you want to change the base?
Auto save settings #874
Conversation
2528c14
to
d6df88e
Compare
} | ||
return BrevilabsClient.instance; | ||
} | ||
|
||
private checkLicenseKey() { | ||
if (!this.licenseKey) { | ||
if (!getSettings().plusLicenseKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change ensures BrevilabsClient always uses the latest license key
d6df88e
to
5b66df2
Compare
subscribeToChainTypeChange(() => | ||
this.setChain(getChainType(), { | ||
debug: getSettings().debug, | ||
refreshIndex: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This chunk of code re-implements reindex on chain type change
5b66df2
to
6608b0b
Compare
): void { | ||
this.langChainParams[key] = value; | ||
this.createChainWithNewModel(); | ||
subscribeToModelKeyChange(() => this.createChainWithNewModel()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chain needs to be recreated when model key or settings change.
Here it listens to all settings key changes, which is not necessary. But it will cover all edge cases in case someone adds new settings dependency in the future.
this.chatModelManager.setChatModel(customModel); | ||
// Must update the chatModel for chain because ChainFactory always | ||
// retrieves the old chain without the chatModel change if it exists! | ||
// Create a new chain with the new chatModel | ||
this.createChain(this.getLangChainParams().chainType, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
createChain and setChain has no functionality differences
@@ -138,30 +120,15 @@ export default class ChainManager { | |||
customModel = BUILTIN_CHAT_MODELS[0]; | |||
newModelKey = customModel.name + "|" + customModel.provider; | |||
} | |||
this.setLangChainParam("modelKey", newModelKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done within setChain
@@ -174,10 +141,7 @@ export default class ChainManager { | |||
this.validateChainType(chainType); | |||
|
|||
// Handle index refresh if needed | |||
if ( | |||
options.refreshIndex && | |||
(chainType === ChainType.VAULT_QA_CHAIN || chainType === ChainType.COPILOT_PLUS_CHAIN) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is done when setting refreshIndex
@@ -188,25 +152,14 @@ export default class ChainManager { | |||
|
|||
switch (chainType) { | |||
case ChainType.LLM_CHAIN: { | |||
// For initial load of the plugin | |||
if (options.forceNewCreation) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forceNewCreation is always true
@@ -261,9 +246,7 @@ export default class ChatModelManager { | |||
|
|||
const modelConfig = this.getModelConfig(model); | |||
|
|||
// MUST update it since chatModelManager is a singleton. | |||
this.getLangChainParams().modelKey = modelKey; | |||
new Notice(`Setting model: ${modelConfig.modelName}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am removing this notice because buildModelMap is called on any settings value change, which can be annoying for it to show when unrelated settings values are set
@@ -72,12 +56,6 @@ class VectorStoreManager { | |||
console.error("Failed to initialize Copilot database:", error); | |||
}); | |||
|
|||
// Initialize the rate limiter | |||
VectorDBManager.initialize({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is no longer needed
encryptionService: EncryptionService, | ||
getLangChainParams: () => LangChainParams | ||
) { | ||
constructor(app: App) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no subscription to settings needed in this manager because all the methods always use the latest settings value
@@ -24,42 +55,11 @@ export interface ModelConfig { | |||
enableCors?: boolean; | |||
} | |||
|
|||
export interface LangChainParams { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LangChainParams have been replaced with settings, modelKey and chainType.
src/aiState.ts
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is no longer needed as components are interacting with the hooks exposed from aiParams.ts
@@ -11,7 +11,7 @@ import { getAIResponse } from "@/langchainStream"; | |||
import ChainManager from "@/LLMProviders/chainManager"; | |||
import CopilotPlugin from "@/main"; | |||
import { Mention } from "@/mentions/Mention"; | |||
import { useSettingsValueContext } from "@/settings/contexts/SettingsValueContext"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
settings has become global. We don't need settings value context any more
b55e3cf
to
1089023
Compare
if (!usageStrategy) { | ||
console.warn("PromptUsageStrategy not initialize"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code was very fragile. Many callers do not have access a usage strategy instance. If they ever get called before main.ts, it will not work. With the settings refactoring, this class can instantiate its usageStrategy now.
@@ -60,13 +56,13 @@ export class CustomPromptProcessor { | |||
} | |||
|
|||
// Clean up promptUsageTimestamps | |||
this.usageStrategy?.removeUnusedPrompts(prompts.map((prompt) => prompt.title)).save(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
save is done automatically when any setting value changes now
} | ||
|
||
export default class EncryptionService { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't need a class if all the classes are static and there is no class state.
src/main.ts
Outdated
|
||
async onload(): Promise<void> { | ||
await this.loadSettings(); | ||
this.settingsUnsubscriber = subscribeToSettingsChange(() => { | ||
this.saveData(getSettings()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto save on any settings value change
@@ -379,17 +340,6 @@ export default class CopilotPlugin extends Plugin { | |||
}, | |||
}); | |||
|
|||
// Index vault to Copilot index on startup and after loading all commands |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already done in VectorStoreManager post init job
1089023
to
984a54c
Compare
@@ -550,27 +506,6 @@ export default class CopilotPlugin extends Plugin { | |||
return Array.from(modelMap.values()); | |||
} | |||
|
|||
mergeAllActiveModelsWithCoreModels(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to settings/model.ts
984a54c
to
fd23e95
Compare
@@ -276,24 +275,6 @@ export function areEmbeddingModelsSame( | |||
return model1 === model2; | |||
} | |||
|
|||
export function sanitizeSettings(settings: CopilotSettings): CopilotSettings { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to settings/model
|
||
private static getRateLimiter(): RateLimiter { | ||
if (!this.config) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config will always be set now
b45bf47
to
dbd2ccb
Compare
@@ -98,13 +92,13 @@ export class BrevilabsClient { | |||
method, | |||
headers: { | |||
"Content-Type": "application/json", | |||
Authorization: `Bearer ${this.licenseKey}`, | |||
Authorization: `Bearer ${getDecryptedKey(getSettings().plusLicenseKey)}`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found an existing bug! plus license key was not working with encryption enabled
@@ -507,8 +464,9 @@ export default class CopilotPlugin extends Plugin { | |||
active: true, | |||
}); | |||
} | |||
} else { | |||
this.app.workspace.revealLeaf(leaves[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found an existing bug! Reveal leaf will throw if the leaf is newly created
dbd2ccb
to
afdbaba
Compare
afdbaba
to
b1a662b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created a base class for confirm modal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Migrate the existing modal to use the new confirm modal. Also refine the styling of this modal
<div style={{ display: "flex", flexDirection: "column", gap: "1rem" }}> | ||
<h1 style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}> | ||
Copilot Settings | ||
<button onClick={() => new ResetSettingsConfirmModal(app, () => resetSettings()).open()}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a modal to confirm the change here. This button becomes more powerful than before and it can get annoying if people accidentally click on it and lose all of their keys
Goal: Enable Auto-Saving for Settings
This PR introduces auto-saving functionality for settings by leveraging the Jotai state management library.
Implementation Details:
subscribeToSettingsChange
in managers that need to reset on settings change.Notes: