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