Add up/down reorder buttons and order numbers to public models

- Add order number badge (#1, #2, #3) to each public model
- Add up/down arrow buttons to reorder models in the list
- Add persistPublicModelsOrder function to save reordered list
- Add server-side /api/admin/models/reorder endpoint
- Remove automatic alphabetical sorting to preserve custom order
- First model (#1) gets green background highlighting
This commit is contained in:
southseact-3d
2026-02-18 14:08:21 +00:00
parent 0ce352ad8d
commit 7e5dc8b62d
2 changed files with 99 additions and 3 deletions

View File

@@ -15857,7 +15857,7 @@ async function handleAdminModelsList(req, res) {
if (!session) return;
// Return new unified structure
sendJson(res, 200, {
publicModels: publicModels.sort((a, b) => (a.label || '').localeCompare(b.label || '')),
publicModels: publicModels,
providerChain,
// Legacy support
models: getConfiguredModels('opencode'),
@@ -15954,7 +15954,7 @@ async function handleAdminModelUpsert(req, res) {
await persistAdminModels();
sendJson(res, 200, {
model: payload,
publicModels: publicModels.sort((a, b) => (a.label || '').localeCompare(b.label || '')),
publicModels: publicModels,
providerChain,
models: getConfiguredModels('opencode'),
});
@@ -15964,6 +15964,45 @@ async function handleAdminModelUpsert(req, res) {
}
}
async function handleAdminModelsReorder(req, res) {
const session = requireAdminAuth(req, res);
if (!session) return;
try {
const body = await parseJsonBody(req);
if (!Array.isArray(body.models)) {
return sendJson(res, 400, { error: 'models array is required' });
}
// Validate that all provided IDs exist
const currentIds = new Set(publicModels.map(m => m.id));
const newIds = body.models.map(m => m.id);
const allExist = newIds.every(id => currentIds.has(id));
if (!allExist) {
return sendJson(res, 400, { error: 'Invalid model IDs in reorder request' });
}
// Reorder publicModels based on the provided order
const reordered = [];
body.models.forEach(m => {
const model = publicModels.find(pm => pm.id === m.id);
if (model) reordered.push(model);
});
publicModels = reordered;
await persistAdminModels();
sendJson(res, 200, {
ok: true,
publicModels: publicModels,
providerChain,
models: getConfiguredModels('opencode'),
});
} catch (error) {
sendJson(res, 400, { error: error.message || 'Unable to reorder models' });
}
}
async function handleAdminModelDelete(req, res, id) {
const session = requireAdminAuth(req, res);
if (!session) return;
@@ -18878,6 +18917,7 @@ async function routeInternal(req, res, url, pathname) {
if (req.method === 'GET' && pathname === '/api/admin/icons') return handleAdminListIcons(req, res);
if (req.method === 'GET' && pathname === '/api/admin/models') return handleAdminModelsList(req, res);
if (req.method === 'POST' && pathname === '/api/admin/models') return handleAdminModelUpsert(req, res);
if (req.method === 'POST' && pathname === '/api/admin/models/reorder') return handleAdminModelsReorder(req, res);
if (req.method === 'GET' && pathname === '/api/admin/openrouter-settings') return handleAdminOpenRouterSettingsGet(req, res);
if (req.method === 'POST' && pathname === '/api/admin/openrouter-settings') return handleAdminOpenRouterSettingsPost(req, res);
if (req.method === 'GET' && pathname === '/api/admin/mistral-settings') return handleAdminMistralSettingsGet(req, res);