model.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // server custom models
  23. customModels
  24. .split(",")
  25. .filter((v) => !!v && v.length > 0)
  26. .forEach((m) => {
  27. const available = !m.startsWith("-");
  28. const nameConfig =
  29. m.startsWith("+") || m.startsWith("-") ? m.slice(1) : m;
  30. const [name, displayName] = nameConfig.split("=");
  31. // enable or disable all models
  32. if (name === "all") {
  33. Object.values(modelTable).forEach((model) => (model.available = available));
  34. } else {
  35. modelTable[name] = {
  36. name,
  37. displayName: displayName || name,
  38. available,
  39. provider: modelTable[name]?.provider, // Use optional chaining
  40. };
  41. }
  42. });
  43. return modelTable;
  44. }
  45. /**
  46. * Generate full model table.
  47. */
  48. export function collectModels(
  49. models: readonly LLMModel[],
  50. customModels: string,
  51. ) {
  52. const modelTable = collectModelTable(models, customModels);
  53. const allModels = Object.values(modelTable);
  54. return allModels;
  55. }