Skip to content

Commit b3054a6

Browse files
committed
Reload model list on auth changes
1 parent b2c02de commit b3054a6

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/api/providers/fetchers/modelCache.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ export const getModels = async (options: GetModelsOptions): Promise<ModelRecord>
100100
case "huggingface":
101101
models = await getHuggingFaceModels()
102102
break
103-
case "roo":
103+
case "roo": {
104104
// Roo Code Cloud provider requires baseUrl and optional apiKey
105-
models = await getRooModels(
106-
options.baseUrl ?? process.env.ROO_CODE_PROVIDER_URL ?? "https://api.roocode.com/proxy",
107-
options.apiKey,
108-
)
105+
const rooBaseUrl =
106+
options.baseUrl ?? process.env.ROO_CODE_PROVIDER_URL ?? "https://api.roocode.com/proxy"
107+
models = await getRooModels(rooBaseUrl, options.apiKey)
109108
break
109+
}
110110
default: {
111111
// Ensures router is exhaustively checked if RouterName is a strict union.
112112
const exhaustiveCheck: never = provider

src/api/providers/roo.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import { ApiStream } from "../transform/stream"
1010
import type { ApiHandlerCreateMessageMetadata } from "../index"
1111
import { DEFAULT_HEADERS } from "./constants"
1212
import { BaseOpenAiCompatibleProvider } from "./base-openai-compatible-provider"
13-
import { getModels } from "../providers/fetchers/modelCache"
13+
import { getModels, flushModels } from "../providers/fetchers/modelCache"
1414

1515
export class RooHandler extends BaseOpenAiCompatibleProvider<string> {
1616
private authStateListener?: (state: { state: AuthState }) => void
1717
private mergedModels: Record<string, ModelInfo> = {}
1818
private modelsLoaded = false
19+
private fetcherBaseURL: string
1920

2021
constructor(options: ApiHandlerOptions) {
2122
let sessionToken: string | undefined = undefined
@@ -44,8 +45,8 @@ export class RooHandler extends BaseOpenAiCompatibleProvider<string> {
4445
})
4546

4647
// Load dynamic models asynchronously - strip /v1 from baseURL for fetcher
47-
const fetcherBaseURL = baseURL.endsWith("/v1") ? baseURL.slice(0, -3) : baseURL
48-
this.loadDynamicModels(fetcherBaseURL, sessionToken).catch((error) => {
48+
this.fetcherBaseURL = baseURL.endsWith("/v1") ? baseURL.slice(0, -3) : baseURL
49+
this.loadDynamicModels(this.fetcherBaseURL, sessionToken).catch((error) => {
4950
console.error("[RooHandler] Failed to load dynamic models:", error)
5051
})
5152

@@ -54,17 +55,31 @@ export class RooHandler extends BaseOpenAiCompatibleProvider<string> {
5455

5556
this.authStateListener = (state: { state: AuthState }) => {
5657
if (state.state === "active-session") {
58+
const newToken = cloudService.authService?.getSessionToken()
5759
this.client = new OpenAI({
5860
baseURL: this.baseURL,
59-
apiKey: cloudService.authService?.getSessionToken() ?? "unauthenticated",
61+
apiKey: newToken ?? "unauthenticated",
6062
defaultHeaders: DEFAULT_HEADERS,
6163
})
64+
65+
// Flush cache and reload models with the new auth token
66+
flushModels("roo")
67+
.then(() => {
68+
return this.loadDynamicModels(this.fetcherBaseURL, newToken)
69+
})
70+
.catch((error) => {
71+
console.error("[RooHandler] Failed to reload models after auth:", error)
72+
})
6273
} else if (state.state === "logged-out") {
6374
this.client = new OpenAI({
6475
baseURL: this.baseURL,
6576
apiKey: "unauthenticated",
6677
defaultHeaders: DEFAULT_HEADERS,
6778
})
79+
80+
// Clear models cache when logged out
81+
this.mergedModels = {}
82+
this.modelsLoaded = false
6883
}
6984
}
7085

0 commit comments

Comments
 (0)