Skip to content

feat: add OpenAI-compatible /v1/models discovery fallback - #53

Open
hossihub wants to merge 2 commits into
agustif:mainfrom
hossihub:feat/openai-compatible-model-fallback
Open

feat: add OpenAI-compatible /v1/models discovery fallback#53
hossihub wants to merge 2 commits into
agustif:mainfrom
hossihub:feat/openai-compatible-model-fallback

Conversation

@hossihub

Copy link
Copy Markdown

Summary

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.

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: Added toModelConfigFromID() for mapping OpenAI /v1/models record IDs to ModelConfig with conservative defaults (32k context, 8k output). Added discoverCatalog() wrapper that tries native first, then falls back to /v1/models on failure. Removed early return when no provider exists + auto-detect fails so the fallback path can still execute. Passes pre-fetched response from autoDetectLMStudio() through to avoid redundant fetch calls.

Types & utilities

  • src/types/index.ts: Added OpenAICompatibleModelsResponseSchema, OpenAICompatibleModelRecord, and OpenAICompatibleModelsResponse zod schemas for validating { data: [{id, object}], object: "list" } responses.
  • src/utils/lmstudio-api.ts: Added OPENAI_MODELS_PATH, toOpenAIModelsURL(), and discoverOpenAIModels() 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) was true — 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

Test Files  2 passed (2)
Tests       31 passed (31)
Lint        clean
Typecheck   clean
Build       clean

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant