feat: add OpenAI-compatible /v1/models discovery fallback - #53
Open
hossihub wants to merge 2 commits into
Open
Conversation
When LM Studio's native GET /api/v1/models endpoint is unreachable or
returns an unsupported response shape, fall back to the OpenAI-compatible
GET /v1/models endpoint so models are still discovered.
- Add OpenAICompatibleModelsResponseSchema and related types in src/types
- Add discoverOpenAIModels() utility that parses { data: [{id}] } responses
- Refactor enhance-config.ts with discoverCatalog() wrapper that tries
native first, then falls back to /v1/models on failure
- Remove early return when no provider exists + auto-detect fails so the
fallback path can still execute
- Pass pre-fetched response from autoDetectLMStudio() through to avoid
redundant fetch calls when native succeeds during detection
- Fix test assertion: "/api/v1/models".endsWith("/v1/models") is true,
add negation for /api/v1/models suffix in fallback tests
- Update docs/v1-contract.md and README.md to document the new fallback
- Add OpenAIModelInfo interface with contextLength and maxTokens - discoverOpenAIModels returns full model records instead of just IDs - toModelConfigFromID uses the record's context length instead of a fixed default - Add max_tokens and context_length to OpenAICompatibleModelSchema - Relax LMStudioCapabilitiesSchema reasoning fields to z.string for proxy compatibility
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When LM Studio's native
GET /api/v1/modelsendpoint is unreachable or returns an unsupported response shape, fall back to the OpenAI-compatibleGET /v1/modelsendpoint so models are still discovered.This enables opencode to display available models when connecting to servers that expose an OpenAI-compatible models list but not LM Studio's native schema (e.g., OpenRouter, LiteLLM proxies).
Changes
Core logic
src/plugin/enhance-config.ts: AddedtoModelConfigFromID()for mapping OpenAI/v1/modelsrecord IDs to ModelConfig with conservative defaults (32k context, 8k output). AddeddiscoverCatalog()wrapper that tries native first, then falls back to/v1/modelson failure. Removed early return when no provider exists + auto-detect fails so the fallback path can still execute. Passes pre-fetched response fromautoDetectLMStudio()through to avoid redundant fetch calls.Types & utilities
src/types/index.ts: AddedOpenAICompatibleModelsResponseSchema,OpenAICompatibleModelRecord, andOpenAICompatibleModelsResponsezod schemas for validating{ data: [{id, object}], object: "list" }responses.src/utils/lmstudio-api.ts: AddedOPENAI_MODELS_PATH,toOpenAIModelsURL(), anddiscoverOpenAIModels()utility function.Tests
test/plugin.test.ts: Added 4 new tests for OpenAI-compatible fallback behavior (fallback when native unavailable, auth passthrough, native-only path, both-endpoints-fail). Fixed assertion bug where/api/v1/models.endsWith(/v1/models) wastrue— added negation for the native endpoint suffix.Documentation
docs/v1-contract.md: Documented new fallback endpoint and simplified model mapping policy.README.md: Updated behavior list and requirements to reflect fallback support.Test Results