refactor: unify loaded model grouping

This commit is contained in:
Dariusz L
2026-08-08 22:23:04 +02:00
parent 9795410748
commit 8724bb5d5b
3 changed files with 127 additions and 55 deletions
+41
View File
@@ -0,0 +1,41 @@
export function groupLoadedModelsByCategory(loadedModels = []) {
const byCategory = {};
for (const model of loadedModels) {
const category = model.category || 'unknown';
if (!byCategory[category]) {
byCategory[category] = { active: [], inactive: [] };
}
const isActive = model.active !== false && model.connected !== false;
byCategory[category][isActive ? 'active' : 'inactive'].push(model);
}
return byCategory;
}
export function buildLoadedModelTokenStrings(
byCategory,
getToken,
{ preserveEmptyCategories = false } = {}
) {
const build = filter => {
const categoryStrings = Object.entries(byCategory).map(([category, models]) => {
const selected = filter === 'all'
? [...models.active, ...models.inactive]
: models[filter];
return selected.map(model => getToken(model, category)).join(' ');
});
return (preserveEmptyCategories
? categoryStrings
: categoryStrings.filter(Boolean)
).join(' ');
};
return {
active: build('active'),
inactive: build('inactive'),
all: build('all'),
};
}