Repository navigation
Expand file tree
/
Copy pathmodel.ts
More file actions
529 lines (479 loc) · 17.1 KB
/
Copy pathmodel.ts
File metadata and controls
529 lines (479 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import {
type CustomModelSetting,
MODEL_SLUG_ALIASES_BY_PROVIDER,
ModelCapabilities,
type ModelSelection,
ProviderDriverKind,
ProviderInstanceId,
type ProviderOptionDescriptor,
type ProviderOptionSelection,
} from "@t3tools/contracts";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import { copySorted } from "./Array.ts";
const DEFAULT_PROVIDER_DRIVER_KIND = ProviderDriverKind.make("codex");
/** Choose the command for a model change against the thread's current provider instance. */
export function modelSelectionCommandType(
currentInstanceId: ProviderInstanceId,
selection: ModelSelection,
) {
return currentInstanceId === selection.instanceId
? ("thread.model-selection.set" as const)
: ("provider.switch" as const);
}
export interface SelectableModelOption {
slug: string;
name: string;
aliases?: ReadonlyArray<string> | undefined;
}
export function createModelCapabilities(input: {
optionDescriptors: ReadonlyArray<ProviderOptionDescriptor>;
}): ModelCapabilities {
return {
optionDescriptors: input.optionDescriptors.map(cloneDescriptor),
};
}
function getRawSelectionValueById(
selections: ReadonlyArray<ProviderOptionSelection> | null | undefined,
id: string,
): string | boolean | undefined {
const selection = selections?.find((candidate) => candidate.id === id);
return selection?.value;
}
function getProviderOptionSelectionValue(
selections: ReadonlyArray<ProviderOptionSelection> | null | undefined,
id: string,
): string | boolean | undefined {
return getRawSelectionValueById(selections, id);
}
export function getProviderOptionStringSelectionValue(
selections: ReadonlyArray<ProviderOptionSelection> | null | undefined,
id: string,
): string | undefined {
const value = getProviderOptionSelectionValue(selections, id);
return typeof value === "string" ? value : undefined;
}
export function getProviderOptionBooleanSelectionValue(
selections: ReadonlyArray<ProviderOptionSelection> | null | undefined,
id: string,
): boolean | undefined {
const value = getProviderOptionSelectionValue(selections, id);
return typeof value === "boolean" ? value : undefined;
}
export function getModelSelectionStringOptionValue(
modelSelection: ModelSelection | null | undefined,
id: string,
): string | undefined {
return getProviderOptionStringSelectionValue(modelSelection?.options, id);
}
export function getModelSelectionBooleanOptionValue(
modelSelection: ModelSelection | null | undefined,
id: string,
): boolean | undefined {
return getProviderOptionBooleanSelectionValue(modelSelection?.options, id);
}
function canonicalModelSelectionOptions(
modelSelection: ModelSelection,
): ReadonlyArray<readonly [id: string, value: string | boolean]> {
return copySorted(
(modelSelection.options ?? []).map(
(selection): readonly [id: string, value: string | boolean] => [
selection.id,
selection.value,
],
),
(
[leftId, leftValue]: readonly [id: string, value: string | boolean],
[rightId, rightValue]: readonly [id: string, value: string | boolean],
) => {
const idOrder = leftId.localeCompare(rightId);
return idOrder !== 0 ? idOrder : String(leftValue).localeCompare(String(rightValue));
},
);
}
/**
* Compares the complete provider selection while treating option ordering and
* an omitted empty option list as presentation details.
*/
export function modelSelectionsEqual(left: ModelSelection, right: ModelSelection): boolean {
if (left.instanceId !== right.instanceId || left.model !== right.model) {
return false;
}
const leftOptions = canonicalModelSelectionOptions(left);
const rightOptions = canonicalModelSelectionOptions(right);
return (
leftOptions.length === rightOptions.length &&
leftOptions.every(
([id, value], index) => id === rightOptions[index]?.[0] && value === rightOptions[index]?.[1],
)
);
}
function resolveDescriptorChoiceValue(
descriptor: Extract<ProviderOptionDescriptor, { type: "select" }>,
raw: string | null | undefined,
): string | undefined {
const trimmed = trimOrNull(raw);
if (!trimmed) {
return descriptor.currentValue ?? descriptor.options.find((option) => option.isDefault)?.id;
}
if (descriptor.options.length === 0) {
return trimmed;
}
if (
descriptor.promptInjectedValues?.includes(trimmed) &&
descriptor.options.some((option) => option.id === trimmed)
) {
return descriptor.options.find((option) => option.isDefault)?.id;
}
if (descriptor.options.some((option) => option.id === trimmed)) {
return trimmed;
}
return descriptor.currentValue ?? descriptor.options.find((option) => option.isDefault)?.id;
}
function cloneDescriptor(descriptor: ProviderOptionDescriptor): ProviderOptionDescriptor {
return descriptor.type === "select"
? {
...descriptor,
options: [...descriptor.options],
...(descriptor.promptInjectedValues
? { promptInjectedValues: [...descriptor.promptInjectedValues] }
: {}),
}
: { ...descriptor };
}
function cloneSelection(selection: ProviderOptionSelection): ProviderOptionSelection {
return { ...selection };
}
function withDescriptorCurrentValue(
descriptor: ProviderOptionDescriptor,
rawCurrentValue: string | boolean | undefined,
): ProviderOptionDescriptor {
if (descriptor.type === "boolean") {
if (typeof rawCurrentValue === "boolean") {
return {
...descriptor,
currentValue: rawCurrentValue,
};
}
return descriptor;
}
const currentValue =
typeof rawCurrentValue === "string"
? resolveDescriptorChoiceValue(descriptor, rawCurrentValue)
: resolveDescriptorChoiceValue(descriptor, descriptor.currentValue);
if (!currentValue) {
const { currentValue: _unusedCurrentValue, ...rest } = descriptor;
return rest;
}
return {
...descriptor,
currentValue,
};
}
export function getProviderOptionDescriptors(input: {
caps: ModelCapabilities;
selections?: ReadonlyArray<ProviderOptionSelection> | null | undefined;
}): ReadonlyArray<ProviderOptionDescriptor> {
const { caps, selections } = input;
const baseDescriptors = (caps.optionDescriptors ?? []).map(cloneDescriptor);
return baseDescriptors.map((descriptor) =>
withDescriptorCurrentValue(
descriptor,
getRawSelectionValueById(selections, descriptor.id) ?? descriptor.currentValue,
),
);
}
function getReportedOptionValue(
id: string,
selection?: ModelSelection | null,
reportedSelection?: ModelSelection | null,
) {
if (
!selection ||
!reportedSelection ||
selection.instanceId !== reportedSelection.instanceId ||
selection.model !== reportedSelection.model ||
selection.options?.some((option) => option.id === id)
)
return undefined;
return getRawSelectionValueById(reportedSelection.options, id);
}
export function getProviderOptionCurrentValue(
descriptor: ProviderOptionDescriptor | null | undefined,
selection?: ModelSelection | null,
reportedSelection?: ModelSelection | null,
): string | boolean | undefined {
if (!descriptor) {
return undefined;
}
const hasExplicitOption = selection?.options?.some((option) => option.id === descriptor.id);
// Reported values are display-only; callers that build dispatch options omit this context.
const reportedValue = getReportedOptionValue(descriptor.id, selection, reportedSelection);
if (reportedValue !== undefined) return reportedValue;
if (descriptor.id === "variant" && selection && !hasExplicitOption) return undefined;
if (descriptor.type === "boolean") {
return descriptor.currentValue;
}
if (descriptor.currentValue) {
return descriptor.currentValue;
}
return descriptor.options.find((option) => option.isDefault)?.id;
}
export function getProviderOptionCurrentLabel(
descriptor: ProviderOptionDescriptor | null | undefined,
selection?: ModelSelection | null,
reportedSelection?: ModelSelection | null,
): string | undefined {
if (!descriptor) {
return undefined;
}
if (descriptor.type === "boolean") {
return typeof descriptor.currentValue === "boolean"
? descriptor.currentValue
? "On"
: "Off"
: undefined;
}
const currentValue = getProviderOptionCurrentValue(descriptor, selection, reportedSelection);
return (
descriptor.options.find((option) => option.id === currentValue)?.label ??
(getReportedOptionValue(descriptor.id, selection, reportedSelection) === "default"
? "Default"
: descriptor.id === "variant"
? "Unknown"
: undefined)
);
}
export function buildProviderOptionSelectionsFromDescriptors(
descriptors: ReadonlyArray<ProviderOptionDescriptor> | null | undefined,
): Array<ProviderOptionSelection> | undefined {
if (!descriptors || descriptors.length === 0) {
return undefined;
}
const nextSelections: Array<ProviderOptionSelection> = [];
for (const descriptor of descriptors) {
const value = getProviderOptionCurrentValue(descriptor);
if (typeof value === "string" || typeof value === "boolean") {
nextSelections.push({ id: descriptor.id, value });
}
}
return nextSelections.length > 0 ? nextSelections : undefined;
}
export function buildExplicitProviderOptionSelectionsFromDescriptors(
descriptors: ReadonlyArray<ProviderOptionDescriptor> | null | undefined,
selections: ReadonlyArray<ProviderOptionSelection> | null | undefined,
): Array<ProviderOptionSelection> | undefined {
if (!selections || selections.length === 0) {
return undefined;
}
const explicitIds = new Set(selections.map((selection) => selection.id));
const normalized = buildProviderOptionSelectionsFromDescriptors(descriptors)?.filter(
(selection) => explicitIds.has(selection.id),
);
return normalized && normalized.length > 0 ? normalized : undefined;
}
export function isClaudeUltrathinkPrompt(text: string | null | undefined): boolean {
return typeof text === "string" && /\bultrathink\b/i.test(text);
}
/** Compare Codex model families without changing provider-owned dispatch identifiers. */
export function codexModelFamily(slug: string): string {
return slug.startsWith("openai.gpt-") ? slug.slice("openai.".length) : slug;
}
export function formatCodexModelName(name: string): string {
return name.replace(/^gpt/i, "GPT").replace(/-([a-z])/g, (_, c) => "-" + c.toUpperCase());
}
export function formatModelSlugName(slug: string): string {
const separator = slug.lastIndexOf("/") + 1;
const prefix = slug.slice(0, separator);
const name = slug.slice(separator);
if (/^gpt-\d/i.test(name)) return prefix + formatCodexModelName(name);
if (!/^(claude-(opus|sonnet|haiku|fable)|gemini|grok|composer)-\d/i.test(name)) return slug;
return (
prefix +
name
.replace(/^(claude-[a-z]+-\d+)-(\d{1,2})(?=-|\[|$)/i, "$1.$2")
.split("-")
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
.join(" ")
);
}
export function normalizeModelSlug(
model: string | null | undefined,
provider: ProviderDriverKind = DEFAULT_PROVIDER_DRIVER_KIND,
): string | null {
const trimmed = normalizeCustomModelSlug(model);
if (!trimmed) {
return null;
}
const aliases = MODEL_SLUG_ALIASES_BY_PROVIDER[provider] ?? {};
const aliased = Object.prototype.hasOwnProperty.call(aliases, trimmed)
? aliases[trimmed]
: undefined;
return typeof aliased === "string" ? aliased : trimmed;
}
/** Custom model identifiers are provider-owned, so only trim them; never expand aliases. */
export function normalizeCustomModelSlug(model: string | null | undefined): string | null {
if (typeof model !== "string") {
return null;
}
return model.trim() || null;
}
/** A custom model setting with its optional fields resolved. */
export interface CustomModelDefinition {
readonly slug: string;
readonly name: string;
readonly capabilities: ModelCapabilities | null;
}
const decodeCustomModelCapabilities = Schema.decodeUnknownOption(ModelCapabilities);
/**
* Read a `customModels` setting into resolved definitions. Accepts the typed
* union as well as the opaque `providerInstances[id].config` blob clients see,
* so it tolerates bare slugs, malformed rows, and unparseable capabilities
* (dropped rather than failing the whole list). Slugs are trimmed and
* deduplicated, first occurrence wins; `name` falls back to the slug.
*/
export function readCustomModelEntries(value: unknown): CustomModelDefinition[] {
if (!Array.isArray(value)) return [];
const entries: CustomModelDefinition[] = [];
const seen = new Set<string>();
for (const raw of value) {
const record =
typeof raw === "string"
? { slug: raw }
: raw !== null && typeof raw === "object"
? (raw as { slug?: unknown; name?: unknown; capabilities?: unknown })
: null;
if (!record) continue;
const slug = normalizeCustomModelSlug(typeof record.slug === "string" ? record.slug : null);
if (!slug || seen.has(slug)) continue;
seen.add(slug);
const name =
(typeof record.name === "string" ? normalizeCustomModelSlug(record.name) : null) ?? slug;
const capabilities =
record.capabilities === undefined || record.capabilities === null
? null
: Option.getOrNull(decodeCustomModelCapabilities(record.capabilities));
entries.push({
slug,
name,
capabilities: capabilities
? createModelCapabilities({ optionDescriptors: capabilities.optionDescriptors ?? [] })
: null,
});
}
return entries;
}
/**
* Write a definition back to the compact stored shape: a bare slug when it
* carries nothing custom, otherwise an entry with only the set fields.
*/
export function toCustomModelSetting(entry: CustomModelDefinition): CustomModelSetting {
const descriptors = entry.capabilities?.optionDescriptors ?? [];
const name = entry.name !== entry.slug ? entry.name : undefined;
if (!name && descriptors.length === 0) return entry.slug;
return {
slug: entry.slug,
...(name ? { name } : {}),
...(descriptors.length > 0
? { capabilities: createModelCapabilities({ optionDescriptors: descriptors }) }
: {}),
};
}
export function resolveSelectableModel(
provider: ProviderDriverKind,
value: string | null | undefined,
options: ReadonlyArray<SelectableModelOption>,
): string | null {
if (typeof value !== "string") {
return null;
}
const trimmed = value.trim();
if (!trimmed) {
return null;
}
const direct = options.find((option) => option.slug === trimmed);
if (direct) {
return direct.slug;
}
const byName = options.find((option) => option.name.toLowerCase() === trimmed.toLowerCase());
if (byName) {
return byName.slug;
}
const byAlias = options.find((option) =>
option.aliases?.some((alias) => alias.toLowerCase() === trimmed.toLowerCase()),
);
if (byAlias) {
return byAlias.slug;
}
const normalized = normalizeModelSlug(trimmed, provider);
if (!normalized) {
return null;
}
const resolved = options.find((option) => option.slug === normalized);
return resolved ? resolved.slug : null;
}
/** Trim a string, returning null for empty/missing values. */
function trimOrNull<T extends string>(value: T | null | undefined): T | null {
if (typeof value !== "string") return null;
const trimmed = value.trim() as T;
return trimmed || null;
}
function cloneSelections(
selections: ReadonlyArray<ProviderOptionSelection>,
): Array<ProviderOptionSelection> {
return selections.map(cloneSelection);
}
export function createModelSelection(
instanceId: ProviderInstanceId,
model: string,
options?: ReadonlyArray<ProviderOptionSelection> | null,
): ModelSelection {
const selections = options ? cloneSelections(options) : [];
const base: ModelSelection = {
instanceId,
model,
};
return selections.length > 0 ? { ...base, options: selections } : base;
}
/**
* Returns the effort value if it is a prompt-injected value according to
* any select descriptor in the given capabilities, or null otherwise.
*
* Unlike a single `find`, this checks every descriptor so that the
* correct descriptor's `promptInjectedValues` list is consulted even when
* multiple select descriptors exist.
*/
export function resolvePromptInjectedEffort(
caps: ModelCapabilities,
rawEffort: string | null | undefined,
): string | null {
const trimmed = trimOrNull(rawEffort);
if (!trimmed) return null;
const descriptors = getProviderOptionDescriptors({ caps });
for (const descriptor of descriptors) {
if (descriptor.type === "select" && descriptor.promptInjectedValues?.includes(trimmed)) {
return trimmed;
}
}
return null;
}
export function applyClaudePromptEffortPrefix(
text: string,
effort: string | null | undefined,
): string {
const trimmed = text.trim();
if (!trimmed) {
return trimmed;
}
// Prefixing a slash command turns it into plain prose, so Claude never
// runs it. Command names come from arbitrary file names ("/deploy.prod",
// "/plugin:skill"), so accept any first token without a second slash;
// absolute paths like "/home/theo/app.ts" keep the prefix.
if (effort !== "ultrathink" || /^\/[^\s/]+(?:\s|$)/u.test(trimmed)) {
return trimmed;
}
if (trimmed.startsWith("Ultrathink:")) {
return trimmed;
}
return `Ultrathink:\n${trimmed}`;
}