56 lines · 1.7 KB · TypeScript
RawDownload
import type {  ModelCapabilities,  ProviderOptionDescriptor,  ProviderOptionSelection,} from "@t3tools/contracts";import {  buildProviderOptionSelectionsFromDescriptors,  getProviderOptionDescriptors,} from "@t3tools/shared/model"; export function resolveProviderOptionDescriptors(input: {  readonly capabilities: ModelCapabilities | null | undefined;  readonly selections: ReadonlyArray<ProviderOptionSelection> | null | undefined;}): ReadonlyArray<ProviderOptionDescriptor> {  if (!input.capabilities) {    return [];  }  return getProviderOptionDescriptors({    caps: input.capabilities,    selections: input.selections,  });} /** * Applies one option change (by descriptor id) and returns the full selection * list to store on the model selection, or null when the change doesn't match * an advertised descriptor / choice. */export function applyProviderOptionSelection(  descriptors: ReadonlyArray<ProviderOptionDescriptor>,  change: ProviderOptionSelection,): ReadonlyArray<ProviderOptionSelection> | null {  const descriptor = descriptors.find((candidate) => candidate.id === change.id);  if (!descriptor) {    return null;  }  if (    (descriptor.type === "boolean" && typeof change.value !== "boolean") ||    (descriptor.type === "select" &&      (typeof change.value !== "string" ||        !descriptor.options.some((option) => option.id === change.value)))  ) {    return null;  }   const nextDescriptors = descriptors.map((candidate) =>    candidate.id === descriptor.id      ? {          ...candidate,          currentValue: change.value,        }      : candidate,  ) as ReadonlyArray<ProviderOptionDescriptor>;   return buildProviderOptionSelectionsFromDescriptors(nextDescriptors) ?? [];}