model.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { LLMModel } from "../client/api";
  2. export function collectModelTable(
  3. models: readonly LLMModel[],
  4. customModels: string,
  5. ) {
  6. const modelTable: Record<
  7. string,
  8. {
  9. available: boolean;
  10. name: string;
  11. displayName: string;
  12. provider?: LLMModel["provider"]; // Marked as optional
  13. }
  14. > = {};
  15. // default models
  16. models.forEach((m) => {
  17. modelTable[m.name] = {
  18. ...m,
  19. displayName: m.name, // 'provider' is copied over if it exists
  20. };
  21. });
  22. const customProvider = (modelName: string) => ({
  23. id: modelName,
  24. providerName: "",
  25. providerType: "custom",
  26. });
  27. // server custom models
  28. customModels
  29. .split(",")
  30. .filter((v) => !!v && v.length > 0)
  31. .forEach((m) => {
  32. const available = !m.startsWith("-");
  33. const nameConfig =
  34. m.startsWith("+") || m.startsWith("-") ? m.slice(1) : m;
  35. const [name, displayName] = nameConfig.split("=");
  36. // enable or disable all models
  37. if (name === "all") {
  38. Object.values(modelTable).forEach(
  39. (model) => (model.available = available),
  40. );
  41. } else {
  42. modelTable[name] = {
  43. name,
  44. displayName: displayName || name,
  45. available,
  46. provider: modelTable[name]?.provider ?? customProvider(name), // Use optional chaining
  47. };
  48. }
  49. });
  50. return modelTable;
  51. }
  52. /**
  53. * Generate full model table.
  54. */
  55. export function collectModels(
  56. models: readonly LLMModel[],
  57. customModels: string,
  58. ) {
  59. const modelTable = collectModelTable(models, customModels);
  60. const allModels = Object.values(modelTable);
  61. return allModels;
  62. }