-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fetch model and provider list from specified OpenRouter base URL #4931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,20 +16,6 @@ import { getLiteLLMModels } from "./litellm" | |
| import { GetModelsOptions } from "../../../shared/api" | ||
| const memoryCache = new NodeCache({ stdTTL: 5 * 60, checkperiod: 5 * 60 }) | ||
|
|
||
| async function writeModels(router: RouterName, data: ModelRecord) { | ||
| const filename = `${router}_models.json` | ||
| const cacheDir = await getCacheDirectoryPath(ContextProxy.instance.globalStorageUri.fsPath) | ||
| await fs.writeFile(path.join(cacheDir, filename), JSON.stringify(data)) | ||
| } | ||
|
|
||
| async function readModels(router: RouterName): Promise<ModelRecord | undefined> { | ||
| const filename = `${router}_models.json` | ||
| const cacheDir = await getCacheDirectoryPath(ContextProxy.instance.globalStorageUri.fsPath) | ||
| const filePath = path.join(cacheDir, filename) | ||
| const exists = await fileExistsAtPath(filePath) | ||
| return exists ? JSON.parse(await fs.readFile(filePath, "utf8")) : undefined | ||
| } | ||
|
|
||
| /** | ||
| * Get models from the cache or fetch them from the provider and cache them. | ||
| * There are two caches: | ||
|
|
@@ -43,15 +29,18 @@ async function readModels(router: RouterName): Promise<ModelRecord | undefined> | |
| */ | ||
| export const getModels = async (options: GetModelsOptions): Promise<ModelRecord> => { | ||
| const { provider } = options | ||
| let models = memoryCache.get<ModelRecord>(provider) | ||
|
|
||
| const cacheKey = JSON.stringify(options) | ||
| let models = memoryCache.get<ModelRecord>(cacheKey) | ||
|
|
||
| if (models) { | ||
| return models | ||
| } | ||
|
|
||
| try { | ||
| switch (provider) { | ||
| case "openrouter": | ||
| models = await getOpenRouterModels() | ||
| models = await getOpenRouterModels(options.baseUrl, options.apiKey) | ||
| break | ||
| case "requesty": | ||
| // Requesty models endpoint requires an API key for per-user custom policies | ||
|
|
@@ -76,17 +65,8 @@ export const getModels = async (options: GetModelsOptions): Promise<ModelRecord> | |
| } | ||
|
|
||
| // Cache the fetched models (even if empty, to signify a successful fetch with no models) | ||
| memoryCache.set(provider, models) | ||
| await writeModels(provider, models).catch((err) => | ||
| console.error(`[getModels] Error writing ${provider} models to file cache:`, err), | ||
| ) | ||
| memoryCache.set(cacheKey, models) | ||
|
|
||
| try { | ||
| models = await readModels(provider) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cache file seems to be ineffective, because it is only read after we just wrote it, so I decided to remove it. |
||
| // console.log(`[getModels] read ${router} models from file cache`) | ||
| } catch (error) { | ||
| console.error(`[getModels] error reading ${provider} models from file cache`, error) | ||
| } | ||
| return models || {} | ||
| } catch (error) { | ||
| // Log the error and re-throw it so the caller can handle it (e.g., show a UI message). | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optionsis a small object, see the type definition ofGetModelsOptions