Open History to see the last commit
1import type { MenuAction } from "@react-native-menu/menu";
2import type { ModelSelection, ProviderOptionDescriptor, RuntimeMode } from "@t3tools/contracts";
3import {
4 getProviderOptionCurrentLabel,
5 getProviderOptionCurrentValue,
6} from "@t3tools/shared/model";
7
8import type { ModelOption, ProviderGroup } from "../../lib/modelOptions";
9
10/**
11 * Desktop-oriented effort keywords that don't belong in the phone picker.
12 * Prompt-injected values (ultrathink and friends) are filtered from the
13 * descriptor metadata; ultracode is a real option but a workflow trigger, not
14 * a reasoning level. A value set elsewhere still displays, it just isn't
15 * offered.
16 */
17export const HIDDEN_EFFORT_OPTION_IDS: ReadonlySet<string> = new Set(["ultracode"]);
18
19export const RUNTIME_MODE_CHOICES: ReadonlyArray<{
20 readonly mode: RuntimeMode;
21 readonly label: string;
22 readonly shortLabel: string;
23}> = [
24 { mode: "approval-required", label: "Approve actions", shortLabel: "Approve" },
25 { mode: "auto-accept-edits", label: "Auto-accept edits", shortLabel: "Edits" },
26 { mode: "auto", label: "Auto", shortLabel: "Auto" },
27 { mode: "full-access", label: "Full access", shortLabel: "Full" },
28];
29
30export function selectableChoices(
31 descriptor: Extract<ProviderOptionDescriptor, { type: "select" }>,
32) {
33 const injected = new Set(descriptor.promptInjectedValues ?? []);
34 return descriptor.options.filter(
35 (option) => !injected.has(option.id) && !HIDDEN_EFFORT_OPTION_IDS.has(option.id),
36 );
37}
38
39export type ThreadSettingsMenuEvent =
40 | { readonly type: "select-model"; readonly option: ModelOption }
41 | { readonly type: "set-option"; readonly optionId: string; readonly value: string | boolean }
42 | { readonly type: "set-runtime"; readonly mode: RuntimeMode };
43
44export type ThreadSettingsMenu = {
45 readonly actions: MenuAction[];
46 /** Menu action id → the change it applies, for the onPressAction dispatch. */
47 readonly events: ReadonlyMap<string, ThreadSettingsMenuEvent>;
48};
49
50/**
51 * Native menu replacement for the thread settings sheet (model, select and
52 * boolean provider options, runtime mode). The menu presents from the
53 * composer pill without resigning the keyboard, so adjusting settings never
54 * bounces focus. A thread is bound to one harness, so the menu covers the
55 * sheet's full surface for existing threads; the sheet remains the Android
56 * and new-task-draft surface.
57 *
58 * Selections apply immediately — the sheet's stage-then-Save flow only exists
59 * because the sheet batches a model change with its option edits.
60 */
61export function buildThreadSettingsMenu(input: {
62 readonly providerGroups: ReadonlyArray<ProviderGroup>;
63 readonly selectedModel: ModelSelection | null;
64 readonly optionDescriptors: ReadonlyArray<ProviderOptionDescriptor>;
65 readonly runtimeMode: RuntimeMode;
66}): ThreadSettingsMenu {
67 const events = new Map<string, ThreadSettingsMenuEvent>();
68 const actions: MenuAction[] = [];
69
70 const isSelected = (option: ModelOption) =>
71 option.selection.instanceId === input.selectedModel?.instanceId &&
72 option.selection.model === input.selectedModel.model;
73
74 // Only top-level leaves (boolean toggles) keep the menu presented (iOS
75 // 16+): the root refreshes in place with clean chrome. Picks inside nested
76 // submenus close the menu — keeping the submenu presented renders an
77 // expanded-submenu header with no way to pop back to the root, and the
78 // bottom-anchored collapse back out travels the levels' height difference.
79 const keepPresented = { keepsMenuPresented: true } as const;
80
81 const modelAction = (option: ModelOption, id: string): MenuAction => {
82 events.set(id, { type: "select-model", option });
83 return {
84 id,
85 title: option.label,
86 ...(option.isDefault ? { subtitle: "Default" } : {}),
87 state: isSelected(option) ? "on" : "off",
88 };
89 };
90
91 const modelItems: MenuAction[] = [];
92 const legacyItems: MenuAction[] = [];
93 let selectedModelLabel: string | undefined;
94 input.providerGroups.forEach((group, groupIndex) => {
95 const groupItems: MenuAction[] = [];
96 group.models.forEach((option, modelIndex) => {
97 if (isSelected(option)) {
98 selectedModelLabel = option.label;
99 }
100 const id = `model:${groupIndex}:${modelIndex}`;
101 // A highlighted legacy model stays in the main list (mirroring the
102 // sheet) so the checkmark isn't hidden behind the Legacy fold.
103 if (option.isLegacy && !isSelected(option)) {
104 legacyItems.push(modelAction(option, id));
105 } else {
106 groupItems.push(modelAction(option, id));
107 }
108 });
109 if (groupItems.length === 0) {
110 return;
111 }
112 // A thread is bound to one harness, so provider sections only appear for
113 // multi-group callers (the new-task draft, if it ever adopts the menu).
114 if (input.providerGroups.length > 1) {
115 modelItems.push({
116 id: `model-group:${groupIndex}`,
117 title: group.providerLabel,
118 displayInline: true,
119 subactions: groupItems,
120 });
121 } else {
122 modelItems.push(...groupItems);
123 }
124 });
125 if (legacyItems.length > 0) {
126 modelItems.push({
127 id: "legacy-models",
128 title: "Legacy Models",
129 subactions: legacyItems,
130 });
131 }
132 if (modelItems.length > 0) {
133 actions.push({
134 id: "model",
135 title: "Model",
136 ...(selectedModelLabel === undefined
137 ? input.selectedModel
138 ? { subtitle: input.selectedModel.model }
139 : {}
140 : { subtitle: selectedModelLabel }),
141 subactions: modelItems,
142 });
143 }
144
145 for (const descriptor of input.optionDescriptors) {
146 if (descriptor.type === "boolean") {
147 const id = `option:${descriptor.id}`;
148 events.set(id, {
149 type: "set-option",
150 optionId: descriptor.id,
151 value: !(descriptor.currentValue ?? false),
152 });
153 actions.push({
154 id,
155 title: descriptor.label,
156 state: descriptor.currentValue ? "on" : "off",
157 attributes: keepPresented,
158 });
159 continue;
160 }
161 const currentValue = getProviderOptionCurrentValue(descriptor);
162 const choices = selectableChoices(descriptor).map((choice): MenuAction => {
163 const id = `option:${descriptor.id}:${choice.id}`;
164 events.set(id, { type: "set-option", optionId: descriptor.id, value: choice.id });
165 return {
166 id,
167 title: choice.label,
168 state: choice.id === currentValue ? "on" : "off",
169 };
170 });
171 if (choices.length === 0) {
172 continue;
173 }
174 const currentLabel = getProviderOptionCurrentLabel(descriptor);
175 actions.push({
176 id: `option:${descriptor.id}`,
177 title: descriptor.label,
178 ...(currentLabel === undefined ? {} : { subtitle: currentLabel }),
179 subactions: choices,
180 });
181 }
182
183 const runtimeLabel = RUNTIME_MODE_CHOICES.find(
184 (choice) => choice.mode === input.runtimeMode,
185 )?.label;
186 actions.push({
187 id: "runtime",
188 title: "Runtime",
189 ...(runtimeLabel === undefined ? {} : { subtitle: runtimeLabel }),
190 subactions: RUNTIME_MODE_CHOICES.map((choice): MenuAction => {
191 const id = `runtime:${choice.mode}`;
192 events.set(id, { type: "set-runtime", mode: choice.mode });
193 return {
194 id,
195 title: choice.label,
196 state: choice.mode === input.runtimeMode ? "on" : "off",
197 };
198 }),
199 });
200
201 return { actions, events };
202}