Open History to see the last commit
1import type {
2 ModelSelection,
3 ProviderDriverKind,
4 ProviderOptionDescriptor,
5} from "@t3tools/contracts";
6import {
7 buildProviderOptionSelectionsFromDescriptors,
8 getProviderOptionCurrentLabel,
9 getProviderOptionCurrentValue,
10 isClaudeUltrathinkPrompt,
11} from "@t3tools/shared/model";
12import { REASONING_EFFORT_OPTION_IDS } from "./state/threadExecution.ts";
13
14const REASONING_DESCRIPTOR_IDS: ReadonlySet<string> = new Set([
15 ...REASONING_EFFORT_OPTION_IDS,
16 "thinking",
17]);
18
19/** The model's reasoning control: a known effort id, or a select its agent marked as reasoning. */
20export function isReasoningControl(descriptor: ProviderOptionDescriptor): boolean {
21 return (
22 (descriptor.type === "select" && descriptor.role === "reasoning") ||
23 REASONING_DESCRIPTOR_IDS.has(descriptor.id)
24 );
25}
26
27export function isReasoningSelect(
28 descriptor: ProviderOptionDescriptor,
29): descriptor is Extract<ProviderOptionDescriptor, { type: "select" }> {
30 return descriptor.type === "select" && isReasoningControl(descriptor);
31}
32
33export function replaceDescriptorCurrentValue(
34 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
35 descriptorId: string,
36 currentValue: string | boolean | undefined,
37): ReadonlyArray<ProviderOptionDescriptor> {
38 return descriptors.map((descriptor) =>
39 descriptor.id !== descriptorId
40 ? descriptor
41 : descriptor.type === "boolean"
42 ? {
43 ...descriptor,
44 ...(typeof currentValue === "boolean" ? { currentValue } : {}),
45 }
46 : {
47 ...descriptor,
48 ...(typeof currentValue === "string" ? { currentValue } : {}),
49 },
50 );
51}
52
53export function getDescriptorStringValue(
54 descriptor: Extract<ProviderOptionDescriptor, { type: "select" }> | null,
55 selection?: ModelSelection | null,
56 reportedSelection?: ModelSelection | null,
57): string | null {
58 if (!descriptor) {
59 return null;
60 }
61 const value = getProviderOptionCurrentValue(descriptor, selection, reportedSelection);
62 return typeof value === "string" ? value : null;
63}
64
65// Prompt-injected levels such as Ultrathink still work when typed, but pickers do not offer them.
66export function selectableProviderOptionChoices(
67 descriptor: Extract<ProviderOptionDescriptor, { type: "select" }>,
68) {
69 return descriptor.options.filter(
70 (option) => !descriptor.promptInjectedValues?.includes(option.id),
71 );
72}
73
74export function getPromptControlledTraits(
75 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
76 prompt: string,
77 allowPromptInjectedEffort = true,
78) {
79 const primarySelectDescriptor =
80 descriptors.find((descriptor) => descriptor.type === "select") ?? null;
81 const ultrathinkPromptControlled =
82 allowPromptInjectedEffort &&
83 (primarySelectDescriptor?.promptInjectedValues?.length ?? 0) > 0 &&
84 isClaudeUltrathinkPrompt(prompt);
85 const ultrathinkInBodyText =
86 ultrathinkPromptControlled && isClaudeUltrathinkPrompt(prompt.replace(/^Ultrathink:\s*/i, ""));
87 return { primarySelectDescriptor, ultrathinkPromptControlled, ultrathinkInBodyText };
88}
89
90export function getTraitSelectChange(
91 selected: ReturnType<typeof getPromptControlledTraits> & {
92 descriptors: ReadonlyArray<ProviderOptionDescriptor>;
93 },
94 descriptor: Extract<ProviderOptionDescriptor, { type: "select" }>,
95 prompt: string,
96 value: string,
97) {
98 if (!value || descriptor.promptInjectedValues?.includes(value)) return null;
99 const isPrimary = descriptor.id === selected.primarySelectDescriptor?.id;
100 if (isPrimary && selected.ultrathinkInBodyText) return null;
101 return {
102 prompt:
103 isPrimary && selected.ultrathinkPromptControlled
104 ? prompt.replace(/^Ultrathink:\s*/i, "")
105 : prompt,
106 modelOptions: buildProviderOptionSelectionsFromDescriptors(
107 replaceDescriptorCurrentValue(selected.descriptors, descriptor.id, value),
108 ),
109 };
110}
111
112export function buildTraitsTriggerDisplay(input: {
113 provider: ProviderDriverKind;
114 descriptors: ReadonlyArray<ProviderOptionDescriptor>;
115 primarySelectDescriptorId: string | null;
116 ultrathinkPromptControlled: boolean;
117 modelSelection?: ModelSelection | null;
118 reportedModelSelection?: ModelSelection | null | undefined;
119}): {
120 label: string;
121 /** "Fast" or "Ultrafast" when a speed option is on. */
122 speedLabel: string | null;
123 ultracode: boolean;
124} {
125 let fastModeFallbackLabel: string | null = null;
126 let speedLabel: string | null = null;
127 let reasoningLabelIndex = -1;
128 let ultracodeLabelIndex = -1;
129 const labels: Array<string> = [];
130 for (const descriptor of input.descriptors) {
131 if (descriptor.id === "fastMode" && descriptor.type === "boolean") {
132 speedLabel = descriptor.currentValue === true ? "Fast" : null;
133 fastModeFallbackLabel = speedLabel ?? "Normal";
134 continue;
135 }
136 if (descriptor.id === "ultracode" && descriptor.type === "boolean") {
137 if (descriptor.currentValue === true) {
138 ultracodeLabelIndex = labels.length;
139 labels.push(descriptor.label);
140 }
141 continue;
142 }
143 if (
144 input.provider === "codex" &&
145 descriptor.id === "serviceTier" &&
146 descriptor.type === "select"
147 ) {
148 const currentValue = getProviderOptionCurrentValue(descriptor);
149 const fastTier = descriptor.options.find(({ label }) => label === "Fast");
150 const ultrafastTier = descriptor.options.find(({ label }) => label === "Ultrafast");
151 if (
152 ((fastTier || ultrafastTier) && currentValue === "default") ||
153 (fastTier && currentValue === fastTier.id) ||
154 (ultrafastTier && currentValue === ultrafastTier.id)
155 ) {
156 speedLabel =
157 ultrafastTier && currentValue === ultrafastTier.id
158 ? "Ultrafast"
159 : fastTier && currentValue === fastTier.id
160 ? "Fast"
161 : null;
162 fastModeFallbackLabel =
163 descriptor.options.find(({ id }) => id === currentValue)?.label ?? "Normal";
164 continue;
165 }
166 }
167 const label =
168 input.ultrathinkPromptControlled && descriptor.id === input.primarySelectDescriptorId
169 ? "Ultrathink"
170 : descriptor.type === "boolean"
171 ? `${descriptor.label} ${descriptor.currentValue === true ? "On" : "Off"}`
172 : getProviderOptionCurrentLabel(
173 descriptor,
174 input.modelSelection,
175 input.reportedModelSelection,
176 );
177 if (typeof label === "string" && label.length > 0) {
178 if (reasoningLabelIndex === -1 && isReasoningSelect(descriptor)) {
179 reasoningLabelIndex = labels.length;
180 }
181 labels.push(label);
182 }
183 }
184
185 const ultracode = ultracodeLabelIndex >= 0;
186 if (labels.length === 0 && fastModeFallbackLabel !== null) {
187 return { label: fastModeFallbackLabel, speedLabel, ultracode };
188 }
189 if (speedLabel) {
190 if (reasoningLabelIndex >= 0) {
191 labels[reasoningLabelIndex] = `${labels[reasoningLabelIndex]} ${speedLabel}`;
192 } else {
193 labels.push(speedLabel);
194 }
195 }
196 return { label: labels.join(" · "), speedLabel, ultracode };
197}