| 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; |
| } |
|
|