File size: 2,219 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
export type ModelCatalogSource = "system" | "custom" | "imported" | "fallback" | "alias";

type ModelCatalogTarget = {
  modelId?: string | null;
  modelName?: string | null;
  alias?: string | null;
  source?: string | null;
};

function normalizeText(value: string | null | undefined): string {
  return typeof value === "string" ? value.trim().toLowerCase() : "";
}

export function normalizeModelCatalogSource(source?: string | null): ModelCatalogSource {
  const normalized = normalizeText(source);

  if (
    normalized === "api-sync" ||
    normalized === "synced" ||
    normalized === "auto-sync" ||
    normalized === "imported"
  ) {
    return "imported";
  }
  if (normalized === "fallback") return "fallback";
  if (normalized === "alias") return "alias";
  if (normalized === "custom" || normalized === "manual") {
    return "custom";
  }

  return "system";
}

export function getModelCatalogSourceLabel(source?: string | null): string {
  switch (normalizeModelCatalogSource(source)) {
    case "imported":
      return "Imported";
    case "custom":
      return "Custom";
    case "fallback":
      return "Fallback";
    case "alias":
      return "Alias";
    case "system":
    default:
      return "Built-in";
  }
}

function getModelCatalogSourceSearchText(source?: string | null): string {
  switch (normalizeModelCatalogSource(source)) {
    case "imported":
      return "synced api imported discovered";
    case "custom":
      return "custom manual imported";
    case "fallback":
      return "fallback compatible";
    case "alias":
      return "alias shortcut";
    case "system":
    default:
      return "built-in builtin official catalog";
  }
}

export function matchesModelCatalogQuery(query: string, target: ModelCatalogTarget): boolean {
  const normalizedQuery = normalizeText(query);
  if (!normalizedQuery) return true;

  const haystacks = [
    normalizeText(target.modelId),
    normalizeText(target.modelName),
    normalizeText(target.alias),
    getModelCatalogSourceSearchText(target.source),
  ].filter(Boolean);

  return haystacks.some((value) => value.includes(normalizedQuery));
}