Open History to see the last commit
1import type {
2 ModelCapabilities,
3 ProviderOptionDescriptor,
4 ProviderOptionSelection,
5} from "@t3tools/contracts";
6import {
7 buildProviderOptionSelectionsFromDescriptors,
8 getProviderOptionCurrentLabel,
9 getProviderOptionDescriptors,
10} from "@t3tools/shared/model";
11
12export function resolveProviderOptionDescriptors(input: {
13 readonly capabilities: ModelCapabilities | null | undefined;
14 readonly selections: ReadonlyArray<ProviderOptionSelection> | null | undefined;
15}): ReadonlyArray<ProviderOptionDescriptor> {
16 if (!input.capabilities) {
17 return [];
18 }
19 return getProviderOptionDescriptors({
20 caps: input.capabilities,
21 selections: input.selections,
22 });
23}
24
25/**
26 * Labels for the option values currently in effect (select values plus
27 * enabled booleans), used to summarize the thread configuration in the
28 * composer trigger pill.
29 */
30export function providerOptionValueLabels(
31 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
32): ReadonlyArray<string> {
33 return descriptors.flatMap((descriptor) => {
34 if (descriptor.type === "boolean") {
35 return descriptor.currentValue ? [descriptor.label] : [];
36 }
37 const label = getProviderOptionCurrentLabel(descriptor);
38 return label ? [label] : [];
39 });
40}
41
42/**
43 * Applies one option change (by descriptor id) and returns the full selection
44 * list to store on the model selection, or null when the change doesn't match
45 * an advertised descriptor / choice.
46 */
47export function applyProviderOptionSelection(
48 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
49 change: ProviderOptionSelection,
50): ReadonlyArray<ProviderOptionSelection> | null {
51 const descriptor = descriptors.find((candidate) => candidate.id === change.id);
52 if (!descriptor) {
53 return null;
54 }
55 if (
56 (descriptor.type === "boolean" && typeof change.value !== "boolean") ||
57 (descriptor.type === "select" &&
58 (typeof change.value !== "string" ||
59 !descriptor.options.some((option) => option.id === change.value)))
60 ) {
61 return null;
62 }
63
64 const nextDescriptors = descriptors.map((candidate) =>
65 candidate.id === descriptor.id
66 ? {
67 ...candidate,
68 currentValue: change.value,
69 }
70 : candidate,
71 ) as ReadonlyArray<ProviderOptionDescriptor>;
72
73 return buildProviderOptionSelectionsFromDescriptors(nextDescriptors) ?? [];
74}