@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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