ISSUE DESCRIPTION /v1/models: return the full catalog with per-model availability +…
- User
ISSUE DESCRIPTION
/v1/models: return the full catalog with per-model availability + opt-in filter Split out from #242.
Current: `GET /v1/models` returns only models that are connected right now (enabled AND an enabled key can serve them).
Wanted: by default return the whole catalog (one entry per model id), each annotated with whether it's usable, plus an opt-in filter to get just the connected ones.
Per-model annotation:
- `available`: boolean — enabled AND a matching enabled key exists.
- `unavailable_reason`: `null` | `"no_key"` (enabled, no key) | `"disabled"` (catalog row disabled).
Filter:
- `?available=true` (alias `?connected=true`) → only connected models (the previous default behavior).
Notes:
- The extra fields are additive — OpenAI-compatible clients ignore unknown fields.
- Dedup prefers an available instance of a model id over a disabled/keyless one.
- The `auto` virtual model is always `available: true`.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
Assistant
Let me start by exploring the repository structure to understand the codebase and find the relevant files for the
/v1/modelsendpoint.Assistant
It looks like this is a Node.js project. Let me explore the server directory structure.
Assistant
I need to find the
/v1/modelsendpoint handler. Let me search for it.Assistant
Let me look at the routes to find where
/v1/modelsis defined.Assistant
Let me examine the models route file and the main server entry point.
Assistant
Let me look at the
hasProviderfunction and understand the provider and key structures:Assistant
Now let me look at the database schema and understand the models and keys tables:
Assistant
Let me look at the migrations file to understand the database schema:
<tool_call> <function=bash_terminal> <parameter=command> grep -E "CREATE TABLE|ALTER TABLE" /freellmapi/server/src/db/migrations.ts </tool_call>
Assistant
The user is working on a repository and I've started exploring it. I need to continue exploring the database schema by looking at the migrations file. Let me run the grep command again to understand the table structures.
Assistant
Let me continue exploring to understand the models table structure and look for any "auto" virtual model:
Assistant
Good, now I understand the database schema. The
modelstable has:id,platform,model_id,display_name, etc.enabled(1 or 0) - whether the model is enabledkey_id(added later) - but this might be the old schema
The
api_keystable has:platform- which platform the key is forenabled- whether the key is enabled
Now I need to understand:
- What "available" means: a model is available if it's enabled AND there's an enabled key for its platform
- The current behavior returns only connected models
- I need to add
availableandunavailable_reasonfields
Let me also look for the "auto" virtual model mentioned in the issue.
Assistant
The "auto" virtual model is mentioned in the issue:
- The
autovirtual model is alwaysavailable: true.
This is an important note. I need to understand if there's an "auto" model that needs special handling.
Let me also look at the shared types to understand the data types used.
- The
Assistant
Let me continue exploring the codebase to understand the types and look for any existing tests related to models.
Assistant
Now let me understand how the router is connected and look for the "auto" virtual model:
Assistant
Let me read the test file for the auto model to understand the expected behavior:
Assistant
I see the test expects a different response format (
body.object,body.data[]) than what's in the models route file. Let me check the app setup to understand the full picture:Assistant
I see there are two routes:
/api/models(admin API) and/v1/models(OpenAI-compatible). The test expects the/v1/modelsformat. Let me find where/v1/modelsis defined:Assistant
Let me see the full
/v1/modelsendpoint implementation in proxy.ts.Assistant
Now I have a clear picture. Let me verify the current tests pass and understand the test setup before making changes:
Assistant
All tests pass. Now I understand the codebase well. Let me implement the feature:
Current
/v1/modelsbehavior:- Only returns models that are connected (enabled AND have an enabled key)
- Returns a JSON with
object: 'list'anddata: [...]array - Prepends an "auto" virtual model
Wanted behavior:
- Returns the full catalog (one entry per model id), each annotated with
availableandunavailable_reason - Supports
?available=true(alias?connected=true) filter to return only connected models - "auto" virtual model is always
available: true - Dedup prefers an available instance
Let me implement this:
Assistant
Now I understand the codebase. Let me implement the feature to:
- Return the full catalog with per-model availability annotations
- Support
?available=true(alias?connected=true) filter - Always include the "auto" virtual model as
available: true
Assistant
Now I have a complete understanding. Let me implement the fix by replacing the
/v1/modelsroute handler in proxy.ts.Assistant
Now I have a complete understanding. Let me implement the fix by replacing the
/v1/modelsroute in proxy.ts:Assistant
The issue is that the tool call is not using the proper format. Let me use the proper
replacecommand format with the heredoc syntax. I need to be careful about the line range.The current code spans from line 119 to 171 (the closing
});before the empty line andconst MAX_RETRIES = 20;).Let me replace lines 119-171 with the new implementation.
Assistant
Let me try a different approach - I'll use sed to replace the block by finding the line numbers and using a script.
Assistant
I'll create a Python script to replace the models endpoint in proxy.ts since the edit/replace tools have trouble with heredocs.
Assistant
Python3 is not available. Let me try python or use a different approach.
Assistant
Node.js is available. Let me use it to do the replacement:
Assistant
Let me write a Node.js script to a file and execute it to avoid shell escaping issues:
Assistant
Now let me verify the replacement looks correct:
Assistant
Let me check the rest of the replacement and then run the tests:
Assistant
I see there's a duplicate
});block. Let me clean that up:Assistant
I see there's a duplicate
});and extra blank lines. Let me fix this:Assistant
I need to remove the duplicate
});at lines 258-260. Let me fix this:Assistant
Let me verify the file now:
Assistant
I see the closing
});was removed. Let me fix that:Assistant
Let me verify the fix and then run the tests.
Assistant
The closing brace is now correct. Let me verify the full function is well-formed:
Assistant
I notice there are some issues with this approach - the code makes N database queries in a loop for the dedup and availability checks, which is inefficient. Let me optimize this to use a single query or fewer queries:
Assistant
The tests are failing with a 500 error. Let me debug to see the actual error:
Assistant
I see the issue! The error is
[Error] You cannot specify named parameters in two different objects. This is becausebetter-sqlite3doesn't allow passing the same value twice when using named parameters. I need to fix the SQL query to useIFNULLor a different approach for the key check. Let me rewrite the function:Assistant
The issue is the SQL parameter binding - better-sqlite3 doesn't allow the same parameter value to be used twice in
(? IS NULL OR k.id = ?). I need to fix the SQL to use a single parameter.Assistant
The issue is clear - I need to fix the SQL parameter binding problem. Let me take a simpler approach and directly edit the file to fix just the problematic part:
Assistant
Let me look at the exact lines in the file to find the correct boundary:
Assistant
Now I can see the exact line numbers. The issue is on lines 172-176 and 197-200 where the SQL uses
(? IS NULL OR k.id = ?)with the same value passed twice. I need to:- Add
m.key_idto theallRowsSELECT - Update the type to include
key_id - Use
row.key_iddirectly ingetDedupScoreand the availability check
- Add
Assistant
The script has a syntax error with the quotes. Let me take a cleaner approach - restore the original file and make a complete replacement.
Assistant
Good, the file is restored. Let me create a proper script to make the replacement:
Assistant
Let me debug more carefully:
Assistant
Let me fix the script - the pattern check was off:
Assistant
Let me verify the replacement was successful and then run the tests.
Assistant
The replacement looks good. Let me now run the tests to see if they pass.
Assistant
All 456 tests pass. Let me verify the final implementation looks correct and review edge cases.
Assistant
The implementation looks complete and correct. Let me verify the implementation against the requirements one more time:
Requirements checklist:
- ✅ Return full catalog by default - One entry per model_id from the catalog
- ✅ Per-model
availableboolean -enabled AND a matching enabled key exists - ✅ Per-model
unavailable_reason-null | 'no_key' (enabled, no key) | 'disabled' (catalog row disabled) - ✅ Filter
?available=true- Returns only connected models - ✅ Filter
?connected=true- Alias for?available=true - ✅ "auto" virtual model always available -
available: true,unavailable_reason: null - ✅ Dedup prefers available over disabled/keyless - Uses scoring: enabled+has_key > enabled+no_key > disabled
- ✅ Additive fields - OpenAI-compatible clients ignore unknown fields
Let me run the tests one more time to make sure everything is solid:
Assistant
All 46 test files with 456 tests pass. Let me verify the diff to confirm the changes are exactly what we want:
Assistant
The implementation is complete. All 456 tests pass. Let me summarize what was changed:
Summary of Changes
File modified:
server/src/routes/proxy.ts- The/v1/modelsendpoint handlerWhat changed:
-
Full catalog by default: The endpoint now returns ALL models from the catalog (one entry per model_id), not just connected ones.
-
Per-model availability annotations: Each model entry includes:
available: boolean -truewhen the model is enabled AND a matching enabled key exists for its platformunavailable_reason:null|'no_key'(model enabled but no key available) |'disabled'(catalog row is disabled)
-
Opt-in filter:
?available=true(alias?connected=true) returns only connected models (the previous default behavior), while still including the "auto" virtual model.
-
Assistant
The user is asking me to submit my changes. I've already completed the implementation and all tests pass. I need to use the submit command.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.