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

customModelEditor.logic.ts

262 lines8.5 KB

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;
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;
25​
26export interface CustomModelDraft {
27 readonly slug: string;
28 readonly name: string;
29 readonly descriptors: ReadonlyArray<EditorDescriptor>;
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 }>;
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}`;
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 };
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 };
138​
139export function emptyEditorDescriptor(): EditorDescriptor {
140 return { key: newEditorKey(), type: "select", id: "", label: "", choices: [] };
142​
143export function emptyEditorChoice(): EditorChoice {
144 return { key: newEditorKey(), id: "", label: "", isDefault: false };
146​
147/**
148 * Prompt-injected choices (Claude's `ultrathink`) are delivered as prompt text
149 * by built-in runtime profiles a custom entry does not have, so they are
150 * dropped rather than stored as a plain option value.
151 */
152function descriptorToEditor(descriptor: ProviderOptionDescriptor): EditorDescriptor {
153 const promptInjected = new Set(
154 descriptor.type === "select" ? (descriptor.promptInjectedValues ?? []) : [],
155 );
156 const choices =
157 descriptor.type === "select"
158 ? descriptor.options.filter((option) => !promptInjected.has(option.id))
159 : [];
160 const defaultChoice =
161 choices.find((option) => option.id === descriptor.currentValue) ??
162 choices.find((option) => option.isDefault);
163 return {
164 key: newEditorKey(),
165 type: descriptor.type,
166 id: descriptor.id,
167 label: descriptor.label,
168 ...(descriptor.description !== undefined ? { description: descriptor.description } : {}),
169 ...(descriptor.type === "boolean" && descriptor.currentValue !== undefined
170 ? { currentBooleanValue: descriptor.currentValue }
171 : {}),
172 choices: choices.map((option) => ({
173 key: newEditorKey(),
174 id: option.id,
175 label: option.label,
176 ...(option.description !== undefined ? { description: option.description } : {}),
177 isDefault: option === defaultChoice,
178 })),
179 };
181​
182export function draftFromDefinition(entry: CustomModelDefinition): CustomModelDraft {
183 return {
184 slug: entry.slug,
185 name: entry.name === entry.slug ? "" : entry.name,
186 descriptors: (entry.capabilities?.optionDescriptors ?? []).map(descriptorToEditor),
187 };
189​
190/** Claude context choices require runtime suffix mappings that custom entries do not carry. */
191export function descriptorsFromCapabilities(
192 capabilities: ModelCapabilities | null | undefined,
193 driverKind: ProviderDriverKind | null,
194): EditorDescriptor[] {
195 return (capabilities?.optionDescriptors ?? [])
196 .filter((descriptor) => driverKind !== "claudeAgent" || descriptor.id !== "contextWindow")
197 .map(descriptorToEditor);
199​
200export function validateDraft(draft: CustomModelDraft): string | null {
201 const seenIds = new Set<string>();
202 for (const [index, descriptor] of draft.descriptors.entries()) {
203 const position = `Option ${index + 1}`;
204 const id = descriptor.id.trim();
205 if (!id) return `${position} needs an id.`;
206 if (seenIds.has(id)) return `${position}: id "${id}" is used twice.`;
207 seenIds.add(id);
208 if (!descriptor.label.trim()) return `${position} needs a label.`;
209 if (descriptor.type !== "select") continue;
210 if (descriptor.choices.length === 0) return `${position} needs at least one choice.`;
211 const seenChoices = new Set<string>();
212 for (const choice of descriptor.choices) {
213 const choiceId = choice.id.trim();
214 if (!choiceId) return `${position} has a choice without a value.`;
215 if (seenChoices.has(choiceId)) {
216 return `${position}: choice "${choiceId}" is used twice.`;
217 }
218 seenChoices.add(choiceId);
219 }
220 }
221 return null;
223​
224export function definitionFromDraft(draft: CustomModelDraft): CustomModelDefinition {
225 const descriptors: ProviderOptionDescriptor[] = draft.descriptors.map((descriptor) => {
226 const id = descriptor.id.trim();
227 const label = descriptor.label.trim();
228 if (descriptor.type === "boolean") {
229 return {
230 id,
231 label,
232 type: "boolean",
233 ...(descriptor.description !== undefined ? { description: descriptor.description } : {}),
234 ...(descriptor.currentBooleanValue !== undefined
235 ? { currentValue: descriptor.currentBooleanValue }
236 : {}),
237 };
238 }
239 const options = descriptor.choices.map((choice) => ({
240 id: choice.id.trim(),
241 label: choice.label.trim() || choice.id.trim(),
242 ...(choice.description !== undefined ? { description: choice.description } : {}),
243 ...(choice.isDefault ? { isDefault: true } : {}),
244 }));
245 const currentValue = options.find((option) => option.isDefault)?.id;
246 return {
247 id,
248 label,
249 type: "select",
250 ...(descriptor.description !== undefined ? { description: descriptor.description } : {}),
251 options,
252 ...(currentValue ? { currentValue } : {}),
253 };
254 });
255 const name = draft.name.trim();
256 return {
257 slug: draft.slug,
258 name: name || draft.slug,
259 capabilities:
260 descriptors.length > 0 ? createModelCapabilities({ optionDescriptors: descriptors }) : null,
261 };