Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
1acf5da
Fix openrouter custome base url Merge pull request [#2181] https://gi…
Aug 28, 2025
64b7e23
Merge branch 'main' into fix-openrouter-custom-base-url
cobra91 Aug 28, 2025
4812986
Add kilocode_change comments
chrarnoldus Aug 29, 2025
9a13ac7
Merge branch 'main' into fix-openrouter-custom-base-url
chrarnoldus Aug 29, 2025
84b4cd2
Merge branch 'Kilo-Org:main' into fix-openrouter-custom-base-url
cobra91 Aug 31, 2025
7077ad6
Merge branch 'main' into fix-openrouter-custom-base-url
cobra91 Aug 31, 2025
44462a3
fix for recover from the good url the model list
cobra91 Sep 1, 2025
fbbe0ec
Merge branch 'Kilo-Org:main' into fix-openrouter-custom-base-url
cobra91 Sep 1, 2025
653b887
Merge branch 'fix-openrouter-custom-base-url' of https://github.com/c…
cobra91 Sep 1, 2025
b87b0d5
final commit test all work
cobra91 Sep 1, 2025
c1a1b00
Merge branch 'main' into fix-openrouter-custom-base-url
cobra91 Sep 1, 2025
cf9b796
Merge branch 'Kilo-Org:main' into fix-openrouter-custom-base-url
cobra91 Sep 3, 2025
77091e8
Merge branch 'main' into fix-openrouter-custom-base-url
kevinvandijk Sep 3, 2025
64fb474
Merge branch 'main' into fix-openrouter-custom-base-url
cobra91 Sep 5, 2025
f4c5be5
Merge branch 'Kilo-Org:main' into fix-openrouter-custom-base-url
cobra91 Sep 9, 2025
8d6390e
Merge branch 'main' into fix-openrouter-custom-base-url
cobra91 Sep 10, 2025
86b5575
Merge branch 'Kilo-Org:main' into fix-openrouter-custom-base-url
cobra91 Sep 11, 2025
1bb7620
Merge branch 'main' into fix-openrouter-custom-base-url
cobra91 Sep 15, 2025
cd85365
Merge branch 'main' into fix-openrouter-custom-base-url
cobra91 Sep 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/api/providers/fetchers/modelEndpointCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ export const getModelEndpoints = async ({
router,
modelId,
endpoint,
// kilocode_change start
baseUrl,
apiKey,
// kilocode_change end
}: {
router: RouterName
modelId?: string
endpoint?: string
// kilocode_change start
baseUrl?: string
apiKey?: string
// kilocode_change end
}): Promise<ModelRecord> => {
// OpenRouter is the only provider that supports model endpoints, but you
// can see how we'd extend this to other providers in the future.
Expand All @@ -53,7 +61,15 @@ export const getModelEndpoints = async ({
return modelProviders
}

modelProviders = await getOpenRouterModelEndpoints(modelId)
modelProviders = await getOpenRouterModelEndpoints(
modelId,
// kilocode_change start
{
openRouterBaseUrl: baseUrl,
openRouterApiKey: apiKey,
},
// kilocode_change end
)

if (Object.keys(modelProviders).length > 0) {
// console.log(`[getModelProviders] API fetch for ${key} -> ${Object.keys(modelProviders).length}`)
Expand Down
3 changes: 2 additions & 1 deletion src/api/providers/fetchers/openrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ export async function getOpenRouterModels(
const baseURL = options?.openRouterBaseUrl || "https://openrouter.ai/api/v1"

try {
const response = await axios.get<OpenRouterModelsResponse>(`${baseURL}/models`, {
const fullUrl = `${baseURL}/models`
const response = await axios.get<OpenRouterModelsResponse>(fullUrl, {
headers: { ...DEFAULT_HEADERS, ...(options?.headers ?? {}) }, // kilocode_change: added headers
})
const result = openRouterModelsResponseSchema.safeParse(response.data)
Expand Down
9 changes: 8 additions & 1 deletion src/api/providers/openrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,18 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH

public async fetchModel() {
const [models, endpoints] = await Promise.all([
getModels({ provider: "openrouter" }),
getModels({
provider: "openrouter",
baseUrl: this.options.openRouterBaseUrl, // kilocode_change
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the api key also be specified here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not for openrouter who don't need apikey to fetch model list => https://openrouter.ai/api/v1/models work on browser

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but the same is true for getModelEndpoints isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}),
getModelEndpoints({
router: "openrouter",
modelId: this.options.openRouterModelId,
endpoint: this.options.openRouterSpecificProvider,
// kilocode_change start
baseUrl: this.options.openRouterBaseUrl,
apiKey: this.options.openRouterApiKey,
// kilocode_change end
}),
])

Expand Down
13 changes: 11 additions & 2 deletions src/core/webview/webviewMessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,17 @@ export const webviewMessageHandler = async (
}

// kilocode_change start: openrouter auth, kilocode provider
const openRouterApiKey = apiConfiguration.openRouterApiKey || message?.values?.openRouterApiKey
const openRouterBaseUrl = apiConfiguration.openRouterBaseUrl || message?.values?.openRouterBaseUrl
// Prioritize the new value from the UI input over the saved configuration for live testing
const openRouterApiKey =
message?.values?.openRouterApiKey !== undefined
? message.values.openRouterApiKey
: apiConfiguration.openRouterApiKey
const openRouterBaseUrl =
message?.values?.openRouterBaseUrl !== undefined
? message.values.openRouterBaseUrl
: apiConfiguration.openRouterBaseUrl

await flushModels("openrouter") // force flush models cache when baseUrl changes

const modelFetchPromises: Array<{ key: RouterName; options: GetModelsOptions }> = [
{
Expand Down
29 changes: 29 additions & 0 deletions webview-ui/src/components/settings/ApiOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,21 @@ const ApiOptions = ({
vscode.postMessage({ type: "requestLmStudioModels" })
} else if (selectedProvider === "vscode-lm") {
vscode.postMessage({ type: "requestVsCodeLmModels" })
// kilocode_change start
} else if (selectedProvider === "openrouter") {
const headerObject = convertHeadersToObject(customHeaders)
vscode.postMessage({
type: "requestRouterModels",
values: {
openRouterBaseUrl: apiConfiguration?.openRouterBaseUrl,
openRouterApiKey: apiConfiguration?.openRouterApiKey,
customHeaders: {},
openAiHeaders: headerObject,
source: "ApiOptions.debounce.openrouter",
},
})
refetchRouterModels()
// kilocode_change end
} else if (selectedProvider === "litellm") {
vscode.postMessage({ type: "requestRouterModels" })
} else if (selectedProvider === "deepinfra") {
Expand All @@ -278,6 +293,7 @@ const ApiOptions = ({
apiConfiguration?.deepInfraApiKey,
apiConfiguration?.deepInfraBaseUrl,
customHeaders,
refetchRouterModels, // kilocode_change
],
)

Expand All @@ -290,6 +306,19 @@ const ApiOptions = ({
setErrorMessage(apiValidationResult)
}, [apiConfiguration, routerModels, organizationAllowList, setErrorMessage])

// kilocode_change start
// This will trigger whenever the baseUrl for OpenRouter changes.
useEffect(() => {
// We only want to do this if the selected provider is actually OpenRouter.
if (apiConfiguration.apiProvider === "openrouter") {
// 1. Flush the server-side cache for openrouter.
vscode.postMessage({ type: "flushRouterModels", text: "openrouter" })
// 2. Trigger a client-side refetch using the new configuration.
refetchRouterModels()
}
}, [apiConfiguration.openRouterBaseUrl, apiConfiguration.apiProvider, refetchRouterModels])
// kilocode_change end

const selectedProviderModels = useMemo(() => {
const models = MODELS_BY_PROVIDER[selectedProvider]
if (!models) return []
Expand Down
8 changes: 6 additions & 2 deletions webview-ui/src/components/ui/hooks/useSelectedModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ export const useModelProviders = (kilocodeDefaultModel: string, apiConfiguration
: provider === "openrouter"
? (apiConfiguration?.openRouterModelId ?? openRouterDefaultModelId)
: undefined,
provider === "openrouter" ? apiConfiguration?.openRouterBaseUrl : undefined,
apiConfiguration?.apiKey,
provider === "openrouter" || provider === "kilocode" ? apiConfiguration?.openRouterBaseUrl : undefined,
provider === "openrouter"
? apiConfiguration?.apiKey
: provider === "kilocode"
? apiConfiguration?.kilocodeToken
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this change made? I don't think it is correct, because the Kilo Code provider doesn't have this endpoint (yet at least).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes want to reuse max code but i guess it's not good because kilo code provider don't need custom ! sorry

: undefined,
apiConfiguration?.kilocodeOrganizationId ?? "personal",
)
}
Expand Down
Loading