GeezerVison is showing github.com — feat: added ability to load dynamically the model list from LM Studio by ricardofiorani · Pull Request #24612 · anomalyco/opencode
feat: added ability to load dynamically the model list from LM Studio
#24612
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.ts
(2)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
feat: added ability to load dynamically the model list from LM Studio
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit 36157b68d41a7342aa4aab4b13c09523c9a97374
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,86 @@
import { Log } from "../util"
const log = Log.create({ service: "model-cache" })
export namespace ModelCache {
const cache: Record<string, Record<string, any>> = {}
export async function fetch(providerID: string, options: { baseURL?: string; apiKey?: string } = {}): Promise<Record<string, any>> {
if (cache[providerID]) return cache[providerID]
if (providerID === "apertis") {
return fetchApertisModels(options)
}
if (providerID === "lmstudio") {
return fetchOpenAICompatibleModels(options)
}
return {}
}
export async function refresh(providerID: string, options: { baseURL?: string; apiKey?: string } = {}): Promise<Record<string, any>> {
const result = await fetch(providerID, options)
if (Object.keys(result).length > 0) {
cache[providerID] = result
}
return result
}
const APERTIS_BASE_URL = "https://api.apertis.ai/v1"
async function fetchOpenAICompatibleModels(options: { baseURL?: string; apiKey?: string }): Promise<Record<string, any>> {
const baseURL = options.baseURL ?? "http://127.0.0.1:1234/v1"
const apiKey = options.apiKey
const url = `${baseURL.replace(/\/+$/, "")}/models`
try {
const response = await Bun.fetch(url, {
method: "GET",
headers: apiKey ? { Authorization: `Bearer ${apiKey}` } : {},
signal: AbortSignal.timeout(10_000),
})
if (!response.ok) {
log.error("openai-compatible model fetch failed", { url, status: response.status })
return {}
}
const json = await response.json() as { data?: Array<{ id: string; owned_by?: string }> }
const models: Record<string, any> = {}
for (const model of json.data ?? []) {
models[model.id] = {
id: model.id,
name: model.id,
family: model.owned_by ?? "",
release_date: "",
attachment: true,
reasoning: false,
temperature: true,
tool_call: true,
cost: { input: 0, output: 0 },
limit: { context: 128000, output: 4096 },
options: {},
modalities: {
input: ["text", "image"],
output: ["text"],
},
}
}
return models
} catch (error) {
log.error("openai-compatible model fetch error", { url, error })
return {}
}
}
async function fetchApertisModels(options: { baseURL?: string; apiKey?: string }): Promise<Record<string, any>> {
return fetchOpenAICompatibleModels({
baseURL: options.baseURL ?? APERTIS_BASE_URL,
apiKey: options.apiKey,
})
}
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -8,6 +8,7 @@ import { lazy } from "@/util/lazy"
import { Filesystem } from "../util"
import { Flock } from "@opencode-ai/core/util/flock"
import { Hash } from "@opencode-ai/core/util/hash"
import { ModelCache } from "./model-cache"
// Try to import bundled snapshot (generated at build time)
// Falls back to undefined in dev mode when snapshot doesn't exist
Expand Down
Expand Up
@@ -145,7 +146,55 @@ export const Data = lazy(async () => {
export async function get() {
const result = await Data()
return result as Record<string, Provider>
const providers = result as Record<string, Provider>
const config = await Filesystem.readJson(path.join(Global.Path.config, "config.json")).catch(() => undefined) as {
disabled_providers?: string[]
enabled_providers?: string[]
provider?: Record<string, { options?: Record<string, unknown> }>
} | undefined
const disabled = new Set(config?.disabled_providers ?? [])
const enabled = config?.enabled_providers ? new Set(config.enabled_providers) : null
const lmstudioDisabled = disabled.has("lmstudio")
const lmstudioEnabled = enabled ? enabled.has("lmstudio") : true
const lmstudioAllowed = lmstudioEnabled && !lmstudioDisabled
if (lmstudioAllowed) {
const lmstudioConfig = config?.provider?.lmstudio?.options
const lmstudioBaseURL = (lmstudioConfig?.baseURL as string) ?? "http://127.0.0.1:1234/v1"
const lmstudioFetchOptions = {
...(lmstudioConfig?.baseURL ? { baseURL: lmstudioConfig.baseURL as string } : {}),
...(lmstudioConfig?.apiKey ? { apiKey: lmstudioConfig.apiKey as string } : {}),
}
try {
const lmstudioModels = await ModelCache.fetch("lmstudio", lmstudioFetchOptions).catch(() => ({}))
if (Object.keys(lmstudioModels).length > 0) {
if (!providers["lmstudio"]) {
providers["lmstudio"] = {
id: "lmstudio",
name: "LMStudio",
env: ["LMSTUDIO_API_KEY"],
api: lmstudioBaseURL,
npm: "@ai-sdk/openai-compatible",
models: lmstudioModels,
}
} else {
const existing = providers["lmstudio"]
providers["lmstudio"] = {
...existing,
models: lmstudioModels,
api: lmstudioConfig?.baseURL ? lmstudioBaseURL : existing.api,
}
}
}
} catch (err) {
log.debug("lmstudio model fetch failed, using snapshot fallback", { error: err })
}
}
return providers
}
export async function refresh(force = false) {
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.