|
|
@@ -1,12 +1,42 @@
|
|
|
import { DEFAULT_MODELS } from "../constant";
|
|
|
import { LLMModel } from "../client/api";
|
|
|
|
|
|
+const CustomSeq = {
|
|
|
+ val: -1000, //To ensure the custom model located at front, start from -1000, refer to constant.ts
|
|
|
+ cache: new Map<string, number>(),
|
|
|
+ next: (id: string) => {
|
|
|
+ if (CustomSeq.cache.has(id)) {
|
|
|
+ return CustomSeq.cache.get(id) as number;
|
|
|
+ } else {
|
|
|
+ let seq = CustomSeq.val++;
|
|
|
+ CustomSeq.cache.set(id, seq);
|
|
|
+ return seq;
|
|
|
+ }
|
|
|
+ },
|
|
|
+};
|
|
|
+
|
|
|
const customProvider = (providerName: string) => ({
|
|
|
id: providerName.toLowerCase(),
|
|
|
providerName: providerName,
|
|
|
providerType: "custom",
|
|
|
+ sorted: CustomSeq.next(providerName),
|
|
|
});
|
|
|
|
|
|
+/**
|
|
|
+ * Sorts an array of models based on specified rules.
|
|
|
+ *
|
|
|
+ * First, sorted by provider; if the same, sorted by model
|
|
|
+ */
|
|
|
+const sortModelTable = (models: ReturnType<typeof collectModels>) =>
|
|
|
+ models.sort((a, b) => {
|
|
|
+ if (a.provider && b.provider) {
|
|
|
+ let cmp = a.provider.sorted - b.provider.sorted;
|
|
|
+ return cmp === 0 ? a.sorted - b.sorted : cmp;
|
|
|
+ } else {
|
|
|
+ return a.sorted - b.sorted;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
export function collectModelTable(
|
|
|
models: readonly LLMModel[],
|
|
|
customModels: string,
|
|
|
@@ -17,6 +47,7 @@ export function collectModelTable(
|
|
|
available: boolean;
|
|
|
name: string;
|
|
|
displayName: string;
|
|
|
+ sorted: number;
|
|
|
provider?: LLMModel["provider"]; // Marked as optional
|
|
|
isDefault?: boolean;
|
|
|
}
|
|
|
@@ -84,6 +115,7 @@ export function collectModelTable(
|
|
|
displayName: displayName || customModelName,
|
|
|
available,
|
|
|
provider, // Use optional chaining
|
|
|
+ sorted: CustomSeq.next(`${customModelName}@${provider?.id}`),
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
@@ -99,13 +131,16 @@ export function collectModelTableWithDefaultModel(
|
|
|
) {
|
|
|
let modelTable = collectModelTable(models, customModels);
|
|
|
if (defaultModel && defaultModel !== "") {
|
|
|
- if (defaultModel.includes('@')) {
|
|
|
+ if (defaultModel.includes("@")) {
|
|
|
if (defaultModel in modelTable) {
|
|
|
modelTable[defaultModel].isDefault = true;
|
|
|
}
|
|
|
} else {
|
|
|
for (const key of Object.keys(modelTable)) {
|
|
|
- if (modelTable[key].available && key.split('@').shift() == defaultModel) {
|
|
|
+ if (
|
|
|
+ modelTable[key].available &&
|
|
|
+ key.split("@").shift() == defaultModel
|
|
|
+ ) {
|
|
|
modelTable[key].isDefault = true;
|
|
|
break;
|
|
|
}
|
|
|
@@ -123,7 +158,9 @@ export function collectModels(
|
|
|
customModels: string,
|
|
|
) {
|
|
|
const modelTable = collectModelTable(models, customModels);
|
|
|
- const allModels = Object.values(modelTable);
|
|
|
+ let allModels = Object.values(modelTable);
|
|
|
+
|
|
|
+ allModels = sortModelTable(allModels);
|
|
|
|
|
|
return allModels;
|
|
|
}
|
|
|
@@ -138,7 +175,10 @@ export function collectModelsWithDefaultModel(
|
|
|
customModels,
|
|
|
defaultModel,
|
|
|
);
|
|
|
- const allModels = Object.values(modelTable);
|
|
|
+ let allModels = Object.values(modelTable);
|
|
|
+
|
|
|
+ allModels = sortModelTable(allModels);
|
|
|
+
|
|
|
return allModels;
|
|
|
}
|
|
|
|