Open History to see the last commit
1import {
2 type ModelCapabilities,
3 MUSE_REASONING_EFFORT_OPTIONS,
4 ProviderDriverKind,
5 type ProviderOptionDescriptor,
6} from "@t3tools/contracts";
7import { type CustomModelDefinition, createModelCapabilities } from "@t3tools/shared/model";
8
9/** Editable mirror of a `ProviderOptionChoice`. `key` is only a React key. */
10export interface EditorChoice {
11 readonly key: string;
12 readonly id: string;
13 readonly label: string;
14 readonly isDefault: boolean;
15 readonly description?: string;
16}
17
18/** Editable mirror of a `ProviderOptionDescriptor`. `key` is only a React key. */
19export interface EditorDescriptor {
20 readonly key: string;
21 readonly type: "select" | "boolean";
22 readonly id: string;
23 readonly label: string;
24 readonly choices: ReadonlyArray<EditorChoice>;
25 readonly currentBooleanValue?: boolean | undefined;
26 readonly description?: string | undefined;
27}
28
29export interface CustomModelDraft {
30 readonly slug: string;
31 readonly name: string;
32 readonly descriptors: ReadonlyArray<EditorDescriptor>;
33}
34
35export interface DescriptorPreset {
36 readonly id: string;
37 readonly label: string;
38 readonly type: "select" | "boolean";
39 readonly choices?: ReadonlyArray<{ id: string; label: string; isDefault?: boolean }>;
40}
41
42const EFFORT_CHOICES = [
43 { id: "low", label: "Low" },
44 { id: "medium", label: "Medium", isDefault: true },
45 { id: "high", label: "High" },
46 { id: "xhigh", label: "Extra High" },
47] as const;
48
49/**
50 * Option ids each adapter actually reads off a turn's model selection, with
51 * the usual choices pre-filled. Anything else the user types is stored
52 * verbatim but will be ignored by the driver.
53 */
54export const DESCRIPTOR_PRESETS_BY_KIND: Partial<
55 Record<ProviderDriverKind, ReadonlyArray<DescriptorPreset>>
56> = {
57 [ProviderDriverKind.make("codex")]: [
58 { id: "reasoningEffort", label: "Reasoning", type: "select", choices: EFFORT_CHOICES },
59 {
60 id: "serviceTier",
61 label: "Speed",
62 type: "select",
63 choices: [
64 { id: "default", label: "Standard", isDefault: true },
65 { id: "fast", label: "Fast" },
66 ],
67 },
68 ],
69 [ProviderDriverKind.make("claudeAgent")]: [
70 {
71 id: "effort",
72 label: "Reasoning",
73 type: "select",
74 choices: [
75 { id: "low", label: "Low" },
76 { id: "medium", label: "Medium" },
77 { id: "high", label: "High", isDefault: true },
78 { id: "xhigh", label: "Extra High" },
79 { id: "max", label: "Max" },
80 ],
81 },
82 { id: "fastMode", label: "Fast Mode", type: "boolean" },
83 { id: "thinking", label: "Thinking", type: "boolean" },
84 ],
85 [ProviderDriverKind.make("cursor")]: [
86 { id: "reasoning", label: "Reasoning", type: "select", choices: EFFORT_CHOICES },
87 { id: "fastMode", label: "Fast Mode", type: "boolean" },
88 { id: "thinking", label: "Thinking", type: "boolean" },
89 ],
90 [ProviderDriverKind.make("grok")]: [
91 { id: "reasoningEffort", label: "Reasoning", type: "select", choices: EFFORT_CHOICES },
92 ],
93 [ProviderDriverKind.make("muse")]: [
94 {
95 id: "reasoningEffort",
96 label: "Reasoning",
97 type: "select",
98 choices: MUSE_REASONING_EFFORT_OPTIONS.filter((choice) =>
99 ["low", "medium", "high", "xhigh", "max"].includes(choice.id),
100 ),
101 },
102 ],
103 [ProviderDriverKind.make("pi")]: [
104 {
105 id: "thinking",
106 label: "Thinking",
107 type: "select",
108 choices: [
109 { id: "off", label: "Off" },
110 { id: "minimal", label: "Minimal" },
111 { id: "low", label: "Low" },
112 { id: "medium", label: "Medium", isDefault: true },
113 { id: "high", label: "High" },
114 { id: "xhigh", label: "Extra High" },
115 { id: "max", label: "Max" },
116 ],
117 },
118 ],
119 [ProviderDriverKind.make("opencode")]: [
120 { id: "variant", label: "Reasoning", type: "select", choices: EFFORT_CHOICES },
121 {
122 id: "agent",
123 label: "Agent",
124 type: "select",
125 choices: [
126 { id: "build", label: "Build", isDefault: true },
127 { id: "plan", label: "Plan" },
128 ],
129 },
130 ],
131};
132
133let nextKey = 0;
134function newEditorKey(): string {
135 nextKey += 1;
136 return `k${nextKey}`;
137}
138
139export function choiceFromPreset(choice: {
140 id: string;
141 label: string;
142 isDefault?: boolean;
143}): EditorChoice {
144 return { key: newEditorKey(), id: choice.id, label: choice.label, isDefault: !!choice.isDefault };
145}
146
147export function descriptorFromPreset(preset: DescriptorPreset): EditorDescriptor {
148 return {
149 key: newEditorKey(),
150 type: preset.type,
151 id: preset.id,
152 label: preset.label,
153 choices: (preset.choices ?? []).map(choiceFromPreset),
154 };
155}
156
157export function emptyEditorDescriptor(): EditorDescriptor {
158 return { key: newEditorKey(), type: "select", id: "", label: "", choices: [] };
159}
160
161export function emptyEditorChoice(): EditorChoice {
162 return { key: newEditorKey(), id: "", label: "", isDefault: false };
163}
164
165/**
166 * Prompt-injected choices (Claude's `ultrathink`) are delivered as prompt text
167 * by built-in runtime profiles a custom entry does not have, so they are
168 * dropped rather than stored as a plain option value.
169 */
170function descriptorToEditor(descriptor: ProviderOptionDescriptor): EditorDescriptor {
171 const promptInjected = new Set(
172 descriptor.type === "select" ? (descriptor.promptInjectedValues ?? []) : [],
173 );
174 const choices =
175 descriptor.type === "select"
176 ? descriptor.options.filter((option) => !promptInjected.has(option.id))
177 : [];
178 const defaultChoice =
179 choices.find((option) => option.id === descriptor.currentValue) ??
180 choices.find((option) => option.isDefault);
181 return {
182 key: newEditorKey(),
183 type: descriptor.type,
184 id: descriptor.id,
185 label: descriptor.label,
186 ...(descriptor.description !== undefined ? { description: descriptor.description } : {}),
187 ...(descriptor.type === "boolean" && descriptor.currentValue !== undefined
188 ? { currentBooleanValue: descriptor.currentValue }
189 : {}),
190 choices: choices.map((option) => ({
191 key: newEditorKey(),
192 id: option.id,
193 label: option.label,
194 ...(option.description !== undefined ? { description: option.description } : {}),
195 isDefault: option === defaultChoice,
196 })),
197 };
198}
199
200export function draftFromDefinition(entry: CustomModelDefinition): CustomModelDraft {
201 return {
202 slug: entry.slug,
203 name: entry.name === entry.slug ? "" : entry.name,
204 descriptors: (entry.capabilities?.optionDescriptors ?? []).map(descriptorToEditor),
205 };
206}
207
208/** Claude context choices require runtime suffix mappings that custom entries do not carry. */
209export function descriptorsFromCapabilities(
210 capabilities: ModelCapabilities | null | undefined,
211 driverKind: ProviderDriverKind | null,
212): EditorDescriptor[] {
213 return (capabilities?.optionDescriptors ?? [])
214 .filter((descriptor) => driverKind !== "claudeAgent" || descriptor.id !== "contextWindow")
215 .map(descriptorToEditor);
216}
217
218/**
219 * Validate the draft before saving. Returns the first problem in reading
220 * order so the message is actionable, or `null` when the draft is sound.
221 */
222export function validateDraft(draft: CustomModelDraft): string | null {
223 const seenIds = new Set<string>();
224 for (const [index, descriptor] of draft.descriptors.entries()) {
225 const position = `Option ${index + 1}`;
226 const id = descriptor.id.trim();
227 if (!id) return `${position} needs an id.`;
228 if (seenIds.has(id)) return `${position}: id "${id}" is used twice.`;
229 seenIds.add(id);
230 if (!descriptor.label.trim()) return `${position} needs a label.`;
231 if (descriptor.type !== "select") continue;
232 if (descriptor.choices.length === 0) return `${position} needs at least one choice.`;
233 const seenChoices = new Set<string>();
234 for (const choice of descriptor.choices) {
235 const choiceId = choice.id.trim();
236 if (!choiceId) return `${position} has a choice without a value.`;
237 if (seenChoices.has(choiceId)) {
238 return `${position}: choice "${choiceId}" is used twice.`;
239 }
240 seenChoices.add(choiceId);
241 }
242 }
243 return null;
244}
245
246/** Convert a validated draft back into a definition. Blank name → slug. */
247export function definitionFromDraft(draft: CustomModelDraft): CustomModelDefinition {
248 const descriptors: ProviderOptionDescriptor[] = draft.descriptors.map((descriptor) => {
249 const id = descriptor.id.trim();
250 const label = descriptor.label.trim();
251 if (descriptor.type === "boolean") {
252 return {
253 id,
254 label,
255 type: "boolean",
256 ...(descriptor.description !== undefined ? { description: descriptor.description } : {}),
257 ...(descriptor.currentBooleanValue !== undefined
258 ? { currentValue: descriptor.currentBooleanValue }
259 : {}),
260 };
261 }
262 const options = descriptor.choices.map((choice) => ({
263 id: choice.id.trim(),
264 label: choice.label.trim() || choice.id.trim(),
265 ...(choice.description !== undefined ? { description: choice.description } : {}),
266 ...(choice.isDefault ? { isDefault: true } : {}),
267 }));
268 const currentValue = options.find((option) => option.isDefault)?.id;
269 return {
270 id,
271 label,
272 type: "select",
273 ...(descriptor.description !== undefined ? { description: descriptor.description } : {}),
274 options,
275 ...(currentValue ? { currentValue } : {}),
276 };
277 });
278 const name = draft.name.trim();
279 return {
280 slug: draft.slug,
281 name: name || draft.slug,
282 capabilities:
283 descriptors.length > 0 ? createModelCapabilities({ optionDescriptors: descriptors }) : null,
284 };
285}