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