fix polling enable bug

This commit is contained in:
Mathew Pareles 2024-12-29 23:45:46 -05:00
parent 3a3bc24fef
commit 0c10cb73f6
3 changed files with 33 additions and 14 deletions

View file

@ -85,7 +85,7 @@ export class RefreshModelService extends Disposable implements IRefreshModelServ
for (const providerName of refreshableProviderNames) {
const { _enabled: enabled } = this.voidSettingsService.state.settingsOfProvider[providerName]
this.refreshModels(providerName, !enabled, { shouldPoll: true, isInternal: true })
this.refreshModels(providerName, !enabled, { isPolling: true, isInternal: true })
// every time providerName.enabled changes, refresh models too, like a useEffect
let relevantVals = () => refreshBasedOn[providerName].map(settingName => this.voidSettingsService.state.settingsOfProvider[providerName][settingName])
@ -101,7 +101,7 @@ export class RefreshModelService extends Disposable implements IRefreshModelServ
// if it was just enabled, or there was a change and it wasn't to the enabled state, refresh
if ((enabled && !prevEnabled) || (!enabled && !prevEnabled)) {
// if user just clicked enable, refresh
this.refreshModels(providerName, !enabled, { shouldPoll: false, isInternal: true })
this.refreshModels(providerName, !enabled, { isPolling: false, isInternal: true })
}
else {
// else if user just clicked disable, don't refresh
@ -134,11 +134,11 @@ export class RefreshModelService extends Disposable implements IRefreshModelServ
// start listening for models (and don't stop until success)
async refreshModels(providerName: RefreshableProviderName, enableProviderOnSuccess?: boolean, options?: { shouldPoll?: boolean, isInternal?: boolean }) {
async refreshModels(providerName: RefreshableProviderName, enableProviderOnSuccess?: boolean, options?: { isPolling?: boolean, isInternal?: boolean }) {
const { shouldPoll, isInternal } = options ?? {}
const { isPolling, isInternal } = options ?? {}
console.log(`refreshModels, isInternal ${isInternal} shouldPoll ${shouldPoll}`)
console.log(`refreshModels, isInternal ${isInternal} isPolling ${isPolling}`)
this._clearProviderTimeout(providerName)
@ -169,13 +169,11 @@ export class RefreshModelService extends Disposable implements IRefreshModelServ
}
})
if (isInternal) {
this._setRefreshState(providerName, 'finished_invisible')
}
if (isInternal) this._setRefreshState(providerName, 'finished_invisible')
// check if we should poll
// if it was originally called as `shouldPoll` and if the `autoRefreshModels` flag is enabled
if (shouldPoll && this.voidSettingsService.state.featureFlagSettings.autoRefreshModels) {
// if it was originally called as `isPolling` and if the `autoRefreshModels` flag is enabled
if (isPolling && this.voidSettingsService.state.featureFlagSettings.autoRefreshModels) {
const timeoutId = setTimeout(() => this.refreshModels(providerName, enableProviderOnSuccess, options), REFRESH_INTERVAL)
this._setTimeoutId(providerName, timeoutId)
}

View file

@ -222,7 +222,7 @@ class VoidSettingsService extends Disposable implements IVoidSettingsService {
setDefaultModels(providerName: ProviderName, newDefaultModelNames: string[]) {
const { models } = this.state.settingsOfProvider[providerName]
const newDefaultModels = modelInfoOfDefaultNames(newDefaultModelNames)
const newDefaultModels = modelInfoOfDefaultNames(newDefaultModelNames, models)
const newModels = [
...newDefaultModels,
...models.filter(m => !m.isDefault), // keep any non-default models

View file

@ -14,9 +14,30 @@ export type VoidModelInfo = {
}
export const modelInfoOfDefaultNames = (modelNames: string[]): VoidModelInfo[] => {
export const modelInfoOfDefaultNames = (modelNames: string[], existingModels?: VoidModelInfo[]): VoidModelInfo[] => {
const isHidden = modelNames.length >= 10 // hide all models if there are a ton of them, and make user enable them individually
return modelNames.map((modelName, i) => ({ modelName, isDefault: true, isHidden }))
if (!existingModels) {
return modelNames.map((modelName, i) => ({ modelName, isDefault: true, isHidden, }))
} else {
// merge `modelNames` with `existingModels`
// keep existing `isHidden` property
const existingModelsMap: Record<string, VoidModelInfo> = {}
for (const em of existingModels) {
existingModelsMap[em.modelName] = em
}
return modelNames.map((modelName, i) => ({
modelName,
isDefault: true,
isHidden: !!existingModelsMap[modelName]?.isHidden,
}))
}
}
// https://docs.anthropic.com/en/docs/about-claude/models
@ -385,7 +406,7 @@ type FeatureFlagDisplayInfo = {
export const displayInfoOfFeatureFlag = (featureFlag: FeatureFlagName): FeatureFlagDisplayInfo => {
if (featureFlag === 'autoRefreshModels') {
return {
description: `Automatically enable local providers and models (like Ollama).`, // ${`refreshableProviderNames.map(providerName => titleOfProviderName(providerName)).join(', ')`}
description: `Automatically detect local providers and models (like Ollama).`, // ${`refreshableProviderNames.map(providerName => titleOfProviderName(providerName)).join(', ')`}
}
}
throw new Error(`featureFlagInfo: Unknown feature flag: "${featureFlag}"`)