File size: 2,815 Bytes
df23150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
import {
  PROVIDER_BINDING_KEY,
  SUPPORTED_ADAPTER_TYPES,
} from "../constants.js";
import type {
  AgentRecord,
  CompanyRecord,
  GlobalProviderRecord,
  ProviderModelEntry,
} from "../types.js";

export type ManagedBinding = {
  providerId: string;
  providerName?: string;
  providerBaseUrl?: string;
  managedByPlugin?: boolean;
  syncedAt?: string;
};

export function sortCompanies(companies: CompanyRecord[]): CompanyRecord[] {
  return [...companies].sort((left, right) => left.name.localeCompare(right.name));
}

export function sortAgents(agents: AgentRecord[]): AgentRecord[] {
  return [...agents].sort((left, right) => left.name.localeCompare(right.name));
}

export function isSupportedAgent(agent: AgentRecord): boolean {
  return SUPPORTED_ADAPTER_TYPES.includes(
    String(agent.adapterType ?? "") as (typeof SUPPORTED_ADAPTER_TYPES)[number],
  );
}

export function readManagedBinding(agent: AgentRecord): ManagedBinding | null {
  const adapterConfig = agent.adapterConfig;
  if (!adapterConfig || typeof adapterConfig !== "object" || Array.isArray(adapterConfig)) {
    return null;
  }
  const raw = (adapterConfig as Record<string, unknown>)[PROVIDER_BINDING_KEY];
  if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
    return null;
  }
  const binding = raw as Record<string, unknown>;
  if (typeof binding.providerId !== "string" || binding.providerId.trim().length === 0) {
    return null;
  }
  return {
    providerId: binding.providerId,
    providerName: typeof binding.providerName === "string" ? binding.providerName : undefined,
    providerBaseUrl: typeof binding.providerBaseUrl === "string" ? binding.providerBaseUrl : undefined,
    managedByPlugin: binding.managedByPlugin === true,
    syncedAt: typeof binding.syncedAt === "string" ? binding.syncedAt : undefined,
  };
}

export function buildProviderModelOptions(
  provider: GlobalProviderRecord | null | undefined,
  currentModel?: string | null,
): ProviderModelEntry[] {
  const options = new Map<string, ProviderModelEntry>();

  for (const entry of provider?.modelCatalog ?? []) {
    const id = entry.id.trim();
    if (!id || options.has(id)) continue;
    options.set(id, {
      id,
      label: entry.label.trim() || id,
      raw: entry.raw ?? null,
    });
  }

  const fallback = currentModel?.trim();
  if (fallback && !options.has(fallback)) {
    options.set(fallback, {
      id: fallback,
      label: fallback,
      raw: null,
    });
  }

  return Array.from(options.values()).sort((left, right) => left.id.localeCompare(right.id));
}

export function findProvider(
  providers: GlobalProviderRecord[],
  providerId: string | null | undefined,
): GlobalProviderRecord | null {
  if (!providerId) return null;
  return providers.find((provider) => provider.id === providerId) ?? null;
}