Skip to content

Commit f5d7ba1

Browse files
authored
Fix provider model loading race conditions (#8836)
1 parent 8870348 commit f5d7ba1

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

src/api/providers/roo.ts

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,14 @@ export class RooHandler extends BaseOpenAiCompatibleProvider<string> {
5858
const cloudService = CloudService.instance
5959

6060
this.authStateListener = (state: { state: AuthState }) => {
61-
if (state.state === "active-session") {
62-
const newToken = cloudService.authService?.getSessionToken()
63-
this.client = new OpenAI({
64-
baseURL: this.baseURL,
65-
apiKey: newToken ?? "unauthenticated",
66-
defaultHeaders: DEFAULT_HEADERS,
67-
})
68-
69-
// Flush cache and reload models with the new auth token
70-
flushModels("roo")
71-
.then(() => {
72-
return this.loadDynamicModels(this.fetcherBaseURL, newToken)
73-
})
74-
.catch((error) => {
75-
console.error("[RooHandler] Failed to reload models after auth:", error)
76-
})
77-
} else if (state.state === "logged-out") {
78-
this.client = new OpenAI({
79-
baseURL: this.baseURL,
80-
apiKey: "unauthenticated",
81-
defaultHeaders: DEFAULT_HEADERS,
82-
})
83-
84-
// Flush cache when logged out
85-
flushModels("roo").catch((error) => {
86-
console.error("[RooHandler] Failed to flush models on logout:", error)
87-
})
88-
}
61+
// Update OpenAI client with current auth token
62+
// Note: Model cache flush/reload is handled by extension.ts authStateChangedHandler
63+
const newToken = cloudService.authService?.getSessionToken()
64+
this.client = new OpenAI({
65+
baseURL: this.baseURL,
66+
apiKey: newToken ?? "unauthenticated",
67+
defaultHeaders: DEFAULT_HEADERS,
68+
})
8969
}
9070

9171
cloudService.on("auth-state-changed", this.authStateListener)

src/extension.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
CodeActionProvider,
4141
} from "./activate"
4242
import { initializeI18n } from "./i18n"
43+
import { flushModels, getModels } from "./api/providers/fetchers/modelCache"
4344

4445
/**
4546
* Built using https://github.com/microsoft/vscode-webview-ui-toolkit
@@ -140,6 +141,34 @@ export async function activate(context: vscode.ExtensionContext) {
140141
)
141142
}
142143
}
144+
145+
// Handle Roo models cache based on auth state
146+
const handleRooModelsCache = async () => {
147+
try {
148+
await flushModels("roo")
149+
150+
if (data.state === "active-session") {
151+
// Reload models with the new auth token
152+
const sessionToken = cloudService?.authService?.getSessionToken()
153+
await getModels({
154+
provider: "roo",
155+
baseUrl: process.env.ROO_CODE_PROVIDER_URL ?? "https://api.roocode.com/proxy",
156+
apiKey: sessionToken,
157+
})
158+
cloudLogger(`[authStateChangedHandler] Reloaded Roo models cache for active session`)
159+
} else {
160+
cloudLogger(`[authStateChangedHandler] Flushed Roo models cache on logout`)
161+
}
162+
} catch (error) {
163+
cloudLogger(
164+
`[authStateChangedHandler] Failed to handle Roo models cache: ${error instanceof Error ? error.message : String(error)}`,
165+
)
166+
}
167+
}
168+
169+
if (data.state === "active-session" || data.state === "logged-out") {
170+
await handleRooModelsCache()
171+
}
143172
}
144173

145174
settingsUpdatedHandler = async () => {

0 commit comments

Comments
 (0)