Skip to content
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

Adding support to save the locally selected model so that new chats don´t require repeated model selection #2438

Merged
merged 5 commits into from
Dec 21, 2023

Conversation

arafatkatze
Copy link
Contributor

@arafatkatze arafatkatze commented Dec 19, 2023

image

I was quite frustrated with the loading issue as well so I changed it. The video is pretty self explanatory. It saves the state even after you quit and load Cody again.

I have a strong suspicion that a lot of users will switch to GPT 4-Turbo once and then never switch back

Untitled.mov

Test plan

Just try it out and open close a few times

Copy link
Member

@valerybugakov valerybugakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also started looking into it because I want GPT-4 by default. TY!

Copy link
Contributor

@philipp-spiess philipp-spiess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! I would expect some kind of validation to go on though, did you look into this?

Removing @valerybugakov's approval not to merge this too eagerly since it could cause some nasty errors

@@ -205,7 +207,7 @@ export class ChatPanelsManager implements vscode.Disposable {
this.options.contextProvider,
this.options.platform
),
defaultModelID: defaultModel.model,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check if the saved model is actually a valid model name for the current configuration. Otherwise, if you switch from a dotcom account to enterprise or from pro to free, this may result in a wrong model identifier which, when used, might cause errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the right call. I was too eager to get into the release to start using it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems strange that we write the last selected model ID in SimpleChatPanelProvider, but read it in ChatPanelsManager to pass into SimpleChatPanelProvider as a constructor param. Where possible, it's better to localize state management. Could we move the localStorage.get call to an onReady method in SimpleChatPanelProvider that runs when we receive the ready message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the model selection to this function

private selectModel(models: ChatModelProvider[]): string {
// Check for the last selected model
const lastSelectedModelID = localStorage.get('model')
if (lastSelectedModelID) {
// If the last selected model exists in the list of models then we return it
const model = models.find(m => m.model === lastSelectedModelID)
if (model) {
return lastSelectedModelID
}
}
// If the user has not selected a model before then we return the default model
const defaultModel = models.find(m => m.default) || models[0]
if (!defaultModel) {
throw new Error('No chat model found in server-provided config')
}
return defaultModel.model
}

Also it takes into account that the model selected is valid for that user.

Could we move the localStorage.get call to an onReady method in SimpleChatPanelProvider that runs when we receive the ready message?

Not sure if I fully understand this but hopefully moving the model selection to a single place makes the code better than before.

beyang
beyang previously requested changes Dec 19, 2023
@@ -77,6 +77,7 @@ export type WebviewMessage =
| { command: 'abort' }
| { command: 'custom-prompt'; title: string; value?: CustomCommandType }
| { command: 'reload' }
| { command: 'saveModel'; model: string }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The chatModel command already exists; why not use that one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to store and load from storage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arafatkatze Not sure I follow: If we already send a chatModel message, why would we need another one to save that if the behavior is "always save the last one as default"? 🤔

@@ -139,7 +140,8 @@ export class ChatPanelsManager implements vscode.Disposable {
// Get the view column of the current active chat panel so that we can open a new one on top of it
const activePanelViewColumn = this.activePanelProvider?.webviewPanel?.viewColumn

const provider = this.createProvider()
const savedModel = localStorage.get('model')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's give the key a more distinctive name, like "lastSelectedModelID"

@@ -205,7 +207,7 @@ export class ChatPanelsManager implements vscode.Disposable {
this.options.contextProvider,
this.options.platform
),
defaultModelID: defaultModel.model,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems strange that we write the last selected model ID in SimpleChatPanelProvider, but read it in ChatPanelsManager to pass into SimpleChatPanelProvider as a constructor param. Where possible, it's better to localize state management. Could we move the localStorage.get call to an onReady method in SimpleChatPanelProvider that runs when we receive the ready message?

Copy link
Contributor

@philipp-spiess philipp-spiess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks crisp now but I’m still unsure why we need a new notification for this instead of reusing. Do you mind elaborating?

Copy link
Contributor

@philipp-spiess philipp-spiess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sharp! This looks good now. Let's add a change log entry though 🙇

@philipp-spiess
Copy link
Contributor

I wonder why the test still fails 🤔 Did you create the PR in a fork of the cody repo? You can now create your branches on this repo instead, that should help!

Copy link
Contributor

@philipp-spiess philipp-spiess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found one more nit I’m afraid. I don't feel great that we have a compute function but keep a copy of the value in the class properties. Can we avoid this somehow?

@@ -108,7 +109,7 @@ export class SimpleChatPanelProvider implements vscode.Disposable, IChatPanelPro
private readonly contextStatusAggregator = new ContextStatusAggregator()
private readonly editor: VSCodeEditor
private readonly treeView: TreeViewProvider
private readonly defaultModelID: string
private readonly modelID: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this as a member now that we can compute it based on the localStorage? Asking because I just noticed that we have this.modelID and this.chatmodel.modelID and we only update one at some point. It's better to not keep duplicate references around I think.

@@ -336,6 +355,8 @@ export class SimpleChatPanelProvider implements vscode.Disposable, IChatPanelPro
break
case 'chatModel':
this.chatModel.modelID = message.model
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also update this.modelID (and if yes, can we make it so that there is no duplication?)

Copy link
Contributor

@philipp-spiess philipp-spiess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bliss

@arafatkatze arafatkatze enabled auto-merge (squash) December 21, 2023 17:15
@arafatkatze arafatkatze enabled auto-merge (squash) December 21, 2023 17:23
@arafatkatze arafatkatze merged commit 9ae53a8 into sourcegraph:main Dec 21, 2023
25 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants