Open History to see the last commit
1import type {
2 ModelCapabilities,
3 ProviderOptionDescriptor,
4 ProviderOptionSelection,
5} from "@t3tools/contracts";
6import type { MenuAction } from "@react-native-menu/menu";
7import {
8 buildProviderOptionSelectionsFromDescriptors,
9 getProviderOptionCurrentLabel,
10 getProviderOptionCurrentValue,
11 getProviderOptionDescriptors,
12} from "@t3tools/shared/model";
13
14const PROVIDER_OPTION_EVENT_PREFIX = "provider-option:";
15
16function providerOptionEvent(id: string, value: string | boolean): string {
17 return `${PROVIDER_OPTION_EVENT_PREFIX}${encodeURIComponent(JSON.stringify({ id, value }))}`;
18}
19
20function parseProviderOptionEvent(
21 event: string,
22): { readonly id: string; readonly value: string | boolean } | null {
23 if (!event.startsWith(PROVIDER_OPTION_EVENT_PREFIX)) {
24 return null;
25 }
26
27 try {
28 const parsed: unknown = JSON.parse(
29 decodeURIComponent(event.slice(PROVIDER_OPTION_EVENT_PREFIX.length)),
30 );
31 if (
32 typeof parsed === "object" &&
33 parsed !== null &&
34 "id" in parsed &&
35 typeof parsed.id === "string" &&
36 "value" in parsed &&
37 (typeof parsed.value === "string" || typeof parsed.value === "boolean")
38 ) {
39 return { id: parsed.id, value: parsed.value };
40 }
41 } catch {
42 return null;
43 }
44
45 return null;
46}
47
48export function resolveProviderOptionDescriptors(input: {
49 readonly capabilities: ModelCapabilities | null | undefined;
50 readonly selections: ReadonlyArray<ProviderOptionSelection> | null | undefined;
51}): ReadonlyArray<ProviderOptionDescriptor> {
52 if (!input.capabilities) {
53 return [];
54 }
55 return getProviderOptionDescriptors({
56 caps: input.capabilities,
57 selections: input.selections,
58 });
59}
60
61export function buildProviderOptionMenuActions(
62 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
63): ReadonlyArray<MenuAction> {
64 return descriptors.map((descriptor) => {
65 const currentValue =
66 descriptor.type === "boolean"
67 ? (descriptor.currentValue ?? false)
68 : getProviderOptionCurrentValue(descriptor);
69 const choices =
70 descriptor.type === "select"
71 ? descriptor.options.map((option) => ({
72 id: providerOptionEvent(descriptor.id, option.id),
73 title: `${option.label}${option.isDefault ? " (default)" : ""}`,
74 state: currentValue === option.id ? ("on" as const) : undefined,
75 }))
76 : ([false, true] as const).map((value) => ({
77 id: providerOptionEvent(descriptor.id, value),
78 title: value ? "On" : "Off",
79 state: currentValue === value ? ("on" as const) : undefined,
80 }));
81
82 return {
83 id: `provider-option-menu:${descriptor.id}`,
84 title: descriptor.label,
85 subtitle:
86 descriptor.type === "boolean"
87 ? currentValue
88 ? "On"
89 : "Off"
90 : getProviderOptionCurrentLabel(descriptor),
91 subactions: choices,
92 };
93 });
94}
95
96export function providerOptionsConfigurationLabel(
97 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
98): string {
99 const labels = descriptors.flatMap((descriptor) => {
100 if (descriptor.type === "boolean") {
101 return descriptor.currentValue ? [descriptor.label] : [];
102 }
103 const label = getProviderOptionCurrentLabel(descriptor);
104 return label ? [label] : [];
105 });
106 return labels.length > 0 ? labels.join(" · ") : "Configuration";
107}
108
109export function applyProviderOptionMenuEvent(
110 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
111 event: string,
112): ReadonlyArray<ProviderOptionSelection> | null {
113 const selection = parseProviderOptionEvent(event);
114 if (!selection) {
115 return null;
116 }
117
118 const descriptor = descriptors.find((candidate) => candidate.id === selection.id);
119 if (!descriptor) {
120 return null;
121 }
122 if (
123 (descriptor.type === "boolean" && typeof selection.value !== "boolean") ||
124 (descriptor.type === "select" &&
125 (typeof selection.value !== "string" ||
126 !descriptor.options.some((option) => option.id === selection.value)))
127 ) {
128 return null;
129 }
130
131 const nextDescriptors = descriptors.map((candidate) =>
132 candidate.id === descriptor.id
133 ? {
134 ...candidate,
135 currentValue: selection.value,
136 }
137 : candidate,
138 ) as ReadonlyArray<ProviderOptionDescriptor>;
139
140 return buildProviderOptionSelectionsFromDescriptors(nextDescriptors) ?? [];
141}