Skip to content

Commit 5f7e2f8

Browse files
author
Tajudeen
committed
refactor: use shared resolveAutoModelSelection utility across all features
- Replace manual 'auto' model resolution in autocompleteService with shared utility - Replace manual 'auto' model resolution in quickEditActions with shared utility - Replace manual 'auto' model resolution in codeReviewService with shared utility - Improve error message in sendLLMMessage for auto model selection failures - Fix duration calculation bug in sendLLMMessage (use Date.now() instead of getMilliseconds) This ensures consistent auto model selection behavior across all features and reduces code duplication.
1 parent e4d50bc commit 5f7e2f8

4 files changed

Lines changed: 43 additions & 24 deletions

File tree

src/vs/workbench/contrib/cortexide/browser/autocompleteService.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -791,10 +791,16 @@ export class AutocompleteService extends Disposable implements IAutocompleteServ
791791

792792
// Detect if using local provider for prefix/suffix optimization
793793
const featureName: FeatureName = 'Autocomplete'
794-
const modelSelection = this._settingsService.state.modelSelectionOfFeature[featureName]
795-
const isLocal = modelSelection && modelSelection.providerName !== 'auto'
796-
? isLocalProvider(modelSelection.providerName, this._settingsService.state.settingsOfProvider)
797-
: false
794+
const modelSelection = this._settingsService.resolveAutoModelSelection(
795+
this._settingsService.state.modelSelectionOfFeature[featureName]
796+
)
797+
798+
if (!modelSelection || modelSelection.providerName === 'auto') {
799+
// No model available - skip autocomplete
800+
return []
801+
}
802+
803+
const isLocal = isLocalProvider(modelSelection.providerName, this._settingsService.state.settingsOfProvider)
798804

799805
const { shouldGenerate, predictionType, llmPrefix, llmSuffix, stopTokens } = getCompletionOptions(prefixAndSuffix, relevantContext, justAcceptedAutocompletion, isLocal)
800806

@@ -822,15 +828,11 @@ export class AutocompleteService extends Disposable implements IAutocompleteServ
822828
console.log('starting autocomplete...', predictionType)
823829

824830
const overridesOfModel = this._settingsService.state.overridesOfModel
825-
// Skip "auto" - it's not a real provider
826-
const modelSelectionOptions = modelSelection && !(modelSelection.providerName === 'auto' && modelSelection.modelName === 'auto')
827-
? this._settingsService.state.optionsOfModelSelection[featureName][modelSelection.providerName]?.[modelSelection.modelName]
828-
: undefined
831+
// Model selection is already resolved above, so we can safely access options
832+
const modelSelectionOptions = this._settingsService.state.optionsOfModelSelection[featureName]?.[modelSelection.providerName]?.[modelSelection.modelName]
829833

830834
// Warm up local model in background (fire-and-forget, doesn't block)
831-
if (modelSelection && modelSelection.providerName !== 'auto' && modelSelection.modelName !== 'auto') {
832-
this._modelWarmupService.warmupModelIfNeeded(modelSelection.providerName, modelSelection.modelName, featureName)
833-
}
835+
this._modelWarmupService.warmupModelIfNeeded(modelSelection.providerName, modelSelection.modelName, featureName)
834836

835837
// set parameters of `newAutocompletion` appropriately
836838
newAutocompletion.llmPromise = new Promise((resolve, reject) => {

src/vs/workbench/contrib/cortexide/browser/quickEditActions.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,12 @@ registerAction2(class extends Action2 {
164164

165165
if (!instruction) return
166166

167-
// Check for model selection
168-
const modelSelection = settingsService.state.modelSelectionOfFeature['Chat']
167+
// Check for model selection and resolve "auto" if needed
168+
const modelSelection = settingsService.resolveAutoModelSelection(
169+
settingsService.state.modelSelectionOfFeature['Chat']
170+
)
169171
if (!modelSelection) {
170-
notificationService.warn('Please select a model in CortexIDE Settings to use Inline Edit.')
172+
notificationService.error('No model provider configured. Please configure a model provider in CortexIDE Settings.')
171173
return
172174
}
173175

@@ -232,11 +234,15 @@ ${contextCode}
232234

233235
const userMessage = `Edit instruction: ${instruction}\n\nGenerate a SEARCH/REPLACE block for the selected code.`
234236

237+
// Ensure modelSelection is resolved and not null
238+
if (!modelSelection || modelSelection.providerName === 'auto') {
239+
notificationService.error('Failed to resolve model selection. Please configure a model provider in CortexIDE Settings.')
240+
return
241+
}
242+
235243
const chatOptions = settingsService.state.optionsOfModelSelection['Chat']
236-
// Skip "auto" - it's not a real provider
237-
const modelOptions = modelSelection && !(modelSelection.providerName === 'auto' && modelSelection.modelName === 'auto')
238-
? chatOptions[modelSelection.providerName]?.[modelSelection.modelName]
239-
: undefined
244+
// Model selection is already resolved above, so we can safely access options
245+
const modelOptions = chatOptions[modelSelection.providerName]?.[modelSelection.modelName]
240246
const overrides = settingsService.state.overridesOfModel
241247

242248
requestId = llmMessageService.sendLLMMessage({

src/vs/workbench/contrib/cortexide/common/codeReviewService.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,21 @@ Provide your review annotations as a JSON array:`;
176176

177177
// Get model selection from settings (use Chat feature model selection)
178178
const settings = this.settingsService.state;
179-
const modelSelection = settings.modelSelectionOfFeature['Chat'] || { providerName: 'auto', modelName: 'auto' };
180-
const modelOptions = modelSelection && !(modelSelection.providerName === 'auto' && modelSelection.modelName === 'auto')
181-
? settings.optionsOfModelSelection['Chat']?.[modelSelection.providerName]?.[modelSelection.modelName]
182-
: undefined;
179+
const modelSelection = this.settingsService.resolveAutoModelSelection(
180+
settings.modelSelectionOfFeature['Chat'] || { providerName: 'auto', modelName: 'auto' }
181+
);
182+
183+
if (!modelSelection) {
184+
return {
185+
uri,
186+
annotations: [],
187+
summary: 'No model provider configured. Please configure a model provider in CortexIDE Settings.',
188+
success: false,
189+
error: 'No models available',
190+
};
191+
}
192+
193+
const modelOptions = settings.optionsOfModelSelection['Chat']?.[modelSelection.providerName]?.[modelSelection.modelName];
183194
const overrides = settings.overridesOfModel;
184195

185196
// Call LLM directly

src/vs/workbench/contrib/cortexide/electron-main/llmMessage/sendLLMMessage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const sendLLMMessage = async ({
6868
const onFinalMessage: OnFinalMessage = (params) => {
6969
const { fullText, fullReasoning, toolCall } = params
7070
if (_didAbort) return
71-
captureLLMEvent(`${loggingName} - Received Full Message`, { messageLength: fullText.length, reasoningLength: fullReasoning?.length, duration: new Date().getMilliseconds() - submit_time.getMilliseconds(), toolCallName: toolCall?.name })
71+
captureLLMEvent(`${loggingName} - Received Full Message`, { messageLength: fullText.length, reasoningLength: fullReasoning?.length, duration: Date.now() - submit_time.getTime(), toolCallName: toolCall?.name })
7272
onFinalMessage_(params)
7373
}
7474

@@ -109,7 +109,7 @@ export const sendLLMMessage = async ({
109109
try {
110110
// Skip "auto" - it's not a real provider
111111
if (providerName === 'auto') {
112-
onError({ message: `Error: Cannot use "auto" provider - must resolve to a real model first.`, fullError: null })
112+
onError({ message: `Error: Cannot use "auto" provider - must resolve to a real model first. This usually means auto model selection failed. Please check your model provider settings or select a specific model.`, fullError: null })
113113
return
114114
}
115115
const implementation = sendLLMMessageToProviderImplementation[providerName]

0 commit comments

Comments
 (0)