Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

customModelEditor.logic.ts

258 lines8.6 KB

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