Skip to content

Commit 46ab113

Browse files
authored
Merge pull request #2081 from Kilo-Org/beatlevic/fetch-openrouter-models-for-org
Add support for retrieving organization filtered models
2 parents 14f60ef + 35eeb54 commit 46ab113

File tree

8 files changed

+48
-31
lines changed

8 files changed

+48
-31
lines changed

src/api/providers/fetchers/modelCache.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ export const getModels = async (options: GetModelsOptions): Promise<ModelRecord>
8181
// kilocode_change start
8282
case "kilocode-openrouter":
8383
models = await getOpenRouterModels({
84-
openRouterBaseUrl: getKiloBaseUriFromToken(options.kilocodeToken ?? "") + "/api/openrouter",
85-
headers: options.kilocodeOrganizationId
86-
? { "X-KiloCode-OrganizationId": options.kilocodeOrganizationId }
87-
: undefined,
84+
openRouterBaseUrl:
85+
getKiloBaseUriFromToken(options.kilocodeToken ?? "") +
86+
(options.kilocodeOrganizationId
87+
? `/api/organizations/${options.kilocodeOrganizationId}`
88+
: "/api/openrouter"),
89+
headers: options.kilocodeToken ? { Authorization: `Bearer ${options.kilocodeToken}` } : undefined,
8890
})
8991
break
9092
case "cerebras":

src/core/webview/webviewMessageHandler.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,18 @@ export const webviewMessageHandler = async (
16621662
if (currentConfig.kilocodeToken !== message.apiConfiguration.kilocodeToken) {
16631663
configToSave = { ...message.apiConfiguration, kilocodeOrganizationId: undefined }
16641664
}
1665+
if (currentConfig.kilocodeOrganizationId !== message.apiConfiguration.kilocodeOrganizationId) {
1666+
await flushModels("kilocode-openrouter")
1667+
const models = await getModels({
1668+
provider: "kilocode-openrouter",
1669+
kilocodeOrganizationId: message.apiConfiguration.kilocodeOrganizationId,
1670+
kilocodeToken: message.apiConfiguration.kilocodeToken,
1671+
})
1672+
provider.postMessageToWebview({
1673+
type: "routerModels",
1674+
routerModels: { "kilocode-openrouter": models } as Record<RouterName, ModelRecord>,
1675+
})
1676+
}
16651677
} catch (error) {
16661678
// Config might not exist yet, that's fine
16671679
}

webview-ui/src/components/kilocode/common/OrganizationSelector.tsx

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,27 @@ export const OrganizationSelector = ({ className, showLabel = false }: { classNa
1212
const selectedOrg = organizations.find((o) => o.id === apiConfiguration?.kilocodeOrganizationId)
1313
const containerRef = useRef<HTMLDivElement>(null)
1414

15+
const handleMessage = (event: MessageEvent<WebviewMessage>) => {
16+
const message = event.data
17+
if (message.type === "profileDataResponse") {
18+
const payload = message.payload as ProfileDataResponsePayload
19+
if (payload.success) {
20+
setOrganizations(payload.data?.organizations ?? [])
21+
} else {
22+
console.error("Error fetching profile organizations data:", payload.error)
23+
setOrganizations([])
24+
}
25+
} else if (message.type === "updateProfileData") {
26+
vscode.postMessage({
27+
type: "fetchProfileDataRequest",
28+
})
29+
}
30+
}
31+
1532
useEffect(() => {
1633
const onKeyDown = (e: KeyboardEvent) => {
1734
if (e.key === "Escape") setIsOpen(false)
1835
}
19-
window.addEventListener("keydown", onKeyDown)
2036

2137
const onMouseDown = (e: MouseEvent) => {
2238
if (!containerRef.current) return
@@ -25,11 +41,14 @@ export const OrganizationSelector = ({ className, showLabel = false }: { classNa
2541
}
2642
}
2743

44+
window.addEventListener("keydown", onKeyDown)
2845
window.addEventListener("mousedown", onMouseDown)
46+
window.addEventListener("message", handleMessage)
2947

3048
return () => {
3149
window.removeEventListener("keydown", onKeyDown)
3250
window.removeEventListener("mousedown", onMouseDown)
51+
window.removeEventListener("message", handleMessage)
3352
}
3453
}, [])
3554

@@ -41,29 +60,6 @@ export const OrganizationSelector = ({ className, showLabel = false }: { classNa
4160
})
4261
}, [apiConfiguration?.kilocodeToken])
4362

44-
useEffect(() => {
45-
const handleMessage = (event: MessageEvent<WebviewMessage>) => {
46-
const message = event.data
47-
if (message.type === "profileDataResponse") {
48-
const payload = message.payload as ProfileDataResponsePayload
49-
if (payload.success) {
50-
setOrganizations(payload.data?.organizations ?? [])
51-
} else {
52-
console.error("Error fetching profile organizations data:", payload.error)
53-
setOrganizations([])
54-
}
55-
} else if (message.type === "updateProfileData") {
56-
vscode.postMessage({
57-
type: "fetchProfileDataRequest",
58-
})
59-
}
60-
}
61-
62-
window.addEventListener("message", handleMessage)
63-
64-
return () => window.removeEventListener("message", handleMessage)
65-
}, [apiConfiguration?.kilocodeToken])
66-
6763
const setSelectedOrganization = (organization: UserOrganizationWithApiKey | null) => {
6864
if (organization === null) {
6965
// Switch back to personal account - clear organization token

webview-ui/src/components/kilocode/hooks/useProviderModels.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export const useProviderModels = (apiConfiguration?: ProviderSettings) => {
254254
const routerModels = useRouterModels({
255255
openRouterBaseUrl: apiConfiguration?.openRouterBaseUrl,
256256
openRouterApiKey: apiConfiguration?.apiKey,
257+
kilocodeOrganizationId: apiConfiguration?.kilocodeOrganizationId ?? "personal",
257258
})
258259

259260
const { models, defaultModel } =

webview-ui/src/components/settings/ApiOptions.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ const ApiOptions = ({
203203
const { data: routerModels, refetch: refetchRouterModels } = useRouterModels({
204204
openRouterBaseUrl: apiConfiguration?.openRouterBaseUrl,
205205
openRouterApiKey: apiConfiguration?.openRouterApiKey,
206+
kilocodeOrganizationId: apiConfiguration?.kilocodeOrganizationId ?? "personal",
206207
})
207208

208209
//const { data: openRouterModelProviders } = useOpenRouterModelProviders(

webview-ui/src/components/ui/hooks/useOpenRouterModelProviders.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,16 @@ type UseOpenRouterModelProvidersOptions = Omit<
137137
"queryKey" | "queryFn"
138138
>
139139

140-
// kilocode_change start: baseUrl, apiKey
140+
// kilocode_change start: baseUrl, apiKey, organizationId
141141
export const useOpenRouterModelProviders = (
142142
modelId?: string,
143143
baseUrl?: string,
144144
apiKey?: string,
145+
organizationId?: string,
145146
options?: UseOpenRouterModelProvidersOptions,
146147
) =>
147148
useQuery<Record<string, OpenRouterModelProvider>>({
148-
queryKey: ["openrouter-model-providers", modelId, baseUrl, apiKey],
149+
queryKey: ["openrouter-model-providers", modelId, baseUrl, apiKey, organizationId],
149150
queryFn: () => (modelId ? getOpenRouterProvidersForModel(modelId, baseUrl, apiKey) : {}),
150151
...options,
151152
})

webview-ui/src/components/ui/hooks/useRouterModels.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type RouterModelsQueryKey = {
4141
openRouterApiKey?: string
4242
lmStudioBaseUrl?: string
4343
ollamaBaseUrl?: string
44+
kilocodeOrganizationId?: string
4445
// Requesty, Unbound, etc should perhaps also be here, but they already have their own hacks for reloading
4546
}
4647

webview-ui/src/components/ui/hooks/useSelectedModel.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export const useModelProviders = (kilocodeDefaultModel: string, apiConfiguration
7676
? (apiConfiguration?.openRouterModelId ?? openRouterDefaultModelId)
7777
: undefined,
7878
provider === "openrouter" ? apiConfiguration?.openRouterBaseUrl : undefined,
79+
apiConfiguration?.apiKey,
80+
apiConfiguration?.kilocodeOrganizationId ?? "personal",
7981
)
8082
}
8183
// kilocode_change end
@@ -86,7 +88,8 @@ export const useSelectedModel = (apiConfiguration?: ProviderSettings) => {
8688
const { kilocodeDefaultModel } = useExtensionState()
8789
const routerModels = useRouterModels({
8890
openRouterBaseUrl: apiConfiguration?.openRouterBaseUrl,
89-
openRouterApiKey: apiConfiguration?.apiKey,
91+
openRouterApiKey: apiConfiguration?.apiKey, // kilocode_change
92+
kilocodeOrganizationId: apiConfiguration?.kilocodeOrganizationId, // kilocode_change
9093
})
9194
const openRouterModelProviders = useModelProviders(kilocodeDefaultModel, apiConfiguration)
9295
// kilocode_change end

0 commit comments

Comments
 (0)